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 Dicom2Run.cc
0028 /// \brief Implementation of the Dicom2Run class
0029 
0030 //=====================================================================
0031 ///
0032 ///  (Description)
0033 ///    Dicom2Run Class is for accumulating scored quantities which is
0034 ///  scored using G4MutiFunctionalDetector and G4VPrimitiveScorer.
0035 ///  Accumulation is done using G4THitsVector object.
0036 ///
0037 ///    The constructor Dicom2Run(const std::vector<G4String> mfdName)
0038 ///  needs a vector filled with MultiFunctionalDetector names which
0039 ///  was assigned at instantiation of MultiFunctionalDetector(MFD).
0040 ///  Then Dicom2Run constructor automatically scans primitive scorers
0041 ///  in the MFD, and obtains collectionIDs of all collections associated
0042 ///  to those primitive scorers. Futhermore, the G4THitsVector 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 Dicom2Run objects as
0047 ///  data members.
0048 ///  std::vector<G4String> fCollName;            // Collection Name,
0049 ///  std::vector<G4int> fCollID;                 // Collection ID,
0050 ///  std::vector<Dicom2RunVector*> fRunMap; // HitsVector for RUN.
0051 ///
0052 ///  The resualtant HitsVector objects are obtain using access method,
0053 ///  GetHitsVector(..).
0054 ///
0055 //=====================================================================
0056 
0057 #include "Dicom2Run.hh"
0058 
0059 #include "DicomDetectorConstruction.hh"
0060 
0061 #include "G4MultiFunctionalDetector.hh"
0062 #include "G4SDManager.hh"
0063 #include "G4VPrimitiveScorer.hh"
0064 
0065 #include <cstdint>
0066 
0067 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0068 //
0069 //  Constructor.
0070 Dicom2Run::Dicom2Run() : DicomRun() {}
0071 
0072 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0073 //
0074 //  Constructor.
0075 //   (The vector of MultiFunctionalDetector name has to given.)
0076 Dicom2Run::Dicom2Run(const std::vector<G4String> mfdName) : DicomRun()
0077 {
0078   ConstructMFD(mfdName);
0079 }
0080 
0081 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0082 //
0083 // Destructor
0084 //    clear all data members.
0085 Dicom2Run::~Dicom2Run()
0086 {
0087   //--- Clear HitsVector for RUN
0088   for (std::size_t i = 0; i < fRunMap.size(); ++i) {
0089     if (fRunMap[i]) fRunMap[i]->clear();
0090   }
0091   fCollName.clear();
0092   fCollID.clear();
0093   fRunMap.clear();
0094 }
0095 
0096 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0097 //
0098 // Destructor
0099 //    clear all data members.
0100 void Dicom2Run::ConstructMFD(const std::vector<G4String>& mfdName)
0101 {
0102   DicomRun::ConstructMFD(mfdName);
0103 
0104   G4SDManager* SDman = G4SDManager::GetSDMpointer();
0105   //=================================================
0106   //  Initalize RunMaps for accumulation.
0107   //  Get CollectionIDs for HitCollections.
0108   //=================================================
0109   for (std::size_t idet = 0; idet < mfdName.size(); ++idet) {
0110     // Loop for all MFD.
0111     G4String detName = mfdName[idet];
0112     //--- Seek and Obtain MFD objects from SDmanager.
0113     G4MultiFunctionalDetector* mfd =
0114       (G4MultiFunctionalDetector*)(SDman->FindSensitiveDetector(detName));
0115     //
0116     if (mfd) {
0117       //--- Loop over the registered primitive scorers.
0118       for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); ++icol) {
0119         // Get Primitive Scorer object.
0120         G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
0121         // collection name and collectionID for HitsCollection,
0122         // where type of HitsCollection is G4THitsVector in case
0123         // of primitive scorer.
0124         // The collection name is given by :
0125         //  <MFD name>/<Primitive Scorer name>.
0126         G4String collectionName = scorer->GetName();
0127         G4String fullCollectionName = detName + "/" + collectionName;
0128         G4int collectionID = SDman->GetCollectionID(fullCollectionName);
0129         //
0130         if (collectionID >= 0) {
0131           G4cout << "++ " << fullCollectionName << " id " << collectionID << G4endl;
0132           // Store obtained HitsCollection information into data
0133           // members. qnd creates new G4THitsVector for accumulating
0134           // quantities during RUN.
0135           fCollName.push_back(fullCollectionName);
0136           fCollID.push_back(collectionID);
0137           fRunMap.push_back(new Dicom2RunVector(detName, collectionName));
0138         }
0139         else {
0140           G4cout << "** collection " << fullCollectionName << " not found. " << G4endl;
0141         }
0142       }
0143     }
0144   }
0145 }
0146 
0147 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0148 //
0149 //  RecordEvent is called at end of event.
0150 //  For scoring purpose, the resultant quantity in a event,
0151 //  is accumulated during a Run.
0152 void Dicom2Run::RecordEvent(const G4Event* aEvent)
0153 {
0154   DicomRun::RecordEvent(aEvent);
0155 
0156   // G4cout << "Dicom Run :: Recording event " << aEvent->GetEventID()
0157   //<< "..." << G4endl;
0158   //=============================
0159   //  HitsCollection of This Event
0160   //============================
0161   G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
0162   if (!HCE) return;
0163 
0164   //=======================================================
0165   // Sum up HitsVector of this Event  into HitsVector of this RUN
0166   //=======================================================
0167   for (std::size_t i = 0; i < fCollID.size(); ++i) {
0168     // Loop over HitsCollection
0169     G4THitsMap<G4double>* EvtMap = nullptr;
0170     if (fCollID[i] >= 0) {
0171       // Collection is attached to HCE
0172       EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID[i]));
0173     }
0174     else {
0175       G4cout << " Error EvtMap Not Found " << i << G4endl;
0176     }
0177 
0178     // if valid pointer, add the pointer
0179     if (EvtMap) {
0180       // for(auto itr = EvtMap->begin(); itr != EvtMap->end(); ++itr)
0181       //     G4cout << "adding " << *EvtMap->GetObject(itr) << G4endl;
0182       //=== Sum up HitsVector of this event to HitsVector of RUN.===
0183       *fRunMap[i] += *EvtMap;
0184     }
0185   }
0186 }
0187 
0188 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0189 // Merge hits map from threads
0190 void Dicom2Run::Merge(const G4Run* aRun)
0191 {
0192   DicomRun::Merge(aRun);
0193 
0194   const Dicom2Run* localRun = static_cast<const Dicom2Run*>(aRun);
0195 
0196   Copy(fCollName, localRun->fCollName);
0197   Copy(fCollID, localRun->fCollID);
0198   std::size_t ncopies = Copy(fRunMap, localRun->fRunMap);
0199   // copy function returns the fRunMap size if all data is copied
0200   // so this loop isn't executed the first time around
0201   for (std::size_t i = ncopies; i < fRunMap.size(); ++i)
0202     *fRunMap[i] += *localRun->fRunMap[i];
0203 }
0204 
0205 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0206 //=================================================================
0207 //  Access method for HitsVector of the RUN
0208 //
0209 //-----
0210 // Access HitsVector.
0211 //  By  MultiFunctionalDetector name and Collection Name.
0212 Dicom2Run::Dicom2RunVector* Dicom2Run::GetHitsVector(const G4String& detName,
0213                                                      const G4String& colName) const
0214 {
0215   G4String fullName = detName + "/" + colName;
0216   return GetHitsVector(fullName);
0217 }
0218 
0219 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0220 // Access HitsVector.
0221 //  By full description of collection name, that is
0222 //    <MultiFunctional Detector Name>/<Primitive Scorer Name>
0223 Dicom2Run::Dicom2RunVector* Dicom2Run::GetHitsVector(const G4String& fullName) const
0224 {
0225   std::size_t Ncol = fCollName.size();
0226   for (std::size_t i = 0; i < Ncol; ++i) {
0227     if (fCollName[i] == fullName) {
0228       return fRunMap[i];
0229     }
0230   }
0231 
0232   G4Exception("Dicom2Run", fullName.c_str(), JustWarning,
0233               "GetHitsVector failed to locate the requested HitsVector");
0234   return nullptr;
0235 }