ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Dicom2Run.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Dicom2Run.cc
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 //
29 
30 //=====================================================================
55 //=====================================================================
56 
57 #include "Dicom2Run.hh"
59 
60 #include "G4SDManager.hh"
62 #include "G4VPrimitiveScorer.hh"
63 
64 #include <cstdint>
65 
66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 //
68 // Constructor.
70 : DicomRun()
71 { }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 //
75 // Constructor.
76 // (The vector of MultiFunctionalDetector name has to given.)
77 Dicom2Run::Dicom2Run(const std::vector<G4String> mfdName)
78 : DicomRun()
79 {
80  ConstructMFD(mfdName);
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 //
85 // Destructor
86 // clear all data members.
88 {
89  //--- Clear HitsVector for RUN
90  for(std::size_t i = 0; i < fRunMap.size(); ++i)
91  {
92  if(fRunMap[i])
93  fRunMap[i]->clear();
94  }
95  fCollName.clear();
96  fCollID.clear();
97  fRunMap.clear();
98 }
99 
100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
101 //
102 // Destructor
103 // clear all data members.
104 void Dicom2Run::ConstructMFD(const std::vector<G4String>& mfdName)
105 {
106  DicomRun::ConstructMFD(mfdName);
107 
109  //=================================================
110  // Initalize RunMaps for accumulation.
111  // Get CollectionIDs for HitCollections.
112  //=================================================
113  for(std::size_t idet = 0; idet < mfdName.size(); ++idet)
114  {
115  // Loop for all MFD.
116  G4String detName = mfdName[idet];
117  //--- Seek and Obtain MFD objects from SDmanager.
120  (SDman->FindSensitiveDetector(detName));
121  //
122  if(mfd)
123  {
124  //--- Loop over the registered primitive scorers.
125  for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); ++icol)
126  {
127  // Get Primitive Scorer object.
128  G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
129  // collection name and collectionID for HitsCollection,
130  // where type of HitsCollection is G4THitsVector in case
131  // of primitive scorer.
132  // The collection name is given by :
133  // <MFD name>/<Primitive Scorer name>.
134  G4String collectionName = scorer->GetName();
135  G4String fullCollectionName = detName+"/"+collectionName;
136  G4int collectionID = SDman->GetCollectionID(fullCollectionName);
137  //
138  if(collectionID >= 0)
139  {
140  G4cout << "++ "<<fullCollectionName<< " id " << collectionID
141  << G4endl;
142  // Store obtained HitsCollection information into data
143  // members. qnd creates new G4THitsVector for accumulating
144  // quantities during RUN.
145  fCollName.push_back(fullCollectionName);
146  fCollID.push_back(collectionID);
147  fRunMap.push_back(new Dicom2RunVector(detName,
148  collectionName));
149  }
150  else
151  {
152  G4cout << "** collection " << fullCollectionName << " not found. "
153  <<G4endl;
154  }
155  }
156  }
157  }
158 }
159 
160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
161 //
162 // RecordEvent is called at end of event.
163 // For scoring purpose, the resultant quantity in a event,
164 // is accumulated during a Run.
165 void Dicom2Run::RecordEvent(const G4Event* aEvent)
166 {
167  DicomRun::RecordEvent(aEvent);
168 
169  //G4cout << "Dicom Run :: Recording event " << aEvent->GetEventID()
170  //<< "..." << G4endl;
171  //=============================
172  // HitsCollection of This Event
173  //============================
174  G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
175  if (!HCE)
176  return;
177 
178  //=======================================================
179  // Sum up HitsVector of this Event into HitsVector of this RUN
180  //=======================================================
181  for(std::size_t i = 0; i < fCollID.size(); ++i)
182  {
183  // Loop over HitsCollection
184  G4THitsMap<G4double>* EvtMap = nullptr;
185  if(fCollID[i] >= 0)
186  {
187  // Collection is attached to HCE
188  EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID[i]));
189  }
190  else
191  {
192  G4cout <<" Error EvtMap Not Found "<< i << G4endl;
193  }
194 
195  // if valid pointer, add the pointer
196  if(EvtMap)
197  {
198  //for(auto itr = EvtMap->begin(); itr != EvtMap->end(); ++itr)
199  // G4cout << "adding " << *EvtMap->GetObject(itr) << G4endl;
200  //=== Sum up HitsVector of this event to HitsVector of RUN.===
201  *fRunMap[i] += *EvtMap;
202  }
203  }
204 }
205 
206 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
207 // Merge hits map from threads
208 void Dicom2Run::Merge(const G4Run* aRun)
209 {
210  DicomRun::Merge(aRun);
211 
212  const Dicom2Run* localRun = static_cast<const Dicom2Run*>(aRun);
213 
214  Copy(fCollName, localRun->fCollName);
215  Copy(fCollID, localRun->fCollID);
216  G4int ncopies = G4int(Copy(fRunMap, localRun->fRunMap));
217  // copy function returns the fRunMap size if all data is copied
218  // so this loop isn't executed the first time around
219  for(G4int i = ncopies; i < G4int(fRunMap.size()); ++i)
220  *fRunMap[i] += *localRun->fRunMap[i];
221 }
222 
223 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
224 //=================================================================
225 // Access method for HitsVector of the RUN
226 //
227 //-----
228 // Access HitsVector.
229 // By MultiFunctionalDetector name and Collection Name.
232  const G4String& colName) const
233 {
234  G4String fullName = detName+"/"+colName;
235  return GetHitsVector(fullName);
236 }
237 
238 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
239 // Access HitsVector.
240 // By full description of collection name, that is
241 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
243 Dicom2Run::GetHitsVector(const G4String& fullName) const
244 {
245 
246  std::size_t Ncol = fCollName.size();
247  for(std::size_t i = 0; i < Ncol; ++i)
248  {
249  if(fCollName[i] == fullName)
250  {
251  return fRunMap[i];
252  }
253  }
254 
255  G4Exception("Dicom2Run", fullName.c_str(), JustWarning,
256  "GetHitsVector failed to locate the requested HitsVector");
257  return nullptr;
258 }