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