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 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 /// \file Dicom2Run.cc
 28 /// \brief Implementation of the Dicom2Run class
 29 
 30 //=====================================================================
 31 ///
 32 ///  (Description)
 33 ///    Dicom2Run Class is for accumulating scored quantities which is
 34 ///  scored using G4MutiFunctionalDetector and G4VPrimitiveScorer.
 35 ///  Accumulation is done using G4THitsVector object.
 36 ///
 37 ///    The constructor Dicom2Run(const std::vector<G4String> mfdName)
 38 ///  needs a vector filled with MultiFunctionalDetector names which
 39 ///  was assigned at instantiation of MultiFunctionalDetector(MFD).
 40 ///  Then Dicom2Run constructor automatically scans primitive scorers
 41 ///  in the MFD, and obtains collectionIDs of all collections associated
 42 ///  to those primitive scorers. Futhermore, the G4THitsVector objects
 43 ///  for accumulating during a RUN are automatically created too.
 44 ///  (*) Collection Name is same as primitive scorer name.
 45 ///
 46 ///    The resultant information is kept inside Dicom2Run objects as
 47 ///  data members.
 48 ///  std::vector<G4String> fCollName;            // Collection Name,
 49 ///  std::vector<G4int> fCollID;                 // Collection ID,
 50 ///  std::vector<Dicom2RunVector*> fRunMap; // HitsVector for RUN.
 51 ///
 52 ///  The resualtant HitsVector objects are obtain using access method,
 53 ///  GetHitsVector(..).
 54 ///
 55 //=====================================================================
 56 
 57 #include "Dicom2Run.hh"
 58 
 59 #include "DicomDetectorConstruction.hh"
 60 
 61 #include "G4MultiFunctionalDetector.hh"
 62 #include "G4SDManager.hh"
 63 #include "G4VPrimitiveScorer.hh"
 64 
 65 #include <cstdint>
 66 
 67 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 68 //
 69 //  Constructor.
 70 Dicom2Run::Dicom2Run() : DicomRun() {}
 71 
 72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 73 //
 74 //  Constructor.
 75 //   (The vector of MultiFunctionalDetector name has to given.)
 76 Dicom2Run::Dicom2Run(const std::vector<G4String> mfdName) : DicomRun()
 77 {
 78   ConstructMFD(mfdName);
 79 }
 80 
 81 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 82 //
 83 // Destructor
 84 //    clear all data members.
 85 Dicom2Run::~Dicom2Run()
 86 {
 87   //--- Clear HitsVector for RUN
 88   for (std::size_t i = 0; i < fRunMap.size(); ++i) {
 89     if (fRunMap[i]) fRunMap[i]->clear();
 90   }
 91   fCollName.clear();
 92   fCollID.clear();
 93   fRunMap.clear();
 94 }
 95 
 96 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 97 //
 98 // Destructor
 99 //    clear all data members.
100 void Dicom2Run::ConstructMFD(const std::vector<G4String>& mfdName)
101 {
102   DicomRun::ConstructMFD(mfdName);
103 
104   G4SDManager* SDman = G4SDManager::GetSDMpointer();
105   //=================================================
106   //  Initalize RunMaps for accumulation.
107   //  Get CollectionIDs for HitCollections.
108   //=================================================
109   for (std::size_t idet = 0; idet < mfdName.size(); ++idet) {
110     // Loop for all MFD.
111     G4String detName = mfdName[idet];
112     //--- Seek and Obtain MFD objects from SDmanager.
113     G4MultiFunctionalDetector* mfd =
114       (G4MultiFunctionalDetector*)(SDman->FindSensitiveDetector(detName));
115     //
116     if (mfd) {
117       //--- Loop over the registered primitive scorers.
118       for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); ++icol) {
119         // Get Primitive Scorer object.
120         G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
121         // collection name and collectionID for HitsCollection,
122         // where type of HitsCollection is G4THitsVector in case
123         // of primitive scorer.
124         // The collection name is given by :
125         //  <MFD name>/<Primitive Scorer name>.
126         G4String collectionName = scorer->GetName();
127         G4String fullCollectionName = detName + "/" + collectionName;
128         G4int collectionID = SDman->GetCollectionID(fullCollectionName);
129         //
130         if (collectionID >= 0) {
131           G4cout << "++ " << fullCollectionName << " id " << collectionID << G4endl;
132           // Store obtained HitsCollection information into data
133           // members. qnd creates new G4THitsVector for accumulating
134           // quantities during RUN.
135           fCollName.push_back(fullCollectionName);
136           fCollID.push_back(collectionID);
137           fRunMap.push_back(new Dicom2RunVector(detName, collectionName));
138         }
139         else {
140           G4cout << "** collection " << fullCollectionName << " not found. " << G4endl;
141         }
142       }
143     }
144   }
145 }
146 
147 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
148 //
149 //  RecordEvent is called at end of event.
150 //  For scoring purpose, the resultant quantity in a event,
151 //  is accumulated during a Run.
152 void Dicom2Run::RecordEvent(const G4Event* aEvent)
153 {
154   DicomRun::RecordEvent(aEvent);
155 
156   // G4cout << "Dicom Run :: Recording event " << aEvent->GetEventID()
157   //<< "..." << G4endl;
158   //=============================
159   //  HitsCollection of This Event
160   //============================
161   G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
162   if (!HCE) return;
163 
164   //=======================================================
165   // Sum up HitsVector of this Event  into HitsVector of this RUN
166   //=======================================================
167   for (std::size_t i = 0; i < fCollID.size(); ++i) {
168     // Loop over HitsCollection
169     G4THitsMap<G4double>* EvtMap = nullptr;
170     if (fCollID[i] >= 0) {
171       // Collection is attached to HCE
172       EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID[i]));
173     }
174     else {
175       G4cout << " Error EvtMap Not Found " << i << G4endl;
176     }
177 
178     // if valid pointer, add the pointer
179     if (EvtMap) {
180       // for(auto itr = EvtMap->begin(); itr != EvtMap->end(); ++itr)
181       //     G4cout << "adding " << *EvtMap->GetObject(itr) << G4endl;
182       //=== Sum up HitsVector of this event to HitsVector of RUN.===
183       *fRunMap[i] += *EvtMap;
184     }
185   }
186 }
187 
188 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
189 // Merge hits map from threads
190 void Dicom2Run::Merge(const G4Run* aRun)
191 {
192   DicomRun::Merge(aRun);
193 
194   const Dicom2Run* localRun = static_cast<const Dicom2Run*>(aRun);
195 
196   Copy(fCollName, localRun->fCollName);
197   Copy(fCollID, localRun->fCollID);
198   std::size_t ncopies = Copy(fRunMap, localRun->fRunMap);
199   // copy function returns the fRunMap size if all data is copied
200   // so this loop isn't executed the first time around
201   for (std::size_t i = ncopies; i < fRunMap.size(); ++i)
202     *fRunMap[i] += *localRun->fRunMap[i];
203 }
204 
205 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
206 //=================================================================
207 //  Access method for HitsVector of the RUN
208 //
209 //-----
210 // Access HitsVector.
211 //  By  MultiFunctionalDetector name and Collection Name.
212 Dicom2Run::Dicom2RunVector* Dicom2Run::GetHitsVector(const G4String& detName,
213                                                      const G4String& colName) const
214 {
215   G4String fullName = detName + "/" + colName;
216   return GetHitsVector(fullName);
217 }
218 
219 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
220 // Access HitsVector.
221 //  By full description of collection name, that is
222 //    <MultiFunctional Detector Name>/<Primitive Scorer Name>
223 Dicom2Run::Dicom2RunVector* Dicom2Run::GetHitsVector(const G4String& fullName) const
224 {
225   std::size_t Ncol = fCollName.size();
226   for (std::size_t i = 0; i < Ncol; ++i) {
227     if (fCollName[i] == fullName) {
228       return fRunMap[i];
229     }
230   }
231 
232   G4Exception("Dicom2Run", fullName.c_str(), JustWarning,
233               "GetHitsVector failed to locate the requested HitsVector");
234   return nullptr;
235 }
236