Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // ******************************************************************** 27 // 28 // CaTS (Calorimetry and Tracking Simulation) 29 // 30 // Authors : Hans Wenzel 31 // Soon Yung Jun 32 // (Fermi National Accelerator Laboratory) 33 // 34 // History 35 // October 18th, 2021 : first implementation 36 // 37 // ******************************************************************** 38 // 39 /// \file EventAction.cc 40 /// \brief Implementation of the CaTS::EventAction class 41 42 // Geant4 headers 43 #include <G4UserEventAction.hh> 44 #include <G4ios.hh> 45 #include "G4Event.hh" 46 #include "G4HCofThisEvent.hh" 47 #include <G4String.hh> 48 #include <G4Types.hh> 49 #include <G4VHit.hh> 50 #include <G4VHitsCollection.hh> 51 #include "G4SDManager.hh" 52 53 // project headers: 54 #include "EventAction.hh" 55 #include "ConfigurationManager.hh" 56 #include "Event.hh" 57 #include "PhotonSD.hh" 58 #include "PhotonHit.hh" 59 #include "InteractionHit.hh" 60 #include "lArTPCHit.hh" 61 #include "TrackerHit.hh" 62 #include "MscHit.hh" 63 #include "CalorimeterHit.hh" 64 #include "DRCalorimeterHit.hh" 65 #ifdef WITH_ROOT 66 #include "RootIO.hh" 67 #endif 68 // stl headers 69 #include <map> 70 #include <utility> 71 #include <algorithm> 72 #include <istream> 73 #ifdef WITH_G4OPTICKS 74 #include "" 75 #include "" 76 #include "" 77 #endif 78 79 #ifdef WITH_ROOT 80 namespace { 81 // Mutex to lock updating the global ion map 82 G4Mutex ionIdMapMutex = G4MUTEX_INITIALIZER; 83 } // namespace 84 #endif 85 86 EventAction::EventAction() 87 : G4UserEventAction() { 88 #ifdef WITH_ROOT 89 RootIO::GetInstance(); 90 #endif 91 } 92 93 void EventAction::BeginOfEventAction(const G4Event*) { 94 } 95 96 void EventAction::EndOfEventAction(const G4Event* event) { 97 G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose(); 98 if (verbose) 99 G4cout << "EventAction::EndOfEventAction Event: " << event->GetEventID() 100 << G4endl; 101 G4HCofThisEvent* HCE = event->GetHCofThisEvent(); 102 if (HCE == nullptr) 103 return; 104 #ifdef WITH_ROOT 105 Event* CaTSEvt = new Event(); 106 CaTSEvt->SetEventNr(event->GetEventID()); 107 std::map<G4String, std::vector < G4VHit*>>*hcmap = CaTSEvt->GetHCMap(); 108 #endif // end WITH_ROOT 109 #ifdef WITH_G4OPTICKS 110 if (ConfigurationManager::getInstance()->isEnable_opticks()) { 111 G4Opticks* g4ok = G4Opticks::Get(); 112 G4int eventid = event->GetEventID(); 113 g4ok->propagateOpticalPhotons(eventid); 114 unsigned num_hits = g4ok->getNumHit(); 115 G4cout << "EndOfEventAction: num_hits: " << num_hits << G4endl; 116 if (num_hits > 0) { 117 G4HCtable* hctable = G4SDManager::GetSDMpointer()->GetHCtable(); 118 for (G4int i = 0; i < hctable->entries(); ++i) { 119 std::string sdn = hctable->GetSDname(i); 120 std::size_t found = sdn.find("Photondetector"); 121 if (found != std::string::npos) { 122 PhotonSD* aSD = 123 (PhotonSD*) G4SDManager::GetSDMpointer()->FindSensitiveDetector( 124 sdn); 125 aSD->AddOpticksHits(); 126 } 127 } 128 } 129 if (verbose) { 130 G4cout << "***********************************************************" 131 "********************************************************" 132 << G4endl; 133 G4cout << " EndOfEventAction: numphotons: " << g4ok->getNumPhotons() 134 << " Gensteps: " << g4ok->getNumGensteps() 135 << " Maxgensteps: " << g4ok->getMaxGensteps() << G4endl; 136 G4cout << " EndOfEventAction: num_hits: " << g4ok->getNumHit() << G4endl; 137 G4cout << g4ok->dbgdesc() << G4endl; 138 } 139 g4ok->reset(); 140 if (verbose) { 141 G4cout << "========================== After reset: " << G4endl; 142 G4cout << " EndOfEventAction: numphotons: " << g4ok->getNumPhotons() 143 << " Gensteps: " << g4ok->getNumGensteps() 144 << " Maxgensteps: " << g4ok->getMaxGensteps() << G4endl; 145 G4cout << "EndOfEventAction: num_hits: " << g4ok->getNumHit() << G4endl; 146 G4cout << g4ok->dbgdesc() << G4endl; 147 G4cout << "***********************************************************" 148 "********************************************************" 149 << G4endl; 150 } 151 } // end isEnable_opticks 152 #endif // end WITH_G4OPTICKS 153 // 154 // Now we deal with the Geant4 Hit collections. 155 // 156 if (verbose) 157 G4cout << "Number of collections: " << HCE->GetNumberOfCollections() 158 << G4endl; 159 #ifdef WITH_ROOT 160 if (ConfigurationManager::getInstance()->isWriteHits()) { 161 for (int i = 0; i < HCE->GetNumberOfCollections(); i++) { 162 G4VHitsCollection* hc = HCE->GetHC(i); 163 G4String hcname = hc->GetName(); 164 std::vector<std::string> y = split(hcname, '_'); 165 std::string Classname = y[1]; 166 if (verbose) 167 G4cout << "Classname: " << Classname << G4endl; 168 if (Classname == "lArTPC") { 169 std::vector<G4VHit*> hitsVector; 170 G4int NbHits = hc->GetSize(); 171 for (G4int ii = 0; ii < NbHits; ii++) { 172 G4VHit* hit = hc->GetHit(ii); 173 lArTPCHit* tpcHit = dynamic_cast<lArTPCHit*> (hit); 174 hitsVector.push_back(tpcHit); 175 } 176 hcmap->insert(std::make_pair(hcname, hitsVector)); 177 } else if (Classname == "Photondetector") { 178 std::vector<G4VHit*> hitsVector; 179 G4int NbHits = hc->GetSize(); 180 if (verbose) 181 G4cout << "Photondetector size: " << hc->GetSize() << G4endl; 182 for (G4int ii = 0; ii < NbHits; ii++) { 183 G4VHit* hit = hc->GetHit(ii); 184 PhotonHit* pHit = dynamic_cast<PhotonHit*> (hit); 185 hitsVector.push_back(pHit); 186 } 187 hcmap->insert(std::make_pair(hcname, hitsVector)); 188 } else if (Classname == "Target") { 189 std::vector<G4VHit*> hitsVector; 190 G4int NbHits = hc->GetSize(); 191 if (verbose) 192 G4cout << "Interaction size: " << hc->GetSize() << G4endl; 193 for (G4int ii = 0; ii < NbHits; ii++) { 194 G4VHit* hit = hc->GetHit(ii); 195 InteractionHit* iaHit = dynamic_cast<InteractionHit*> (hit); 196 hitsVector.push_back(iaHit); 197 } 198 hcmap->insert(std::make_pair(hcname, hitsVector)); 199 } else if (Classname == "Tracker") { 200 std::vector<G4VHit*> hitsVector; 201 G4int NbHits = hc->GetSize(); 202 if (verbose) 203 G4cout << "Tracker size: " << hc->GetSize() << G4endl; 204 for (G4int ii = 0; ii < NbHits; ii++) { 205 G4VHit* hit = hc->GetHit(ii); 206 TrackerHit* tHit = dynamic_cast<TrackerHit*> (hit); 207 hitsVector.push_back(tHit); 208 } 209 hcmap->insert(std::make_pair(hcname, hitsVector)); 210 } else if (Classname == "Msc") { 211 std::vector<G4VHit*> hitsVector; 212 G4int NbHits = hc->GetSize(); 213 if (verbose) 214 G4cout << "Msc size: " << hc->GetSize() << G4endl; 215 for (G4int ii = 0; ii < NbHits; ii++) { 216 G4VHit* hit = hc->GetHit(ii); 217 MscHit* mscHit = dynamic_cast<MscHit*> (hit); 218 hitsVector.push_back(mscHit); 219 } 220 hcmap->insert(std::make_pair(hcname, hitsVector)); 221 } else if (Classname == "Calorimeter") { 222 std::vector<G4VHit*> hitsVector; 223 G4int NbHits = hc->GetSize(); 224 if (verbose) 225 G4cout << "Calorimeter size: " << hc->GetSize() << G4endl; 226 for (G4int ii = 0; ii < NbHits; ii++) { 227 G4VHit* hit = hc->GetHit(ii); 228 CalorimeterHit* cHit = dynamic_cast<CalorimeterHit*> (hit); 229 hitsVector.push_back(cHit); 230 } 231 hcmap->insert(std::make_pair(hcname, hitsVector)); 232 } else if (Classname == "DRCalorimeter") { 233 std::vector<G4VHit*> hitsVector; 234 G4int NbHits = hc->GetSize(); 235 if (verbose) 236 G4cout << "DRCalorimeter size: " << hc->GetSize() << G4endl; 237 for (G4int ii = 0; ii < NbHits; ii++) { 238 G4VHit* hit = hc->GetHit(ii); 239 DRCalorimeterHit* drHit = dynamic_cast<DRCalorimeterHit*> (hit); 240 hitsVector.push_back(drHit); 241 } 242 hcmap->insert(std::make_pair(hcname, hitsVector)); 243 } else { 244 G4cout << "SD type: " << Classname << " unknown" << G4endl; 245 } 246 } 247 G4AutoLock lock(&ionIdMapMutex); 248 RootIO::GetInstance()->Write(CaTSEvt); 249 CaTSEvt->Reset(); 250 delete CaTSEvt; 251 } // end enableio 252 #endif 253 } 254 255 std::vector<std::string>& EventAction::split(const std::string& s, char delim, 256 std::vector<std::string>& elems) { 257 std::stringstream ss(s); 258 std::string item; 259 while (std::getline(ss, item, delim)) { 260 elems.push_back(item); 261 } 262 return elems; 263 } 264 265 std::vector<std::string> EventAction::split(const std::string& s, char delim) { 266 std::vector<std::string> elems; 267 return split(s, delim, elems); 268 } 269