Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:01

0001 //#define EDM_ML_DEBUG
0002 
0003 #include "SimG4CMS/Forward/interface/MtdSD.h"
0004 
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 
0008 #include "Geometry/MTDCommonData/interface/BTLNumberingScheme.h"
0009 #include "Geometry/MTDCommonData/interface/ETLNumberingScheme.h"
0010 #include "DataFormats/ForwardDetId/interface/MTDDetId.h"
0011 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0012 
0013 #include "G4Track.hh"
0014 #include "G4Step.hh"
0015 #include "G4StepPoint.hh"
0016 
0017 #include <iostream>
0018 
0019 //-------------------------------------------------------------------
0020 MtdSD::MtdSD(const std::string& name,
0021              const SensitiveDetectorCatalog& clg,
0022              edm::ParameterSet const& p,
0023              const SimTrackManager* manager)
0024     : TimingSD(name, clg, manager), numberingScheme(nullptr) {
0025   //Parameters
0026   edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("MtdSD");
0027   int verbn = m_p.getUntrackedParameter<int>("Verbosity");
0028 
0029   SetVerboseLevel(verbn);
0030 
0031   MTDNumberingScheme* scheme = nullptr;
0032   if (name == "FastTimerHitsBarrel") {
0033     scheme = dynamic_cast<MTDNumberingScheme*>(new BTLNumberingScheme());
0034     isBTL = true;
0035   } else if (name == "FastTimerHitsEndcap") {
0036     scheme = dynamic_cast<MTDNumberingScheme*>(new ETLNumberingScheme());
0037     isETL = true;
0038   } else {
0039     scheme = nullptr;
0040     edm::LogWarning("MtdSim") << "MtdSD: ReadoutName not supported";
0041   }
0042   if (scheme)
0043     setNumberingScheme(scheme);
0044 
0045   energyCut = m_p.getParameter<double>("EnergyThresholdForPersistencyInGeV") * CLHEP::GeV;     //default must be 0.5
0046   energyHistoryCut = m_p.getParameter<double>("EnergyThresholdForHistoryInGeV") * CLHEP::GeV;  //default must be 0.05
0047 
0048   setCuts(energyCut, energyHistoryCut);
0049 
0050   double newTimeFactor = 1. / m_p.getParameter<double>("TimeSliceUnit");
0051   edm::LogVerbatim("MtdSim") << "New time factor = " << newTimeFactor;
0052   setTimeFactor(newTimeFactor);
0053 
0054   edm::LogVerbatim("MtdSim") << "MtdSD: Instantiation completed for " << name;
0055 }
0056 
0057 MtdSD::~MtdSD() {}
0058 
0059 uint32_t MtdSD::setDetUnitId(const G4Step* aStep) {
0060   if (numberingScheme == nullptr) {
0061     return MTDDetId();
0062   } else {
0063     getBaseNumber(aStep);
0064 #ifdef EDM_ML_DEBUG
0065     edm::LogVerbatim("MtdSim") << "DetId = " << numberingScheme->getUnitID(theBaseNumber);
0066 #endif
0067     return numberingScheme->getUnitID(theBaseNumber);
0068   }
0069 }
0070 
0071 void MtdSD::setNumberingScheme(MTDNumberingScheme* scheme) {
0072   if (scheme != nullptr) {
0073     edm::LogVerbatim("MtdSim") << "MtdSD: updates numbering scheme for " << GetName();
0074     if (numberingScheme)
0075       delete numberingScheme;
0076     numberingScheme = scheme;
0077   }
0078 }
0079 
0080 void MtdSD::getBaseNumber(const G4Step* aStep) {
0081   theBaseNumber.reset();
0082   const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
0083   int theSize = touch->GetHistoryDepth() + 1;
0084   if (theBaseNumber.getCapacity() < theSize)
0085     theBaseNumber.setSize(theSize);
0086   //Get name and copy numbers
0087   if (theSize > 1) {
0088 #ifdef EDM_ML_DEBUG
0089     edm::LogVerbatim("MtdSim") << "Building MTD basenumber:";
0090 #endif
0091     for (int ii = 0; ii < theSize; ii++) {
0092       theBaseNumber.addLevel(touch->GetVolume(ii)->GetName(), touch->GetReplicaNumber(ii));
0093 #ifdef EDM_ML_DEBUG
0094       edm::LogVerbatim("MtdSim") << "MtdSD::getBaseNumber(): Adding level " << ii << ": "
0095                                  << touch->GetVolume(ii)->GetName() << "[" << touch->GetReplicaNumber(ii) << "]";
0096 #endif
0097     }
0098   }
0099 }
0100 
0101 int MtdSD::getTrackID(const G4Track* aTrack) {
0102   int theID = aTrack->GetTrackID();
0103   TrackInformation* trkInfo = cmsTrackInformation(aTrack);
0104   const G4String& rname = aTrack->GetVolume()->GetLogicalVolume()->GetRegion()->GetName();
0105   if (trkInfo != nullptr) {
0106 #ifdef EDM_ML_DEBUG
0107     trkInfo->Print();
0108 #endif
0109     if (rname == "FastTimerRegionSensBTL") {
0110       theID = trkInfo->mcTruthID();
0111       if (trkInfo->isExtSecondary() && !trkInfo->isInTrkFromBackscattering()) {
0112         theID = PSimHit::addTrackIdOffset(theID, k_idsecOffset);
0113       } else if (trkInfo->isInTrkFromBackscattering()) {
0114         theID = PSimHit::addTrackIdOffset(theID, k_idFromCaloOffset);
0115       } else if (trkInfo->isBTLlooper()) {
0116         theID = PSimHit::addTrackIdOffset(theID, k_idloopOffset);
0117       }
0118 #ifdef EDM_ML_DEBUG
0119       edm::LogVerbatim("MtdSim") << "MtdSD: Track ID: " << aTrack->GetTrackID()
0120                                  << " BTL Track ID: " << trkInfo->mcTruthID() << ":" << theID;
0121 #endif
0122     } else if (rname == "FastTimerRegionSensETL") {
0123       theID = trkInfo->getIDonCaloSurface();
0124 #ifdef EDM_ML_DEBUG
0125       edm::LogVerbatim("MtdSim") << "MtdSD: Track ID: " << aTrack->GetTrackID()
0126                                  << " ETL Track ID: " << trkInfo->mcTruthID() << ":" << theID;
0127 #endif
0128     } else {
0129       throw cms::Exception("MtdSDError") << "MtdSD called in incorrect region " << rname;
0130     }
0131   } else {
0132 #ifdef EDM_ML_DEBUG
0133     edm::LogWarning("MtdSim") << "MtdSD: Problem with primaryID **** set by force to TkID **** " << theID;
0134 #endif
0135   }
0136   return theID;
0137 }