Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:29:47

0001 #ifndef SimG4CMS_CaloSD_h
0002 #define SimG4CMS_CaloSD_h
0003 ///////////////////////////////////////////////////////////////////////////////
0004 // File: CaloSD.h
0005 // Description: Stores hits of calorimetric type in appropriate container
0006 // Use in your sensitive detector builder:
0007 //    CaloSD* caloSD = new CaloSD(SDname, new CaloNumberingScheme());
0008 ///////////////////////////////////////////////////////////////////////////////
0009 
0010 #include "SimG4CMS/Calo/interface/CaloG4Hit.h"
0011 #include "SimG4CMS/Calo/interface/CaloG4HitCollection.h"
0012 #include "SimG4CMS/Calo/interface/CaloMeanResponse.h"
0013 #include "SimG4Core/Notification/interface/Observer.h"
0014 #include "SimG4Core/Notification/interface/BeginOfRun.h"
0015 #include "SimG4Core/Notification/interface/BeginOfEvent.h"
0016 #include "SimG4Core/Notification/interface/BeginOfTrack.h"
0017 #include "SimG4Core/Notification/interface/EndOfTrack.h"
0018 #include "SimG4Core/Notification/interface/EndOfEvent.h"
0019 #include "SimG4Core/Notification/interface/TrackWithHistory.h"
0020 #include "SimG4Core/SensitiveDetector/interface/SensitiveCaloDetector.h"
0021 
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0024 
0025 #include "G4VPhysicalVolume.hh"
0026 #include "G4Track.hh"
0027 #include "G4VGFlashSensitiveDetector.hh"
0028 
0029 #include <vector>
0030 #include <map>
0031 #include <unordered_map>
0032 #include <memory>
0033 
0034 class G4Step;
0035 class G4HCofThisEvent;
0036 class CaloSlaveSD;
0037 class G4GFlashSpot;
0038 class SimTrackManager;
0039 
0040 class CaloSD : public SensitiveCaloDetector,
0041                public G4VGFlashSensitiveDetector,
0042                public Observer<const BeginOfRun*>,
0043                public Observer<const BeginOfEvent*>,
0044                public Observer<const BeginOfTrack*>,
0045                public Observer<const EndOfTrack*>,
0046                public Observer<const EndOfEvent*> {
0047 public:
0048   CaloSD(const std::string& aSDname,
0049          const SensitiveDetectorCatalog& clg,
0050          edm::ParameterSet const& p,
0051          const SimTrackManager*,
0052          float timeSlice = 1.,
0053          bool ignoreTkID = false,
0054          const std::string& newcolname = "");
0055   ~CaloSD() override;
0056 
0057   void newCollection(const std::string& name, edm::ParameterSet const& p);
0058   G4bool ProcessHits(G4Step* step, G4TouchableHistory*) override;
0059   bool ProcessHits(G4GFlashSpot* aSpot, G4TouchableHistory*) override;
0060 
0061   uint32_t setDetUnitId(const G4Step* step) override = 0;
0062 
0063   void Initialize(G4HCofThisEvent* HCE) override;
0064   void EndOfEvent(G4HCofThisEvent* eventHC) override;
0065   void clear() override;
0066   void DrawAll() override;
0067   void PrintAll() override;
0068 
0069   void clearHits() override;
0070   void fillHits(edm::PCaloHitContainer&, const std::string&) override;
0071   void reset() override;
0072 
0073   bool isItFineCalo(const G4VTouchable* touch);
0074 
0075 protected:
0076   virtual double getEnergyDeposit(const G4Step* step);
0077   virtual double EnergyCorrected(const G4Step& step, const G4Track*);
0078   virtual bool getFromLibrary(const G4Step* step);
0079 
0080   G4ThreeVector setToLocal(const G4ThreeVector&, const G4VTouchable*) const;
0081   G4ThreeVector setToGlobal(const G4ThreeVector&, const G4VTouchable*) const;
0082 
0083   bool hitExists(const G4Step*, int k);
0084   bool checkHit(int k = 0);
0085   CaloG4Hit* createNewHit(const G4Step*, const G4Track*, int k);
0086   void updateHit(CaloG4Hit*, int k);
0087   void resetForNewPrimary(const G4Step*);
0088   double getAttenuation(const G4Step* aStep, double birk1, double birk2, double birk3) const;
0089 
0090   static std::string printableDecayChain(const std::vector<unsigned int>& decayChain);
0091   std::string shortreprID(const CaloHitID& ID);
0092   std::string shortreprID(const CaloG4Hit* hit);
0093   unsigned int findBoundaryCrossingParent(const G4Track* track, bool markParentAsSaveable = true);
0094 
0095   void update(const BeginOfRun*) override;
0096   void update(const BeginOfEvent*) override;
0097   void update(const BeginOfTrack* trk) override;
0098   void update(const EndOfTrack* trk) override;
0099   void update(const ::EndOfEvent*) override;
0100   virtual void initRun();
0101   virtual void initEvent(const BeginOfEvent*);
0102   virtual void endEvent();
0103   virtual bool filterHit(CaloG4Hit*, double);
0104 
0105   virtual int getTrackID(const G4Track*);
0106   virtual int setTrackID(const G4Step*);
0107   virtual uint16_t getDepth(const G4Step*);
0108   virtual void processSecondHit(const G4Step*, const G4Track*);
0109 
0110   double getResponseWt(const G4Track*, int k = 0);
0111   int getNumberOfHits(int k = 0);
0112   void ignoreRejection() { ignoreReject = true; }
0113 
0114   inline void setParameterized(bool val) { isParameterized = val; }
0115   inline void setUseMap(bool val) { useMap = val; }
0116 
0117   inline void processHit(const G4Step* step) {
0118     // check if it is in the same unit and timeslice as the previous one
0119     if (currentID[0] == previousID[0]) {
0120       updateHit(currentHit[0], 0);
0121     } else if (!checkHit()) {
0122       currentHit[0] = createNewHit(step, step->GetTrack(), 0);
0123     }
0124   }
0125 
0126   inline void setNumberCheckedHits(int val, int k = 0) { nCheckedHits[k] = val; }
0127   void printDetectorLevels(const G4VTouchable*) const;
0128 
0129 private:
0130   void storeHit(CaloG4Hit*, int k = 0);
0131   bool saveHit(CaloG4Hit*, int k = 0);
0132   void cleanHitCollection();
0133 
0134 protected:
0135   // Data relative to primary particle (the one which triggers a shower)
0136   // These data are common to all Hits of a given shower.
0137   // One shower is made of several hits which differ by the
0138   // unit ID (crystal/fibre/scintillator) and the Time slice ID.
0139 
0140   G4ThreeVector entrancePoint;
0141   G4ThreeVector entranceLocal;
0142   G4ThreeVector posGlobal;
0143   float incidentEnergy;
0144   float edepositEM, edepositHAD;
0145 
0146   CaloHitID currentID[2], previousID[2];
0147 
0148   double energyCut, tmaxHit, eminHit;
0149   std::vector<std::string> hcn_;
0150   std::vector<int> useResMap_;
0151 
0152   CaloG4Hit* currentHit[2];
0153 
0154   bool suppressHeavy;
0155   double kmaxIon, kmaxNeutron, kmaxProton;
0156 
0157   bool forceSave;
0158   int nHC_;
0159   std::string detName_, collName_[2];
0160 
0161 private:
0162   struct Detector {
0163     Detector() {}
0164     std::string name;
0165     G4LogicalVolume* lv;
0166     int level;
0167   };
0168 
0169   const SimTrackManager* m_trackManager;
0170 
0171   std::unique_ptr<CaloSlaveSD> slave[2];
0172   std::unique_ptr<CaloMeanResponse> meanResponse[2];
0173 
0174   CaloG4HitCollection* theHC[2];
0175 
0176   bool ignoreTrackID;
0177   bool isParameterized;
0178   bool ignoreReject;
0179   bool useMap;  // use map for comparison of ID
0180   bool corrTOFBeam;
0181 
0182   int hcID[2];
0183   int primAncestor;
0184   int cleanIndex[2];
0185   int totalHits[2];
0186   int primIDSaved[2];   // ID of the last saved primary
0187   int nCheckedHits[2];  // number of last hits to compare ID
0188 
0189   float timeSlice;
0190   double eminHitD;
0191   double correctT;
0192   bool doFineCalo_;
0193   double eMinFine_;
0194 
0195   std::map<CaloHitID, CaloG4Hit*> hitMap[2];
0196   std::map<int, TrackWithHistory*> tkMap;
0197   std::unordered_map<unsigned int, unsigned int> boundaryCrossingParentMap_;
0198   std::vector<std::unique_ptr<CaloG4Hit>> reusehit[2];
0199   std::vector<Detector> fineDetectors_;
0200   bool doFineCaloThisStep_;
0201 };
0202 
0203 #endif  // SimG4CMS_CaloSD_h