#include "Run.hh" #include "G4SDManager.hh" #include "G4MultiFunctionalDetector.hh" #include "G4VPrimitiveScorer.hh" #include "G4VTrajectory.hh" #include "G4VTrajectoryPoint.hh" #include // Constructor. Run::Run() : G4Run() { } // Constructor Run::Run(const std::vector mfdName): G4Run() { G4SDManager* SDman = G4SDManager::GetSDMpointer(); //================================================= // Initalize RunMaps for accumulation. // Get CollectionIDs for HitCollections. //================================================= size_t Nmfd = mfdName.size(); for (size_t idet = 0; idet < Nmfd; ++idet) // Loop for all MFD. { G4String detName = mfdName[idet]; //--- Seek and Obtain MFD objects from SDmanager. G4MultiFunctionalDetector* mfd = (G4MultiFunctionalDetector*)(SDman->FindSensitiveDetector(detName)); // if (mfd) { //--- Loop over the registered primitive scorers. for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); ++icol) { // Get Primitive Scorer object. G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol); // collection name and collectionID for HitsCollection, // where type of HitsCollection is G4THitsMap in case // of primitive scorer. // The collection name is given by /. G4String collectionName = scorer->GetName(); G4String fullCollectionName = detName + "/" + collectionName; G4int collectionID = SDman->GetCollectionID(fullCollectionName); G4cout << "Collection ID" << collectionID << G4endl; // if (collectionID >= 0) { // Store obtained HitsCollection information into data members. // And, creates new G4THitsMap for accumulating quantities during RUN. fCollName.push_back(fullCollectionName); fCollID.push_back(collectionID); fRunMap.push_back(new G4THitsMap(detName, collectionName)); } else { G4cout << "** collection " << fullCollectionName << " not found. " << G4endl; } } } } } // Destructor Run::~Run() { //--- Clear HitsMap for RUN G4int nMap = fRunMap.size(); for (G4int i = 0; i < nMap; i++) { if (fRunMap[i]) fRunMap[i]->clear(); } fCollName.clear(); fCollID.clear(); fRunMap.clear(); } // RecordEvent is called at end of event. // For scoring purpose, the resultant quantity in a event, // is accumulated during a Run. void Run::RecordEvent(const G4Event* aEvent) { //============================= // HitsCollection of This Event //============================ G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent(); if (!HCE) return; //======================================================= // Sum up HitsMap of this Event into HitsMap of this RUN //======================================================= size_t Ncol = fCollID.size(); for ( size_t i = 0; i < Ncol ; ++i ) // Loop over HitsCollection { G4THitsMap* EvtMap = nullptr; if ( fCollID[i] >= 0 ) // Collection is attached to HCE { EvtMap = static_cast*>(HCE->GetHC(fCollID[i])); } else {G4cout <<" Error EvtMap Not Found "<< i << G4endl;} if ( EvtMap ){*fRunMap[i] += *EvtMap; } } G4Run::RecordEvent(aEvent); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... // Merge hits map from threads void Run::Merge(const G4Run* aRun) { const Run* localRun = static_cast(aRun); Copy(fCollName, localRun->fCollName); Copy(fCollID, localRun->fCollID); size_t ncopies = Copy(fRunMap, localRun->fRunMap); // copy function returns the fRunMap size if all data is copied // so this loop isn't executed the first time around for (size_t i = ncopies; i < fRunMap.size(); ++i) { *fRunMap[i] += *localRun->fRunMap[i]; } G4Run::Merge(aRun); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... //================================================================= // Access method for HitsMap of the RUN // //----- // Access HitsMap. // By MultiFunctionalDetector name and Collection Name. G4THitsMap* Run::GetHitsMap(const G4String& detName, const G4String& colName) const { G4String fullName = detName+"/"+colName; return GetHitsMap(fullName); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... // Access HitsMap. // By full description of collection name, that is // / G4THitsMap* Run::GetHitsMap(const G4String& fullName) const { size_t Ncol = fCollName.size(); for ( size_t i = 0; i < Ncol; ++i) { if ( fCollName[i] == fullName ) { return fRunMap[i]; //if(hitsmap) { *hitsmap += *fRunMap[i]; } //if(!hitsmap) { hitsmap = fRunMap[i]; } } } G4Exception("DicomRun", fullName.c_str(), JustWarning, "GetHitsMap failed to locate the requested HitsMap"); return nullptr; }