Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-09 03:15:25

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   ~CaloSD() override;
0055 
0056   G4bool ProcessHits(G4Step* step, G4TouchableHistory*) override;
0057   bool ProcessHits(G4GFlashSpot* aSpot, G4TouchableHistory*) override;
0058 
0059   uint32_t setDetUnitId(const G4Step* step) override = 0;
0060 
0061   void Initialize(G4HCofThisEvent* HCE) override;
0062   void EndOfEvent(G4HCofThisEvent* eventHC) override;
0063   void clear() override;
0064   void DrawAll() override;
0065   void PrintAll() override;
0066 
0067   void clearHits() override;
0068   void fillHits(edm::PCaloHitContainer&, const std::string&) override;
0069   void reset() override;
0070 
0071   bool isItFineCalo(const G4VTouchable* touch);
0072 
0073 protected:
0074   virtual double getEnergyDeposit(const G4Step* step);
0075   virtual double EnergyCorrected(const G4Step& step, const G4Track*);
0076   virtual bool getFromLibrary(const G4Step* step);
0077 
0078   G4ThreeVector setToLocal(const G4ThreeVector&, const G4VTouchable*) const;
0079   G4ThreeVector setToGlobal(const G4ThreeVector&, const G4VTouchable*) const;
0080 
0081   bool hitExists(const G4Step*);
0082   bool checkHit();
0083   CaloG4Hit* createNewHit(const G4Step*, const G4Track*);
0084   void updateHit(CaloG4Hit*);
0085   void resetForNewPrimary(const G4Step*);
0086   double getAttenuation(const G4Step* aStep, double birk1, double birk2, double birk3) const;
0087 
0088   static std::string printableDecayChain(const std::vector<unsigned int>& decayChain);
0089   std::string shortreprID(const CaloHitID& ID);
0090   std::string shortreprID(const CaloG4Hit* hit);
0091   unsigned int findBoundaryCrossingParent(const G4Track* track, bool markParentAsSaveable = true);
0092 
0093   void update(const BeginOfRun*) override;
0094   void update(const BeginOfEvent*) override;
0095   void update(const BeginOfTrack* trk) override;
0096   void update(const EndOfTrack* trk) override;
0097   void update(const ::EndOfEvent*) override;
0098   virtual void initRun();
0099   virtual void initEvent(const BeginOfEvent*);
0100   virtual void endEvent();
0101   virtual bool filterHit(CaloG4Hit*, double);
0102 
0103   virtual int getTrackID(const G4Track*);
0104   virtual int setTrackID(const G4Step*);
0105   virtual uint16_t getDepth(const G4Step*);
0106   double getResponseWt(const G4Track*);
0107   int getNumberOfHits();
0108   void ignoreRejection() { ignoreReject = true; }
0109 
0110   inline void setParameterized(bool val) { isParameterized = val; }
0111   inline void setUseMap(bool val) { useMap = val; }
0112 
0113   inline void processHit(const G4Step* step) {
0114     // check if it is in the same unit and timeslice as the previous one
0115     if (currentID == previousID) {
0116       updateHit(currentHit);
0117     } else if (!checkHit()) {
0118       currentHit = createNewHit(step, step->GetTrack());
0119     }
0120   }
0121 
0122   inline void setNumberCheckedHits(int val) { nCheckedHits = val; }
0123   void printDetectorLevels(const G4VTouchable*) const;
0124 
0125 private:
0126   void storeHit(CaloG4Hit*);
0127   bool saveHit(CaloG4Hit*);
0128   void cleanHitCollection();
0129 
0130 protected:
0131   // Data relative to primary particle (the one which triggers a shower)
0132   // These data are common to all Hits of a given shower.
0133   // One shower is made of several hits which differ by the
0134   // unit ID (crystal/fibre/scintillator) and the Time slice ID.
0135 
0136   G4ThreeVector entrancePoint;
0137   G4ThreeVector entranceLocal;
0138   G4ThreeVector posGlobal;
0139   float incidentEnergy;
0140   float edepositEM, edepositHAD;
0141 
0142   CaloHitID currentID, previousID;
0143 
0144   double energyCut, tmaxHit, eminHit;
0145 
0146   CaloG4Hit* currentHit;
0147 
0148   bool suppressHeavy;
0149   double kmaxIon, kmaxNeutron, kmaxProton;
0150 
0151   bool forceSave;
0152 
0153 private:
0154   struct Detector {
0155     Detector() {}
0156     std::string name;
0157     G4LogicalVolume* lv;
0158     int level;
0159   };
0160 
0161   const SimTrackManager* m_trackManager;
0162 
0163   std::unique_ptr<CaloSlaveSD> slave;
0164   std::unique_ptr<CaloMeanResponse> meanResponse;
0165 
0166   CaloG4HitCollection* theHC;
0167 
0168   bool ignoreTrackID;
0169   bool isParameterized;
0170   bool ignoreReject;
0171   bool useMap;  // use map for comparison of ID
0172   bool corrTOFBeam;
0173 
0174   int hcID;
0175   int primAncestor;
0176   int cleanIndex;
0177   int totalHits;
0178   int primIDSaved;   // ID of the last saved primary
0179   int nCheckedHits;  // number of last hits to compare ID
0180 
0181   float timeSlice;
0182   double eminHitD;
0183   double correctT;
0184   bool doFineCalo_;
0185   double eMinFine_;
0186 
0187   std::map<CaloHitID, CaloG4Hit*> hitMap;
0188   std::map<int, TrackWithHistory*> tkMap;
0189   std::unordered_map<unsigned int, unsigned int> boundaryCrossingParentMap_;
0190   std::vector<std::unique_ptr<CaloG4Hit>> reusehit;
0191   std::vector<Detector> fineDetectors_;
0192   bool doFineCaloThisStep_;
0193 };
0194 
0195 #endif  // SimG4CMS_CaloSD_h