ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScoreSpecies.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ScoreSpecies.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 // This example is provided by the Geant4-DNA collaboration
27 // Any report or published results obtained using the Geant4-DNA software
28 // shall cite the following Geant4-DNA collaboration publication:
29 // Med. Phys. 37 (2010) 4692-4708
30 // J. Comput. Phys. 274 (2014) 841-882
31 // Phys. Med. Biol. 63(10) (2018) 105014-12pp
32 // The Geant4-DNA web site is available at http://geant4-dna.org
33 //
34 // ScoreSpecies.cc
35 //
36 #include "ScoreSpecies.hh"
37 
38 #include "G4UnitsTable.hh"
40 #include <G4MoleculeCounter.hh>
41 #include "G4Event.hh"
42 #include <G4SystemOfUnits.hh>
43 #include <globals.hh>
44 #include <G4EventManager.hh>
45 #include <iomanip>
46 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 : G4VPrimitiveScorer(name,depth),
60  fEdep(0),
61  fHCID(-1),
62  fEvtMap(0)
63 {
64  fNEvent = 0;
65  G4double tMin = 1.0 * CLHEP::picosecond;
66  G4double tMax = 999999 * CLHEP::picosecond;
67  G4double tLogMin = std::log10(tMin);
68  G4double tLogMax = std::log10(tMax);
69  G4int tBins = 50;
70  for ( int i = 0; i <= tBins; i++ )
71  AddTimeToRecord(std::pow(10., tLogMin + i*(tLogMax-tLogMin)/tBins));
72 
73  fEdep = 0;
74 }
75 
76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
77 
79 {;}
80 
81 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
82 
84 {
86 
87  if ( edep == 0. ) return FALSE;
88 
89  edep *= aStep->GetPreStepPoint()->GetWeight();
90  G4int index = GetIndex(aStep);
91  fEvtMap->add(index,edep);
92  fEdep+=edep;
93 
94  return TRUE;
95 }
96 
97 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
98 
100 {
102  GetName());
103 
104  if(fHCID < 0)
105  {
106  fHCID = GetCollectionID(0);
107  }
108 
110 }
111 
112 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
113 
115 {
116  if(G4EventManager::GetEventManager()->GetConstCurrentEvent()->IsAborted())
117  {
118  fEdep = 0.;
120  return;
121  }
122 
124 
125  if(species.get() == 0 || species->size() == 0)
126  {
127  G4cout << "No molecule recorded, energy deposited= "
128  << G4BestUnit(fEdep, "Energy") << G4endl;
129  ++fNEvent;
130  fEdep = 0.;
132  return;
133  }
134 
135  for(auto molecule: *species)
136  {
137  for(auto time_mol: fTimeToRecord)
138  {
139  double n_mol =
141  time_mol);
142 
143  if(n_mol < 0)
144  {
145  G4cerr << "N molecules not valid < 0 " << G4endl;
146  G4Exception("","N<0",FatalException,"");
147  }
148 
149  SpeciesInfo& molInfo = fSpeciesInfoPerTime[time_mol][molecule];
150  molInfo.fNumber += n_mol;
151  double gValue = (n_mol/(fEdep/eV)) * 100.;
152  molInfo.fG += gValue;
153  molInfo.fG2 += gValue*gValue;
154  }
155  }
156  ++fNEvent;
157 
158  fEdep = 0.;
160 }
161 
162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
163 
164 void
166 {
168  dynamic_cast<ScoreSpecies*>(dynamic_cast<G4VPrimitiveScorer*>(workerScorer));
169 
170  if(right == 0)
171  {
172  return;
173  }
174  if(right == this)
175  {
176  return;
177  }
178 
179  SpeciesMap::iterator it_map1 = right->fSpeciesInfoPerTime.begin();
180  SpeciesMap::iterator end_map1 = right->fSpeciesInfoPerTime.end();
181 
182  for(; it_map1 != end_map1; ++it_map1)
183  {
184  InnerSpeciesMap& map2 = it_map1->second;
185  InnerSpeciesMap::iterator it_map2 = map2.begin();
186  InnerSpeciesMap::iterator end_map2 = map2.end();
187 
188  for(; it_map2 != end_map2; ++it_map2)
189  {
190  SpeciesInfo& molInfo =
191  fSpeciesInfoPerTime[it_map1->first][it_map2->first] ;
192  molInfo.fNumber += it_map2->second.fNumber;
193  molInfo.fG += it_map2->second.fG;
194  molInfo.fG2 += it_map2->second.fG2;
195  }
196  }
197 
198  fNEvent += right->fNEvent;
199  right->fNEvent = 0;
200  right->fEdep = 0.;
201 }
202 
203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
204 
205 void ScoreSpecies::clear()
206 {
207  fEvtMap->clear();
208  fNEvent = 0;
209  fEdep = 0;
210  fSpeciesInfoPerTime.clear();
211 }
212 
213 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
214 
216 {;}
217 
218 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
219 
221 {
222  G4cout << " MultiFunctionalDet " << detector->GetName() << G4endl;
223  G4cout << " PrimitiveScorer " << GetName() << G4endl;
224  G4cout << " Number of events " << fNEvent << G4endl;
225  G4cout << " Number of energy deposition recorded "
226  << fEvtMap->entries() << G4endl;
227 
228  for(auto itr : *fEvtMap->GetMap()) {
229  G4cout << " copy no.: " << itr.first
230  << " energy deposit: "
231  << *(itr.second)/GetUnitValue()
232  << " [" << GetUnit()<<"]"
233  << G4endl;
234  }
235 }
236 
237 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
238 
239 void ScoreSpecies::ASCII()
240 {
241  std::ofstream out("Species.txt");
242  if(!out) return;
243 
244  out << "# Time [ps] G-value (/100 eV) RMS Molecule" << G4endl;
245 
246  std::map<G4String, std::map<G4double, std::pair<G4double,G4double>>> mol;
247 
248  for(auto it_map1: fSpeciesInfoPerTime)
249  {
250  InnerSpeciesMap& map2 = it_map1.second;
251  G4double time = it_map1.first/ps;
252  for(auto it_map2: map2)
253  {
254  G4double G = it_map2.second.fG;
255  G4double G2 = it_map2.second.fG2;
256  G4double N = fNEvent;
257  G /= N;
258  G2 = std::sqrt( N/(N-1) * ( G2/N - G*G) );
259  mol[it_map2.first->GetName()][time]=std::make_pair(G,G2);
260  }
261  }
262 
263  for ( auto it1 : mol )
264  for ( auto it2 : it1.second )
265  out << std::setw(12) << it2.first << std::setw(12) << it2.second.first
266  << std::setw(12) << it2.second.second << std::setw(12)
267  << std::setw(12) << it1.first << G4endl;
268 
269  out.close();
270 }
271 
272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
273 
275 {
276  if(G4Threading::IsWorkerThread()) return;
277 
278  //----------------------------------------------------------------------------
279  // Save results in ASCII format
280  ASCII();
281 
282  fNEvent = 0;
283  fSpeciesInfoPerTime.clear();
284 }
285