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