Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:46

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 /// \file medical/DICOM/src/DicomRun.cc
0028 /// \brief Implementation of the DicomRun class
0029 
0030 //=====================================================================
0031 ///
0032 ///  (Description)
0033 ///    DicomRun Class is for accumulating scored quantities which is
0034 ///  scored using G4MutiFunctionalDetector and G4VPrimitiveScorer.
0035 ///  Accumulation is done using G4THitsMap object.
0036 ///
0037 ///    The constructor DicomRun(const std::vector<G4String> mfdName)
0038 ///  needs a vector filled with MultiFunctionalDetector names which
0039 ///  was assigned at instantiation of MultiFunctionalDetector(MFD).
0040 ///  Then DicomRun constructor automatically scans primitive scorers
0041 ///  in the MFD, and obtains collectionIDs of all collections associated
0042 ///  to those primitive scorers. Futhermore, the G4THitsMap objects
0043 ///  for accumulating during a RUN are automatically created too.
0044 ///  (*) Collection Name is same as primitive scorer name.
0045 ///
0046 ///    The resultant information is kept inside DicomRun objects as
0047 ///  data members.
0048 ///  std::vector<G4String> fCollName;            // Collection Name,
0049 ///  std::vector<G4int> fCollID;                 // Collection ID,
0050 ///  std::vector<G4THitsMap<G4double>*> fRunMap; // HitsMap for RUN.
0051 ///
0052 ///  The resualtant HitsMap objects are obtain using access method,
0053 ///  GetHitsMap(..).
0054 ///
0055 //=====================================================================
0056 
0057 #include "DicomRun.hh"
0058 
0059 #include "G4MultiFunctionalDetector.hh"
0060 #include "G4SDManager.hh"
0061 #include "G4VPrimitiveScorer.hh"
0062 
0063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0064 //
0065 //  Constructor.
0066 DicomRun::DicomRun() : G4Run() {}
0067 
0068 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0069 //
0070 //  Constructor.
0071 //   (The vector of MultiFunctionalDetector name has to given.)
0072 DicomRun::DicomRun(const std::vector<G4String> mfdName) : G4Run()
0073 {
0074   ConstructMFD(mfdName);
0075 }
0076 
0077 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0078 //
0079 // Destructor
0080 //    clear all data members.
0081 DicomRun::~DicomRun()
0082 {
0083   //--- Clear HitsMap for RUN
0084   size_t Nmap = fRunMap.size();
0085   for (size_t i = 0; i < Nmap; ++i) {
0086     if (fRunMap[i]) fRunMap[i]->clear();
0087   }
0088   fCollName.clear();
0089   fCollID.clear();
0090   fRunMap.clear();
0091 }
0092 
0093 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0094 //
0095 // Destructor
0096 //    clear all data members.
0097 void DicomRun::ConstructMFD(const std::vector<G4String>& mfdName)
0098 {
0099   G4SDManager* SDman = G4SDManager::GetSDMpointer();
0100   //=================================================
0101   //  Initalize RunMaps for accumulation.
0102   //  Get CollectionIDs for HitCollections.
0103   //=================================================
0104   size_t Nmfd = mfdName.size();
0105   for (size_t idet = 0; idet < Nmfd; ++idet)  // Loop for all MFD.
0106   {
0107     G4String detName = mfdName[idet];
0108     //--- Seek and Obtain MFD objects from SDmanager.
0109     G4MultiFunctionalDetector* mfd =
0110       (G4MultiFunctionalDetector*)(SDman->FindSensitiveDetector(detName));
0111     //
0112     if (mfd) {
0113       //--- Loop over the registered primitive scorers.
0114       for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); ++icol) {
0115         // Get Primitive Scorer object.
0116         G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
0117         // collection name and collectionID for HitsCollection,
0118         // where type of HitsCollection is G4THitsMap in case
0119         // of primitive scorer.
0120         // The collection name is given by <MFD name>/<Primitive
0121         // Scorer name>.
0122         G4String collectionName = scorer->GetName();
0123         G4String fullCollectionName = detName + "/" + collectionName;
0124         G4int collectionID = SDman->GetCollectionID(fullCollectionName);
0125         //
0126         if (collectionID >= 0) {
0127           G4cout << "++ " << fullCollectionName << " id " << collectionID << G4endl;
0128           // Store obtained HitsCollection information into data members.
0129           // And, creates new G4THitsMap for accumulating quantities during RUN.
0130           fCollName.push_back(fullCollectionName);
0131           fCollID.push_back(collectionID);
0132           fRunMap.push_back(new G4THitsMap<G4double>(detName, collectionName));
0133         }
0134         else {
0135           G4cout << "** collection " << fullCollectionName << " not found. " << G4endl;
0136         }
0137       }
0138     }
0139   }
0140 }
0141 
0142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0143 //
0144 //  RecordEvent is called at end of event.
0145 //  For scoring purpose, the resultant quantity in a event,
0146 //  is accumulated during a Run.
0147 void DicomRun::RecordEvent(const G4Event* aEvent)
0148 {
0149   // disable below because this is done in G4Run::RecordEvent(aEvent);
0150   // numberOfEvent++;  // This is an original line.
0151 
0152   // G4cout << "Dicom Run :: Recording event " << aEvent->GetEventID()
0153   //<< "..." << G4endl;
0154   //=============================
0155   //  HitsCollection of This Event
0156   //============================
0157   G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
0158   if (!HCE) return;
0159 
0160   //=======================================================
0161   // Sum up HitsMap of this Event  into HitsMap of this RUN
0162   //=======================================================
0163   size_t Ncol = fCollID.size();
0164   for (size_t i = 0; i < Ncol; ++i)  // Loop over HitsCollection
0165   {
0166     G4THitsMap<G4double>* EvtMap = nullptr;
0167     if (fCollID[i] >= 0)  // Collection is attached to HCE
0168     {
0169       EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID[i]));
0170     }
0171     else {
0172       G4cout << " Error EvtMap Not Found " << i << G4endl;
0173     }
0174     if (EvtMap) {
0175       //=== Sum up HitsMap of this event to HitsMap of RUN.===
0176       *fRunMap[i] += *EvtMap;
0177       // G4cout << "Summing EvtMap into RunMap at " << i << "..." << G4endl;
0178       //======================================================
0179     }  // else {
0180     // G4cout << "Null pointer to EvtMap at " << i << "..." << G4endl;
0181     // }
0182   }
0183 
0184   G4Run::RecordEvent(aEvent);
0185 }
0186 
0187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0188 // Merge hits map from threads
0189 void DicomRun::Merge(const G4Run* aRun)
0190 {
0191   const DicomRun* localRun = static_cast<const DicomRun*>(aRun);
0192   Copy(fCollName, localRun->fCollName);
0193   Copy(fCollID, localRun->fCollID);
0194   size_t ncopies = Copy(fRunMap, localRun->fRunMap);
0195   // copy function returns the fRunMap size if all data is copied
0196   // so this loop isn't executed the first time around
0197   for (size_t i = ncopies; i < fRunMap.size(); ++i) {
0198     *fRunMap[i] += *localRun->fRunMap[i];
0199   }
0200   G4Run::Merge(aRun);
0201 }
0202 
0203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0204 //=================================================================
0205 //  Access method for HitsMap of the RUN
0206 //
0207 //-----
0208 // Access HitsMap.
0209 //  By  MultiFunctionalDetector name and Collection Name.
0210 G4THitsMap<G4double>* DicomRun::GetHitsMap(const G4String& detName, const G4String& colName) const
0211 {
0212   G4String fullName = detName + "/" + colName;
0213   return GetHitsMap(fullName);
0214 }
0215 
0216 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0217 // Access HitsMap.
0218 //  By full description of collection name, that is
0219 //    <MultiFunctional Detector Name>/<Primitive Scorer Name>
0220 G4THitsMap<G4double>* DicomRun::GetHitsMap(const G4String& fullName) const
0221 {
0222   // G4THitsMap<G4double>* hitsmap = 0;
0223 
0224   size_t Ncol = fCollName.size();
0225   for (size_t i = 0; i < Ncol; ++i) {
0226     if (fCollName[i] == fullName) {
0227       return fRunMap[i];
0228       // if(hitsmap) { *hitsmap += *fRunMap[i]; }
0229       // if(!hitsmap) { hitsmap = fRunMap[i]; }
0230     }
0231   }
0232 
0233   G4Exception("DicomRun", fullName.c_str(), JustWarning,
0234               "GetHitsMap failed to locate the requested HitsMap");
0235   return nullptr;
0236 }