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 | // File: G4PersistencyManager.cc |
---|
27 | // |
---|
28 | // History: |
---|
29 | // 01.07.17 Youhei Morita Initial creation (with "fadsclass") |
---|
30 | |
---|
31 | #include "G4PersistencyManager.hh" |
---|
32 | |
---|
33 | // Addtional Include: |
---|
34 | #include <iomanip> |
---|
35 | #include "G4PersistencyCenter.hh" |
---|
36 | |
---|
37 | // Implementation of Constructor #1 |
---|
38 | G4PersistencyManager::G4PersistencyManager(G4PersistencyCenter* pc, std::string n) |
---|
39 | : f_pc(pc), nameMgr(n), f_is_initialized(false) |
---|
40 | { |
---|
41 | m_verbose = f_pc->VerboseLevel(); |
---|
42 | |
---|
43 | // G4cout << "G4PersistencyManager is constructed with name \"" << nameMgr |
---|
44 | // << "\", " << this << ", verbose = " << m_verbose << G4endl; |
---|
45 | |
---|
46 | // f_GenCenter = GeneratorCenter::GetGeneratorCenter(); |
---|
47 | // f_MCTman = G4MCTManager::GetPointer(); |
---|
48 | } |
---|
49 | |
---|
50 | // Implementation of Destructor #1 |
---|
51 | G4PersistencyManager::~G4PersistencyManager() |
---|
52 | {} |
---|
53 | |
---|
54 | // Implementation of GetPersistencyManager |
---|
55 | G4PersistencyManager* G4PersistencyManager::GetPersistencyManager() |
---|
56 | { |
---|
57 | return G4PersistencyCenter::GetPersistencyCenter()->CurrentPersistencyManager(); |
---|
58 | } |
---|
59 | |
---|
60 | // Implementation of SetVerboseLevel |
---|
61 | void G4PersistencyManager::SetVerboseLevel(int v) |
---|
62 | { |
---|
63 | m_verbose = v; |
---|
64 | if ( m_verbose > 2 ) { |
---|
65 | G4cout << "G4PersistencyManager[\"" << nameMgr << "\"," << this |
---|
66 | << "]: verbose level is set to " << m_verbose << "." |
---|
67 | << G4endl; |
---|
68 | } |
---|
69 | if ( EventIO() != 0 ) EventIO()->SetVerboseLevel(m_verbose); |
---|
70 | #ifndef WIN32 |
---|
71 | #ifdef G4LIB_USE_HEPMC |
---|
72 | if ( HepMCIO() != 0 ) HepMCIO()->SetVerboseLevel(m_verbose); |
---|
73 | if ( MCTruthIO() != 0 ) MCTruthIO()->SetVerboseLevel(m_verbose); |
---|
74 | #endif |
---|
75 | #endif |
---|
76 | if ( HitIO() != 0 ) HitIO()->SetVerboseLevel(m_verbose); |
---|
77 | if ( DigitIO() != 0 ) DigitIO()->SetVerboseLevel(m_verbose); |
---|
78 | if (TransactionManager() != 0) TransactionManager()->SetVerboseLevel(m_verbose); |
---|
79 | |
---|
80 | size_t i; |
---|
81 | |
---|
82 | G4HCIOcatalog* hcio = G4HCIOcatalog::GetHCIOcatalog(); |
---|
83 | if ( hcio != 0 ) { |
---|
84 | hcio->SetVerboseLevel(m_verbose); |
---|
85 | for ( i = 0; i < hcio->NumberOfHCIOmanager(); i++ ) { |
---|
86 | hcio->GetHCIOmanager(i)->SetVerboseLevel(m_verbose); |
---|
87 | } |
---|
88 | } |
---|
89 | G4DCIOcatalog* dcio = G4DCIOcatalog::GetDCIOcatalog(); |
---|
90 | if ( dcio != 0 ) { |
---|
91 | dcio->SetVerboseLevel(m_verbose); |
---|
92 | for ( i = 0; i < dcio->NumberOfDCIOmanager(); i++ ) { |
---|
93 | dcio->GetDCIOmanager(i)->SetVerboseLevel(m_verbose); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | // Implementation of Store |
---|
99 | G4bool G4PersistencyManager::Store(const G4Event* evt) |
---|
100 | { |
---|
101 | if ( m_verbose > 2 ) { |
---|
102 | G4cout << "G4PersistencyManager::Store() is called for event# " |
---|
103 | << evt->GetEventID() << "." << G4endl; |
---|
104 | } |
---|
105 | |
---|
106 | if ( TransactionManager() == 0 ) return true; |
---|
107 | |
---|
108 | G4bool is_store = f_pc->CurrentStoreMode("HepMC") != kOff || |
---|
109 | f_pc->CurrentStoreMode("MCTruth") != kOff || |
---|
110 | f_pc->CurrentStoreMode("Hits") != kOff || |
---|
111 | f_pc->CurrentStoreMode("Digits") != kOff; |
---|
112 | |
---|
113 | if ( ! is_store ) return true; |
---|
114 | |
---|
115 | // Call package dependent Initialize() |
---|
116 | // |
---|
117 | if ( ! f_is_initialized ) { |
---|
118 | f_is_initialized = true; |
---|
119 | if ( m_verbose > 1 ) { |
---|
120 | G4cout << "G4PersistencyManager:: Initializing Transaction ... " |
---|
121 | << G4endl; |
---|
122 | } |
---|
123 | Initialize(); |
---|
124 | } |
---|
125 | |
---|
126 | G4bool st1 = true, st2 = true; |
---|
127 | |
---|
128 | // Start event IO transaction |
---|
129 | // |
---|
130 | if ( TransactionManager()->StartUpdate() ) { |
---|
131 | if ( m_verbose > 2 ) { |
---|
132 | G4cout << "G4PersistencyManager: Update transaction started for event#" |
---|
133 | << evt->GetEventID() << "." << G4endl; |
---|
134 | } |
---|
135 | } else { |
---|
136 | G4cerr << "TransactionManager::Store(G4Event) - StartUpdate() failed." |
---|
137 | << G4endl; |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | std::string file; |
---|
142 | std::string obj; |
---|
143 | |
---|
144 | #ifndef WIN32 |
---|
145 | #ifdef G4LIB_USE_HEPMC |
---|
146 | |
---|
147 | G4bool sthep = true, stmct = true, st3 = true; |
---|
148 | |
---|
149 | // Store HepMC event |
---|
150 | // |
---|
151 | obj = "HepMC"; |
---|
152 | HepMC::GenEvent* hepevt = 0; |
---|
153 | if ( f_pc->CurrentStoreMode(obj) == kOn ) { |
---|
154 | |
---|
155 | // Note: This part of code will not be activated until a method |
---|
156 | // to obtain the current pointer of HepMC::GenEvent* become available. |
---|
157 | |
---|
158 | // if ( (hepevt = f_GenCenter->GetGenEvent()) !=0 ) { |
---|
159 | if ( hepevt !=0 ) { |
---|
160 | |
---|
161 | file = f_pc->CurrentWriteFile(obj); |
---|
162 | if ( TransactionManager()->SelectWriteFile(obj, file) ) { |
---|
163 | sthep = HepMCIO()->Store(hepevt); |
---|
164 | if ( sthep && m_verbose > 1 ) { |
---|
165 | G4cout << " -- File : " << file << " -- Event# " |
---|
166 | << evt->GetEventID() << " -- HepMC Stored." << G4endl; |
---|
167 | } |
---|
168 | } else { |
---|
169 | sthep = false; |
---|
170 | } |
---|
171 | } // end of if ( hepevt != 0 ) |
---|
172 | } else { // recycle or off |
---|
173 | // hepevt= f_GenCenter-> GetGenEvent(); |
---|
174 | } |
---|
175 | |
---|
176 | // Store MCTruth event |
---|
177 | // |
---|
178 | obj = "MCTruth"; |
---|
179 | G4MCTEvent* mctevt = 0; |
---|
180 | if ( f_pc->CurrentStoreMode(obj) == kOn ) { |
---|
181 | |
---|
182 | // Note: This part of code will not be activated until a method |
---|
183 | // to obtain the current pointer of G4MCTEvent* become available. |
---|
184 | |
---|
185 | // if ( (mctevt = f_MCTman->GetCurrentEvent()) != 0 ) { |
---|
186 | if ( mctevt != 0 ) { |
---|
187 | file = f_pc->CurrentWriteFile(obj); |
---|
188 | if ( TransactionManager()->SelectWriteFile(obj, file) ) { |
---|
189 | stmct = MCTruthIO()->Store(mctevt); |
---|
190 | if ( stmct && m_verbose > 1 ) { |
---|
191 | G4cout << " -- File : " << file << " -- Event# " |
---|
192 | << evt->GetEventID() << " -- G4MCTEvent Stored." << G4endl; |
---|
193 | } |
---|
194 | } else { |
---|
195 | stmct = false; |
---|
196 | } |
---|
197 | } // end of if ( mctevt != 0 ) |
---|
198 | } |
---|
199 | |
---|
200 | #endif |
---|
201 | #endif |
---|
202 | |
---|
203 | // Store hits collection |
---|
204 | // |
---|
205 | obj = "Hits"; |
---|
206 | if ( f_pc->CurrentStoreMode(obj) == kOn ) { |
---|
207 | if ( G4HCofThisEvent* hc = evt->GetHCofThisEvent() ) { |
---|
208 | file = f_pc->CurrentWriteFile(obj); |
---|
209 | if ( TransactionManager()->SelectWriteFile(obj, file) ) { |
---|
210 | st1 = HitIO()->Store(hc); |
---|
211 | if ( st1 && m_verbose > 1 ) { |
---|
212 | G4cout << " -- File : " << file << " -- Event# " |
---|
213 | << evt->GetEventID() |
---|
214 | << " -- Hit Collections Stored." << G4endl; |
---|
215 | } |
---|
216 | } else { |
---|
217 | st1 = false; |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | // Store digits collection |
---|
223 | // |
---|
224 | obj = "Digits"; |
---|
225 | if ( f_pc->CurrentStoreMode(obj) == kOn ) { |
---|
226 | if ( G4DCofThisEvent* dc = evt->GetDCofThisEvent() ) { |
---|
227 | file = f_pc->CurrentWriteFile(obj); |
---|
228 | if ( TransactionManager()->SelectWriteFile(obj, file) ) { |
---|
229 | st2 = DigitIO()->Store(dc); |
---|
230 | if ( st2 && m_verbose > 1 ) { |
---|
231 | G4cout << " -- File : " << file << " -- Event# " |
---|
232 | << evt->GetEventID() |
---|
233 | << " -- Digit Collections Stored." << G4endl; |
---|
234 | } |
---|
235 | } else { |
---|
236 | st2 = false; |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | #ifndef WIN32 |
---|
242 | #ifdef G4LIB_USE_HEPMC |
---|
243 | |
---|
244 | // Store this G4EVENT |
---|
245 | // |
---|
246 | if ( hepevt!=0 || mctevt!=0 || evt!=0 ) { |
---|
247 | obj = "Hits"; |
---|
248 | file = f_pc->CurrentWriteFile(obj); |
---|
249 | if ( TransactionManager()->SelectWriteFile(obj, file) ) { |
---|
250 | // st3 = EventIO()->Store(hepevt, mctevt, evt); |
---|
251 | st3 = EventIO()->Store(hepevt, evt); |
---|
252 | if ( st3 && m_verbose > 1 ) { |
---|
253 | G4cout << " -- File name: " << f_pc->CurrentWriteFile("Hits") |
---|
254 | << " -- Event# " << evt->GetEventID() |
---|
255 | << " -- G4Pevent is Stored." << G4endl; |
---|
256 | } |
---|
257 | } else { |
---|
258 | st3 = false; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | G4bool st = sthep && stmct && st1 && st2 && st3; |
---|
263 | |
---|
264 | #else |
---|
265 | |
---|
266 | G4bool st = st1 && st2; |
---|
267 | |
---|
268 | #endif |
---|
269 | |
---|
270 | #else |
---|
271 | |
---|
272 | G4bool st = st1 && st2; |
---|
273 | |
---|
274 | #endif |
---|
275 | |
---|
276 | if ( st ) { |
---|
277 | TransactionManager()->Commit(); |
---|
278 | if ( m_verbose > 0 ) |
---|
279 | G4cout << "G4PersistencyManager: event# " |
---|
280 | << evt->GetEventID() << " is stored." << G4endl; |
---|
281 | } else { |
---|
282 | G4cerr << "G4PersistencyManager::Store(G4Event) - Transaction aborted." |
---|
283 | << G4endl; |
---|
284 | TransactionManager()->Abort(); |
---|
285 | } |
---|
286 | |
---|
287 | return st; |
---|
288 | } |
---|
289 | |
---|
290 | // Implementation of Retrieve |
---|
291 | G4bool G4PersistencyManager::Retrieve(G4Event*& evt) |
---|
292 | { |
---|
293 | if ( m_verbose > 2 ) { |
---|
294 | G4cout << "G4PersistencyManager::Retrieve(G4Event*&) is called." |
---|
295 | << G4endl; |
---|
296 | } |
---|
297 | |
---|
298 | if ( TransactionManager() == 0 ) return true; |
---|
299 | |
---|
300 | if ( f_pc->CurrentRetrieveMode("HepMC") == false && |
---|
301 | f_pc->CurrentRetrieveMode("MCTruth") == false && |
---|
302 | f_pc->CurrentRetrieveMode("Hits") == false && |
---|
303 | f_pc->CurrentRetrieveMode("Digits") == false ) { |
---|
304 | return true; |
---|
305 | } |
---|
306 | |
---|
307 | // Call package dependent Initialize() |
---|
308 | // |
---|
309 | if ( ! f_is_initialized ) { |
---|
310 | f_is_initialized = true; |
---|
311 | if ( m_verbose > 1 ) { |
---|
312 | G4cout << "G4PersistencyManager:: Initializing Transaction ... " |
---|
313 | << G4endl; |
---|
314 | } |
---|
315 | Initialize(); |
---|
316 | } |
---|
317 | |
---|
318 | // Start event IO transaction |
---|
319 | // |
---|
320 | if ( TransactionManager()->StartRead() ) { |
---|
321 | if ( m_verbose > 2 ) { |
---|
322 | G4cout << "G4PersistencyManager: Read transaction started." |
---|
323 | << G4endl; |
---|
324 | } |
---|
325 | } else { |
---|
326 | G4cerr << "TransactionManager::Retrieve(G4Event) - StartRead() failed." |
---|
327 | << G4endl; |
---|
328 | return false; |
---|
329 | } |
---|
330 | |
---|
331 | G4bool st = false; |
---|
332 | std::string file; |
---|
333 | |
---|
334 | // Retrieve a G4EVENT |
---|
335 | // |
---|
336 | std::string obj = "Hits"; |
---|
337 | if ( f_pc->CurrentRetrieveMode(obj) == true ) { |
---|
338 | file = f_pc->CurrentReadFile(obj); |
---|
339 | if ( TransactionManager()->SelectReadFile(obj, file) ) { |
---|
340 | st = EventIO()->Retrieve(evt); |
---|
341 | if ( st && m_verbose > 1 ) { |
---|
342 | G4cout << " -- File : " << file << " -- Event# " |
---|
343 | << evt->GetEventID() |
---|
344 | << " -- G4Event is Retrieved." << G4endl; |
---|
345 | } |
---|
346 | } else { |
---|
347 | st = false; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | if ( st ) { |
---|
352 | TransactionManager()->Commit(); |
---|
353 | } else { |
---|
354 | G4cerr << "G4PersistencyManager::Retrieve() - Transaction aborted." |
---|
355 | << G4endl; |
---|
356 | TransactionManager()->Abort(); |
---|
357 | } |
---|
358 | |
---|
359 | return st; |
---|
360 | } |
---|
361 | |
---|
362 | #ifndef WIN32 |
---|
363 | #ifdef G4LIB_USE_HEPMC |
---|
364 | |
---|
365 | // Implementation of Retrieve |
---|
366 | G4bool G4PersistencyManager::Retrieve(HepMC::GenEvent*& evt, int id) |
---|
367 | { |
---|
368 | if ( m_verbose > 2 ) { |
---|
369 | G4cout << "G4PersistencyManager::Retrieve(HepMC::GenEvent*&) is called." |
---|
370 | << G4endl; |
---|
371 | } |
---|
372 | |
---|
373 | if ( TransactionManager() == 0 ) return true; |
---|
374 | |
---|
375 | // Call package dependent Initialize() |
---|
376 | // |
---|
377 | if ( ! f_is_initialized ) { |
---|
378 | f_is_initialized = true; |
---|
379 | if ( m_verbose > 1 ) { |
---|
380 | G4cout << "G4PersistencyManager:: Initializing Transaction ... " |
---|
381 | << G4endl; |
---|
382 | } |
---|
383 | Initialize(); |
---|
384 | } |
---|
385 | |
---|
386 | // Start event IO transaction |
---|
387 | // |
---|
388 | if ( TransactionManager()->StartRead() ) { |
---|
389 | if ( m_verbose > 2 ) { |
---|
390 | G4cout << "G4PersistencyManager: Read transaction started." |
---|
391 | << G4endl; |
---|
392 | } |
---|
393 | } else { |
---|
394 | G4cerr << "TransactionManager::Retrieve(HepMC) - StartRead() failed." |
---|
395 | << G4endl; |
---|
396 | return false; |
---|
397 | } |
---|
398 | |
---|
399 | G4bool st = false; |
---|
400 | std::string file; |
---|
401 | |
---|
402 | // Retrieve a HepMC GenEvent |
---|
403 | // |
---|
404 | std::string obj = "HepMC"; |
---|
405 | if ( f_pc->CurrentRetrieveMode(obj) == true ) { |
---|
406 | file = f_pc->CurrentReadFile(obj); |
---|
407 | if ( TransactionManager()->SelectReadFile(obj, file) ) { |
---|
408 | st = HepMCIO()->Retrieve(evt, id); |
---|
409 | if ( st && m_verbose > 1 ) { |
---|
410 | G4cout << " -- File: " << file |
---|
411 | << " - Event# " << HepMCIO()->LastEventID() |
---|
412 | << " - HepMC event is Retrieved." << G4endl; |
---|
413 | } |
---|
414 | } else { |
---|
415 | st = false; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | if ( st ) { |
---|
420 | TransactionManager()->Commit(); |
---|
421 | } else { |
---|
422 | G4cerr << "G4PersistencyManager::Retrieve(HepMC) - Transaction aborted." |
---|
423 | << G4endl; |
---|
424 | TransactionManager()->Abort(); |
---|
425 | } |
---|
426 | |
---|
427 | return st; |
---|
428 | } |
---|
429 | #endif |
---|
430 | #endif |
---|
431 | |
---|
432 | // End of G4PersistencyManager.cc |
---|
433 | |
---|