Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DicomRun.cc
Go to the documentation of this file.
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 // $Id: DicomRun.cc 105190 2017-07-14 12:08:43Z gcosmo $
27 //
30 
31 //=====================================================================
56 //=====================================================================
57 
58 #include "DicomRun.hh"
59 #include "G4SDManager.hh"
60 
62 #include "G4VPrimitiveScorer.hh"
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
65 //
66 // Constructor.
68 : G4Run()
69 { }
70 
71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 //
73 // Constructor.
74 // (The vector of MultiFunctionalDetector name has to given.)
75 DicomRun::DicomRun(const std::vector<G4String> mfdName): G4Run()
76 {
77  ConstructMFD(mfdName);
78 }
79 
80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81 //
82 // Destructor
83 // clear all data members.
85 {
86  //--- Clear HitsMap for RUN
87  G4int Nmap = fRunMap.size();
88  for ( G4int i = 0; i < Nmap; 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 DicomRun::ConstructMFD(const std::vector<G4String>& mfdName)
101 {
102 
104  //=================================================
105  // Initalize RunMaps for accumulation.
106  // Get CollectionIDs for HitCollections.
107  //=================================================
108  G4int Nmfd = mfdName.size();
109  for ( G4int idet = 0; idet < Nmfd ; idet++){ // Loop for all MFD.
110  G4String detName = mfdName[idet];
111  //--- Seek and Obtain MFD objects from SDmanager.
114  //
115  if ( mfd ){
116  //--- Loop over the registered primitive scorers.
117  for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++){
118  // Get Primitive Scorer object.
119  G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
120  // collection name and collectionID for HitsCollection,
121  // where type of HitsCollection is G4THitsMap in case
122  // of primitive scorer.
123  // The collection name is given by <MFD name>/<Primitive
124  // Scorer name>.
125  G4String collectionName = scorer->GetName();
126  G4String fullCollectionName = detName+"/"+collectionName;
127  G4int collectionID = SDman->GetCollectionID(fullCollectionName);
128  //
129  if ( collectionID >= 0 ){
130  G4cout << "++ "<<fullCollectionName<< " id " << collectionID
131  << G4endl;
132  // Store obtained HitsCollection information into data members.
133  // And, creates new G4THitsMap for accumulating quantities during RUN.
134  fCollName.push_back(fullCollectionName);
135  fCollID.push_back(collectionID);
136  fRunMap.push_back(new G4THitsMap<G4double>(detName,collectionName));
137  } else {
138  G4cout << "** collection " << fullCollectionName << " not found. "
139  <<G4endl;
140  }
141  }
142  }
143  }
144 }
145 
146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
147 //
148 // RecordEvent is called at end of event.
149 // For scoring purpose, the resultant quantity in a event,
150 // is accumulated during a Run.
151 void DicomRun::RecordEvent(const G4Event* aEvent)
152 {
153  G4Run::RecordEvent(aEvent);
154 
155  //G4cout << "Dicom Run :: Recording event " << aEvent->GetEventID()
156  //<< "..." << G4endl;
157  //=============================
158  // HitsCollection of This Event
159  //============================
160  G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
161  if (!HCE) return;
162 
163  //=======================================================
164  // Sum up HitsMap of this Event into HitsMap of this RUN
165  //=======================================================
166  G4int Ncol = fCollID.size();
167  for ( G4int i = 0; i < Ncol ; i++ ){ // Loop over HitsCollection
168  G4THitsMap<G4double>* EvtMap = 0;
169  if ( fCollID[i] >= 0 ){ // Collection is attached to HCE
170  EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID[i]));
171  }else{
172  G4cout <<" Error EvtMap Not Found "<< i << G4endl;
173  }
174  if ( EvtMap ) {
175  //=== Sum up HitsMap of this event to HitsMap of RUN.===
176  *fRunMap[i] += *EvtMap;
177  //G4cout << "Summing EvtMap into RunMap at " << i << "..." << G4endl;
178  //======================================================
179  } //else {
180  //G4cout << "Null pointer to EvtMap at " << i << "..." << G4endl;
181  //}
182  }
183 
184 }
185 
186 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
187 // Merge hits map from threads
188 void DicomRun::Merge(const G4Run* aRun)
189 {
190  const DicomRun* localRun = static_cast<const DicomRun*>(aRun);
191  Copy(fCollName, localRun->fCollName);
192  Copy(fCollID, localRun->fCollID);
193  unsigned ncopies = Copy(fRunMap, localRun->fRunMap);
194  // copy function returns the fRunMap size if all data is copied
195  // so this loop isn't executed the first time around
196  G4cout << "DicomRun :: Num copies = " << ncopies << G4endl;
197  for(unsigned i = ncopies; i < fRunMap.size(); ++i) {
198  *fRunMap[i] += *localRun->fRunMap[i];
199  }
200  G4Run::Merge(aRun);
201 }
202 
203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
204 //=================================================================
205 // Access method for HitsMap of the RUN
206 //
207 //-----
208 // Access HitsMap.
209 // By MultiFunctionalDetector name and Collection Name.
211  const G4String& colName) const
212 {
213  G4String fullName = detName+"/"+colName;
214  return GetHitsMap(fullName);
215 }
216 
217 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
218 // Access HitsMap.
219 // By full description of collection name, that is
220 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
222 {
223 
224  //G4THitsMap<G4double>* hitsmap = 0;
225 
226  G4int Ncol = fCollName.size();
227  for ( G4int i = 0; i < Ncol; i++){
228  if ( fCollName[i] == fullName ){
229  return fRunMap[i];
230  //if(hitsmap) { *hitsmap += *fRunMap[i]; }
231  //if(!hitsmap) { hitsmap = fRunMap[i]; }
232  }
233  }
234 
235  //if(hitsmap) { return hitsmap; }
236 
237  G4Exception("DicomRun", fullName.c_str(), JustWarning,
238  "GetHitsMap failed to locate the requested HitsMap");
239  return NULL;
240 }
virtual void Merge(const G4Run *)
Definition: G4Run.cc:54
G4String GetName() const
G4VHitsCollection * GetHC(G4int i)
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:135
virtual void Merge(const G4Run *)
Definition: DicomRun.cc:188
void Copy(std::vector< T > &main, const std::vector< T > &data)
Definition: DicomRun.hh:91
virtual void RecordEvent(const G4Event *)
Definition: DicomRun.cc:151
void ConstructMFD(const std::vector< G4String > &)
Definition: DicomRun.cc:100
virtual ~DicomRun()
Definition: DicomRun.cc:84
int G4int
Definition: G4Types.hh:78
Definition of the DicomRun class.
G4GLOB_DLL std::ostream G4cout
DicomRun()
Definition: DicomRun.cc:67
G4VPrimitiveScorer * GetPrimitive(G4int id) const
Definition: G4Run.hh:46
G4VSensitiveDetector * FindSensitiveDetector(G4String dName, G4bool warning=true)
Definition: G4SDManager.cc:128
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
virtual void RecordEvent(const G4Event *)
Definition: G4Run.cc:51
#define G4endl
Definition: G4ios.hh:61
G4THitsMap< G4double > * GetHitsMap(G4int i) const
Definition: DicomRun.hh:71
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:185