Geant4_10
G4MoleculeCounter.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: G4MoleculeCounter.cc 74551 2013-10-14 12:59:14Z gcosmo $
27 //
28 #include "G4MoleculeCounter.hh"
29 #include "G4UIcommand.hh"
30 #include "G4UnitsTable.hh"
31 #include <iomanip>
32 
33 using namespace std;
34 
37 
39 {
40  fVerbose = 0 ;
41 }
42 
44 
46 {
47  if(!fpInstance)
48  fpInstance = new G4MoleculeCounter();
49 
50  return fpInstance;
51 }
52 
54 {
55  if(fpInstance)
56  {
57  delete fpInstance;
58  fpInstance = 0;
59  }
60 }
61 
62 void G4MoleculeCounter::SetTimeSlice(double timeSlice)
63 {
65 }
66 
68 {
69  if(fDontRegister[molecule.GetDefinition()]) return ;
70 
71  if(fVerbose)
72  {
73  G4cout<<"G4MoleculeCounter::AddAMoleculeAtTime : "<< molecule.GetName()
74  << " at time : " << G4BestUnit(time, "Time") <<G4endl;
75  }
76 
77  CounterMapType::iterator counterMap_i = fCounterMap.find(molecule) ;
78 
79  if(counterMap_i == fCounterMap.end())
80  {
81  // DEBUG
82 // if(fVerbose) G4cout << " !! ***** Map is empty " << G4endl;
83  fCounterMap[molecule][time] = 1;
84  }
85  else if(counterMap_i->second.empty())
86  {
87  // DEBUG
88 // if(fVerbose) G4cout << " !! ***** Map is empty " << G4endl;
89  counterMap_i->second[time] = 1;
90  }
91  else
92  {
93  NbMoleculeAgainstTime::iterator end = counterMap_i->second.end();
94  end--;
95 
96  // DEBUG
97 // if(fVerbose)
98 // G4cout<<"!! End Time = "<< G4BestUnit(end->first, "Time") <<G4endl;
99 
100  if(end->first <= time)
101  {
102  counterMap_i->second[time]=end->second + 1;
103  }
104  else
105  {
106  NbMoleculeAgainstTime::iterator it = counterMap_i->second.lower_bound(time);
107 
108  while(it->first > time && it!=counterMap_i->second.begin())
109  {
110  // DEBUG
111 // if(fVerbose)
112 // G4cout<<"!! ********** Is going back!!!!"<<G4endl;
113  it--;
114  }
115 
116  if(it==counterMap_i->second.begin() && it->first > time)
117  {
118  // DEBUG
119 // if(fVerbose)
120 // G4cout<<"!! ********** Illegal !!!!"<<G4endl;
121  return ;
122  }
123 
124  // DEBUG
125 // if(fVerbose)
126 // {
127 // G4cout<<"!! PREVIOUS NB = "<< it->second <<G4endl;
128 // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time") <<G4endl;
129 // }
130  counterMap_i->second[time]=it->second + 1;
131  }
132  }
133 
134  // DEBUG
135 // if(fVerbose)
136 // G4cout<<"!! NB = "<< fCounterMap[molecule][time]<<G4endl;
137 }
138 
140 {
141  if(fDontRegister[molecule.GetDefinition()]) return ;
142 
143  if(fVerbose)
144  {
145  G4cout<<"G4MoleculeCounter::RemoveAMoleculeAtTime : "<< molecule.GetName()
146  << " at time : " << G4BestUnit(time,"Time") <<G4endl;
147  }
148 
149  NbMoleculeAgainstTime& nbMolPerTime = fCounterMap[molecule];
150 
151  if(nbMolPerTime.empty())
152  {
153  molecule.PrintState();
154  G4String errMsg = "You are trying to remove molecule "
155  + molecule.GetName()
156  +" from the counter while this kind of molecules has not been registered yet";
157  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime","",FatalErrorInArgument, errMsg);
158 
159  return;
160  }
161  else
162  {
163  NbMoleculeAgainstTime::iterator it ;
164 
165  if(nbMolPerTime.size() == 1)
166  {
167  it = nbMolPerTime.begin() ;
168  // DEBUG
169 // if(fVerbose)
170 // G4cout << "!! fCounterMap[molecule].size() == 1" << G4endl;
171  }
172  else
173  {
174  it = nbMolPerTime.lower_bound(time);
175  }
176 
177  if(it==nbMolPerTime.end())
178  {
179  // DEBUG
180 // if(fVerbose)
181 // G4cout << " ********** NO ITERATOR !!!!!!!!! " << G4endl;
182  it--;
183 
184  if(time<it->first)
185  {
186  G4String errMsg = "There was no "+ molecule.GetName()
187  + " record at the time or even before the time asked";
188  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime","",FatalErrorInArgument, errMsg);
189  }
190  }
191 
192  // DEBUG
193 // if(fVerbose)
194 // {
196 // G4cout<<"!! Molecule = " << molecule.GetName() << G4endl;
197 // G4cout<<"!! At Time = "<< G4BestUnit(time,"Time") <<G4endl;
198 // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time")<<G4endl;
199 // G4cout<<"!! PREVIOUS Nb = "<< it->second <<G4endl;
200 // }
201 
202  // If valgrind problem on the line below, it means that the pointer "it"
203  // points nowhere
204  if(nbMolPerTime.value_comp()(*it, *nbMolPerTime.begin()))
205  {
206  // DEBUG
207 // if(fVerbose)
208 // G4cout<<"!! ***** In value_comp ... " << G4endl;
209  it++;
210  if(time<it->first)
211  {
212  G4String errMsg = "There was no "+ molecule.GetName()
213  + " record at the time or even before the time asked";
214  G4Exception("G4MoleculeCounter::RemoveAMoleculeAtTime","",FatalErrorInArgument, errMsg);
215  }
216  }
217 
218  while(it->first - time > compDoubleWithPrecision::fPrecision && it!=nbMolPerTime.begin())
219  {
220  // DEBUG
221 // if(fVerbose)
222 // {
223 // G4cout<<"!! ***** Is going back!!!!"<<G4endl;
224 // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it-> first,"Time") <<G4endl;
225 // }
226  it--;
227  }
228 
229  if(it==nbMolPerTime.begin() && it->first > time)
230  {
231  // DEBUG
232 // if(fVerbose)
233 // G4cout<<"!! ********** Illegal !!!!"<<G4endl;
234  return ;
235  }
236 
237  // DEBUG
238 // if(fVerbose)
239 // {
240 // G4cout<<"!! PREVIOUS NB = "<< (*it).second <<G4endl;
241 // G4cout<<"!! PREVIOUS TIME = "<< G4BestUnit(it->first,"Time")<<G4endl;
242 // }
243  nbMolPerTime[time]=it->second - 1;
244  }
245 
246  // DEBUG
247 // if(fVerbose)
248 // {
249 // G4cout<<"!! NB = "<< nbMolPerTime[time]<<G4endl;
250 // }
251 }
252 
254 {
255  if(fVerbose > 1)
256  {
257  G4cout<<"Entering in G4MoleculeCounter::RecordMolecules"<<G4endl;
258  }
259 
260  CounterMapType::iterator it;
261  RecordedMolecules output (new vector<G4Molecule>) ;
262 
263  for(it = fCounterMap.begin() ; it != fCounterMap.end() ; it++)
264  {
265  output->push_back(it->first);
266  }
267  return output;
268 }
269 
virtual void AddAMoleculeAtTime(const G4Molecule &, G4double)
static G4MoleculeCounter * GetMoleculeCounter()
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
#define G4ThreadLocal
Definition: tls.hh:52
const G4String & GetName() const
Definition: G4Molecule.cc:243
RecordedMolecules GetRecordedMolecules()
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
#define FALSE
Definition: globals.hh:52
const G4MoleculeDefinition * GetDefinition() const
Definition: G4Molecule.cc:379
std::auto_ptr< std::vector< G4Molecule > > RecordedMolecules
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4ThreadLocal double fPrecision
G4int first
void PrintState() const
Definition: G4Molecule.cc:258
static G4ThreadLocal G4MoleculeCounter * fpInstance
std::map< G4double, G4int, compDoubleWithPrecision > NbMoleculeAgainstTime
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
virtual void RemoveAMoleculeAtTime(const G4Molecule &, G4double)
void SetTimeSlice(double)
static void DeleteInstance()