Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4XmlAnalysisManager.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$
27
28// Author: Ivana Hrivnacova, 15/06/2011 ([email protected])
29
31#include "G4UnitsTable.hh"
32
33#include "tools/waxml/begend"
34#include "tools/waxml/histos"
35
36#include <iostream>
37
38G4XmlAnalysisManager* G4XmlAnalysisManager::fgInstance = 0;
39
40//_____________________________________________________________________________
42{
43 if ( fgInstance == 0 ) {
44 fgInstance = new G4XmlAnalysisManager();
45 }
46
47 return fgInstance;
48}
49
50//_____________________________________________________________________________
52 : G4VAnalysisManager("Xml"),
53 fFile(0),
54 fH1Vector(),
55 fH2Vector(),
56 fH1NameIdMap(),
57 fH2NameIdMap(),
58 fNtuple(0),
59 fNtupleBooking(0),
60 fNtupleIColumnMap(),
61 fNtupleFColumnMap(),
62 fNtupleDColumnMap()
63{
64 if ( fgInstance ) {
65 G4ExceptionDescription description;
66 description << "G4XmlAnalysisManager already exists."
67 << "Cannot create another instance.";
68 G4Exception("G4XmlAnalysisManager::G4XmlAnalysisManager",
69 "Analysis_F001", FatalException, description);
70 }
71
72 fgInstance = this;
73}
74
75//_____________________________________________________________________________
77{
78 std::vector<tools::histo::h1d*>::iterator it;
79 for ( it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
80 delete *it;
81 }
82
83 std::vector<tools::histo::h2d*>::iterator it2;
84 for ( it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
85 delete *it2;
86 }
87
88 delete fNtuple;
89 delete fNtupleBooking;
90 delete fFile;
91
92 fgInstance = 0;
93}
94
95//
96// private methods
97//
98
99//_____________________________________________________________________________
100void G4XmlAnalysisManager::CreateNtupleFromBooking()
101{
102// Create ntuple from ntuple_booking.
103#ifdef G4VERBOSE
104 if ( fpVerboseL4 )
105 fpVerboseL4->Message("create from booking", "ntuple", fNtupleBooking->m_name);
106#endif
107
108 fNtuple = new tools::waxml::ntuple(*fFile, G4cerr, *fNtupleBooking);
109 if ( fNtupleBooking->m_columns.size() ) {
110 // store ntuple columns in local maps
111 const std::vector<tools::ntuple_booking::col_t>& columns
112 = fNtupleBooking->m_columns;
113 std::vector<tools::ntuple_booking::col_t>::const_iterator it;
114 G4int index = 0;
115 for ( it = columns.begin(); it!=columns.end(); ++it) {
116 if ( (*it).second == tools::_cid(int(0) ) ) {
117 G4cout << "adding int " << fNtuple->find_column<int>((*it).first) << G4endl;
118 fNtupleIColumnMap[index++] = fNtuple->find_column<int>((*it).first);
119 }
120 else if( (*it).second == tools::_cid(float(0) ) ) {
121 fNtupleFColumnMap[index++] = fNtuple->find_column<float>((*it).first);
122 }
123 else if((*it).second== tools::_cid(double(0))) {
124 fNtupleDColumnMap[index++] = fNtuple->find_column<double>((*it).first);
125 }
126 else {
127 G4ExceptionDescription description;
128 description << " "
129 << "Unsupported column type " << (*it).first;
130 G4Exception("G4XmlAnalysisManager::OpenFile()",
131 "Analysis_W004", JustWarning, description);
132 }
133 }
134 }
135 FinishNtuple();
136}
137
138//_____________________________________________________________________________
139tools::waxml::ntuple::column<int>*
140G4XmlAnalysisManager::GetNtupleIColumn(G4int id) const
141{
142 std::map<G4int, tools::waxml::ntuple::column<int>* >::const_iterator it
143 = fNtupleIColumnMap.find(id);
144 if ( it == fNtupleIColumnMap.end() ) {
145 G4ExceptionDescription description;
146 description << " " << "column " << id << " does not exist.";
147 G4Exception("G4XmlAnalysisManager::GetNtupleIColumn()",
148 "Analysis_W009", JustWarning, description);
149 return 0;
150 }
151
152 return it->second;
153}
154
155//_____________________________________________________________________________
156tools::waxml::ntuple::column<float>*
157G4XmlAnalysisManager::GetNtupleFColumn(G4int id) const
158{
159 std::map<G4int, tools::waxml::ntuple::column<float>* >::const_iterator it
160 = fNtupleFColumnMap.find(id);
161 if ( it == fNtupleFColumnMap.end() ) {
162 G4ExceptionDescription description;
163 description << " " << "column " << id << " does not exist.";
164 G4Exception("G4XmlAnalysisManager::GetNtupleFColumn()",
165 "Analysis_W009", JustWarning, description);
166 return 0;
167 }
168
169 return it->second;
170}
171
172//_____________________________________________________________________________
173G4bool G4XmlAnalysisManager::Reset()
174{
175// Reset histograms and ntuple
176
177 G4bool finalResult = true;
178
179 std::vector<tools::histo::h1d*>::iterator it;
180 for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
181 G4bool result = (*it)->reset();
182 if ( ! result ) finalResult = false;
183 }
184
185 std::vector<tools::histo::h2d*>::iterator it2;
186 for (it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
187 G4bool result = (*it2)->reset();
188 if ( ! result ) finalResult = false;
189 }
190
191 delete fNtuple;
192 fNtuple = 0;
193
194 return finalResult;
195}
196
197
198//_____________________________________________________________________________
199tools::waxml::ntuple::column<double>*
200G4XmlAnalysisManager::GetNtupleDColumn(G4int id) const
201{
202 std::map<G4int, tools::waxml::ntuple::column<double>* >::const_iterator it
203 = fNtupleDColumnMap.find(id);
204 if ( it == fNtupleDColumnMap.end() ) {
205 G4ExceptionDescription description;
206 description << " " << "column " << id << " does not exist.";
207 G4Exception("G4XmlAnalysisManager::GetNtupleDColumn()",
208 "Analysis_W009", JustWarning, description);
209 return 0;
210 }
211
212 return it->second;
213}
214
215//_____________________________________________________________________________
216tools::histo::h1d* G4XmlAnalysisManager::GetH1InFunction(G4int id,
217 G4String functionName, G4bool warn,
218 G4bool onlyIfActive) const
219{
220 G4int index = id - fFirstHistoId;
221 if ( index < 0 || index >= G4int(fH1Vector.size()) ) {
222 if ( warn) {
223 G4String inFunction = "G4XmlAnalysisManager::";
224 inFunction += functionName;
225 G4ExceptionDescription description;
226 description << " " << "histogram " << id << " does not exist.";
227 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
228 }
229 return 0;
230 }
231
232 // Do not return histogram if inactive
233 if ( fActivation && onlyIfActive && ( ! GetActivation(kH1, id) ) ) {
234 return 0;
235 }
236
237 return fH1Vector[index];
238}
239
240//_____________________________________________________________________________
241tools::histo::h2d* G4XmlAnalysisManager::GetH2InFunction(G4int id,
242 G4String functionName, G4bool warn,
243 G4bool onlyIfActive) const
244{
245 G4int index = id - fFirstHistoId;
246 if ( index < 0 || index >= G4int(fH2Vector.size()) ) {
247 if ( warn) {
248 G4String inFunction = "G4XmlAnalysisManager::";
249 inFunction += functionName;
250 G4ExceptionDescription description;
251 description << " " << "histogram " << id << " does not exist.";
252 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
253 }
254 return 0;
255 }
256
257 // Do not return histogram if inactive
258 if ( fActivation && onlyIfActive && ( ! GetActivation(kH2, id) ) ) {
259 return 0;
260 }
261
262 return fH2Vector[index];
263}
264
265//_____________________________________________________________________________
266void G4XmlAnalysisManager::UpdateTitle(G4String& title,
267 const G4String& unitName,
268 const G4String& fcnName) const
269{
270 if ( fcnName != "none" ) { title += " "; title += fcnName; title += "("; }
271 if ( unitName != "none" ) { title += " ["; title += unitName; title += "]";}
272 if ( fcnName != "none" ) { title += ")"; }
273}
274
275//
276// protected methods
277//
278
279//_____________________________________________________________________________
281{
282// Write selected objects on ASCII file
283// (Only H1 implemented by now)
284// According to the implementation by Michel Maire, originally in
285// extended examples.
286
287 // h1 histograms
288 for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
289 G4int id = i + fFirstHistoId;
291 // skip writing if activation is enabled and H1 is inactivated
292 if ( ! info->fAscii ) continue;
293 tools::histo::h1d* h1 = fH1Vector[i];
294
295#ifdef G4VERBOSE
296 if ( fpVerboseL3 )
297 fpVerboseL3->Message("write on ascii", "h1d", info->fName);
298#endif
299
300 output << "\n 1D histogram " << id << ": " << h1->title()
301 << "\n \n \t X \t\t Y" << G4endl;
302
303 for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
304 output << " " << j << "\t"
305 << h1->axis().bin_center(j) << "\t"
306 << h1->bin_height(j) << G4endl;
307 }
308 }
309
310 return true;
311}
312
313
314//
315// public methods
316//
317
318//_____________________________________________________________________________
320{
321 // Add file extension .Xml if no extension is given
322 G4String name(fileName);
323 if ( name.find(".") == std::string::npos ) {
324 name.append(".");
325 name.append(GetFileType());
326 }
327
328 #ifdef G4VERBOSE
329 if ( fpVerboseL4 )
330 fpVerboseL4->Message("open", "analysis file", name);
331#endif
332
333 // delete a previous file if it exists
334 if ( fFile ) delete fFile;
335
336 fFile = new std::ofstream(name);
337 if ( fFile->fail() ) {
338 G4ExceptionDescription description;
339 description << " " << "Cannot open file " << name;
340 G4Exception("G4XmlAnalysisManager::OpenFile()",
341 "Analysis_W001", JustWarning, description);
342 return false;
343 }
344
345 tools::waxml::begin(*fFile);
346#ifdef G4VERBOSE
347 if ( fpVerboseL1 )
348 fpVerboseL1->Message("open", "analysis file", name);
349#endif
350
351 // Create ntuple if it is booked
352 if ( fNtupleBooking && ( ! fNtuple ) )
353 CreateNtupleFromBooking();
354
355 fLockFileName = true;
356 return true;
357}
358
359//_____________________________________________________________________________
361{
362 // ntuple
363 if ( fNtuple ) fNtuple->write_trailer();
364
365 // h1 histograms
366 for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
367 G4int id = i + fFirstHistoId;
369 // skip writing if activation is enabled and H1 is inactivated
370 if ( fActivation && ( ! info->fActivation ) ) continue;
371 tools::histo::h1d* h1 = fH1Vector[i];
372#ifdef G4VERBOSE
373 if ( fpVerboseL3 )
374 fpVerboseL3->Message("write", "h1d", info->fName);
375#endif
376 G4String path = "/";
378 G4bool result
379 = tools::waxml::write(*fFile, *h1, path, info->fName);
380 if ( ! result ) {
381 G4ExceptionDescription description;
382 description << " " << "saving histogram " << info->fName << " failed";
383 G4Exception("G4XmlAnalysisManager::Write()",
384 "Analysis_W003", JustWarning, description);
385 return false;
386 }
388 }
389
390 // h2 histograms
391 for ( G4int i=0; i<G4int(fH2Vector.size()); ++i ) {
392 G4int id = i + fFirstHistoId;
394 // skip writing if inactivated
395 if ( fActivation && ( ! info->fActivation ) ) continue;
396 tools::histo::h2d* h2 = fH2Vector[i];
397 #ifdef G4VERBOSE
398 if ( fpVerboseL3 )
399 fpVerboseL3->Message("write", "h2d", info->fName);
400#endif
401 G4String path = "/";
403 G4bool result
404 = tools::waxml::write(*fFile, *h2, path, info->fName);
405 if ( ! result ) {
406 G4ExceptionDescription description;
407 description << " " << "saving histogram " << info->fName << " failed";
408 G4Exception("G4XmlAnalysisManager::Write()",
409 "Analysis_W003", JustWarning, description);
410 return false;
411 }
413 }
414 G4bool result = true;
415
416#ifdef G4VERBOSE
417 if ( fpVerboseL1 )
418 fpVerboseL1->Message("write", "file", GetFullFileName(), result);
419#endif
420
421 // Write ASCII if activated
422 if ( IsAscii() ) {
423 result = WriteAscii();
424 }
425
426 return result;
427}
428
429//_____________________________________________________________________________
431{
432 G4bool result = true;
433
434#ifdef G4VERBOSE
435 if ( fpVerboseL4 )
436 fpVerboseL4->Message("close", "file", GetFullFileName());
437#endif
438
439 // reset data
440 result = Reset();
441 if ( ! result ) {
442 G4ExceptionDescription description;
443 description << " " << "Resetting data failed";
444 G4Exception("G4XmlAnalysisManager::CloseFile()",
445 "Analysis_W002", JustWarning, description);
446 result = false;
447 }
448
449 // close file
450 tools::waxml::end(*fFile);
451 fFile->close();
452 fLockFileName = false;
453
454#ifdef G4VERBOSE
455 if ( fpVerboseL1 )
456 fpVerboseL1->Message("close", "file", GetFullFileName());
457#endif
458
459 return result;
460}
461
462//_____________________________________________________________________________
464 G4int nbins, G4double xmin, G4double xmax,
465 const G4String& unitName, const G4String& fcnName)
466{
467#ifdef G4VERBOSE
468 if ( fpVerboseL4 )
469 fpVerboseL4->Message("create", "H1", name);
470#endif
471 G4int index = fH1Vector.size();
472 G4double unit = GetUnitValue(unitName);
473 G4Fcn fcn = GetFunction(fcnName);
474 tools::histo::h1d* h1
475 = new tools::histo::h1d(title, nbins, fcn(xmin), fcn(xmax));
476 // h1 objects are deleted in destructor and reset when
477 // closing a file.
478
479 G4String axisTitle;
480 UpdateTitle(axisTitle,unitName, fcnName);
481 h1->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
482
483 fH1Vector.push_back(h1);
484 AddH1Information(name, unitName, fcnName, unit, fcn);
485
486 fLockFirstHistoId = true;
487#ifdef G4VERBOSE
488 if ( fpVerboseL2 )
489 fpVerboseL2->Message("create", "H1", name);
490#endif
491 fH1NameIdMap[name] = index + fFirstHistoId;
492 return index + fFirstHistoId;
493}
494
495//_____________________________________________________________________________
497 G4int nxbins, G4double xmin, G4double xmax,
498 G4int nybins, G4double ymin, G4double ymax,
499 const G4String& xunitName, const G4String& yunitName,
500 const G4String& xfcnName, const G4String& yfcnName)
501{
502#ifdef G4VERBOSE
503 if ( fpVerboseL4 )
504 fpVerboseL4->Message("create", "H2", name);
505#endif
506 G4int index = fH2Vector.size();
507 G4double xunit = GetUnitValue(xunitName);
508 G4double yunit = GetUnitValue(yunitName);
509 G4Fcn xfcn = GetFunction(xfcnName);
510 G4Fcn yfcn = GetFunction(yfcnName);
511 tools::histo::h2d* h2
512 = new tools::histo::h2d(title,
513 nxbins, xfcn(xmin), xfcn(xmax),
514 nybins, yfcn(ymin), yfcn(ymax));
515 // h1 objects are deleted in destructor and reset when
516 // closing a file.
517
518 G4String xaxisTitle;
519 G4String yaxisTitle;
520 UpdateTitle(xaxisTitle, xunitName, xfcnName);
521 UpdateTitle(yaxisTitle, yunitName, yfcnName);
522 h2->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
523 h2->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
524
525 fH2Vector.push_back(h2);
526 AddH2Information(name, xunitName, yunitName, xfcnName, yfcnName,
527 xunit, yunit, xfcn, yfcn);
528
529 fLockFirstHistoId = true;
530#ifdef G4VERBOSE
531 if ( fpVerboseL2 )
532 fpVerboseL2->Message("create", "H2", name);
533#endif
534 fH2NameIdMap[name] = index + fFirstHistoId;
535 return index + fFirstHistoId;
536}
537
538//_____________________________________________________________________________
540 G4int nbins, G4double xmin, G4double xmax,
541 const G4String& unitName, const G4String& fcnName)
542{
543
544 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1", false, false);
545 if ( ! h1d ) return false;
546
548#ifdef G4VERBOSE
549 if ( fpVerboseL4 )
550 fpVerboseL4->Message("configure", "H1", info->fName);
551#endif
552
553 G4double unit = GetUnitValue(unitName);
554 G4Fcn fcn = GetFunction(fcnName);
555 h1d->configure(nbins, fcn(xmin), fcn(xmax));
556 info->fXUnitName = unitName;
557 info->fYUnitName = unitName;
558 info->fXFcnName = fcnName;
559 info->fYFcnName = fcnName;
560 info->fXUnit = unit;
561 info->fYUnit = unit;
562 info->fXFcn = fcn;
563 info->fYFcn = fcn;
564 SetActivation(kH1, id, true);
565
566 G4String axisTitle;
567 UpdateTitle(axisTitle,unitName, fcnName);
568 h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
569
570 return true;
571}
572
573//_____________________________________________________________________________
575 G4int nxbins, G4double xmin, G4double xmax,
576 G4int nybins, G4double ymin, G4double ymax,
577 const G4String& xunitName, const G4String& yunitName,
578 const G4String& xfcnName, const G4String& yfcnName)
579{
580 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2", false, false);
581 if ( ! h2d ) return false;
582
584#ifdef G4VERBOSE
585 if ( fpVerboseL4 )
586 fpVerboseL4->Message("configure", "H2", info->fName);
587#endif
588
589 G4double xunit = GetUnitValue(xunitName);
590 G4double yunit = GetUnitValue(yunitName);
591 G4Fcn xfcn = GetFunction(xfcnName);
592 G4Fcn yfcn = GetFunction(yfcnName);
593 h2d->configure(nxbins, xfcn(xmin), xfcn(xmax),
594 nybins, yfcn(ymin), yfcn(ymax));
595 info->fXUnitName = xunitName;
596 info->fYUnitName = yunitName;
597 info->fXFcnName = xfcnName;
598 info->fYFcnName = yfcnName;
599 info->fXUnit = xunit;
600 info->fYUnit = yunit;
601 info->fXFcn = xfcn;
602 info->fYFcn = yfcn;
603 SetActivation(kH2, id, true);
604
605 G4String xaxisTitle;
606 G4String yaxisTitle;
607 UpdateTitle(xaxisTitle, xunitName, xfcnName);
608 UpdateTitle(yaxisTitle, yunitName, yfcnName);
609 h2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
610 h2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
611
612 return true;
613}
614
615//_____________________________________________________________________________
617{
618 tools::histo::h1d* h1d = GetH1InFunction(id, "ScaleH1", false, false);
619 if ( ! h1d ) return false;
620
621 return h1d->scale(factor);
622}
623
624//_____________________________________________________________________________
626{
627 tools::histo::h2d* h2d = GetH2InFunction(id, "ScaleH2", false, false);
628 if ( ! h2d ) return false;
629
630 return h2d->scale(factor);
631}
632
633//_____________________________________________________________________________
635 const G4String& title)
636{
637 if ( fNtupleBooking ) {
638 G4ExceptionDescription description;
639 description << " "
640 << "Ntuple already exists. "
641 << "(Only one ntuple is currently supported.)";
642 G4Exception("G4XmlAnalysisManager::CreateNtuple()",
643 "Analysis_W006", JustWarning, description);
644 return;
645 }
646
647#ifdef G4VERBOSE
648 if ( fpVerboseL4 )
649 fpVerboseL4->Message("create", "ntuple", name);
650#endif
651
652 // Create ntuple booking
653 fNtupleBooking = new tools::ntuple_booking();
654 fNtupleBooking->m_name = name;
655 fNtupleBooking->m_title = title;
656
657 // Create ntuple if the file is open
658 if ( fFile ) {
659 fNtuple = new tools::waxml::ntuple(*fFile);
660 // ntuple object is deleted when closing a file
661 }
662
663#ifdef G4VERBOSE
664 if ( fpVerboseL2 )
665 fpVerboseL2->Message("create", "ntuple", name);
666#endif
667}
668
669//_____________________________________________________________________________
671{
672#ifdef G4VERBOSE
673 if ( fpVerboseL4 )
674 fpVerboseL4->Message("create", "ntuple I column", name);
675#endif
676
677 if ( ! fNtupleBooking ) {
678 G4ExceptionDescription description;
679 description << " "
680 << "Ntuple has to be created first. ";
681 G4Exception("G4XmlAnalysisManager::CreateNtupleIColumn()",
682 "Analysis_W005", JustWarning, description);
683 return -1;
684 }
685
686 // Save column info in booking
687 G4int index = fNtupleBooking->m_columns.size();
688 fNtupleBooking->add_column<int>(name);
689
690 // Create column if ntuple already exists
691 if ( fNtuple ) {
692 tools::waxml::ntuple::column<int>* column
693 = fNtuple->create_column<int>(name);
694 fNtupleIColumnMap[index] = column;
695 }
696
698
699#ifdef G4VERBOSE
700 if ( fpVerboseL2 )
701 fpVerboseL2->Message("create", "ntuple I column", name);
702#endif
703
704 return index + fFirstNtupleColumnId;
705}
706
707//_____________________________________________________________________________
709{
710#ifdef G4VERBOSE
711 if ( fpVerboseL4 )
712 fpVerboseL4->Message("create", "ntuple F column", name);
713#endif
714
715 if ( ! fNtupleBooking ) {
716 G4ExceptionDescription description;
717 description << " "
718 << "Ntuple has to be created first. ";
719 G4Exception("G4XmlAnalysisManager::CreateNtupleFColumn()",
720 "Analysis_W005", JustWarning, description);
721 return -1;
722 }
723
724 // Save column info in booking
725 G4int index = fNtupleBooking->m_columns.size();
726 fNtupleBooking->add_column<float>(name);
727
728 // Create column if ntuple already exists
729 if ( fNtuple ) {
730 tools::waxml::ntuple::column<float>* column
731 = fNtuple->create_column<float>(name);
732 fNtupleFColumnMap[index] = column;
733 }
734
736
737#ifdef G4VERBOSE
738 if ( fpVerboseL2 )
739 fpVerboseL2->Message("create", "ntuple F column", name);
740#endif
741
742 return index + fFirstNtupleColumnId;
743}
744
745//_____________________________________________________________________________
747{
748#ifdef G4VERBOSE
749 if ( fpVerboseL4 )
750 fpVerboseL4->Message("create", "ntuple D column", name);
751#endif
752
753 if ( ! fNtupleBooking ) {
754 G4ExceptionDescription description;
755 description << " "
756 << "Ntuple has to be created first. ";
757 G4Exception("G4XmlAnalysisManager::CreateNtupleDColumn()",
758 "Analysis_W005", JustWarning, description);
759 return -1;
760 }
761
762 // Save column info in booking
763 G4int index = fNtupleBooking->m_columns.size();
764 fNtupleBooking->add_column<double>(name);
765
766 // Create column if ntuple already exists
767 if ( fNtuple ) {
768 tools::waxml::ntuple::column<double>* column
769 = fNtuple->create_column<double>(name);
770 fNtupleDColumnMap[index] = column;
771 }
772
774
775#ifdef G4VERBOSE
776 if ( fpVerboseL2 )
777 fpVerboseL2->Message("create", "ntuple D column", name);
778#endif
779
780 return index + fFirstNtupleColumnId;
781}
782
783//_____________________________________________________________________________
785{
786 if ( ! fNtuple ) return;
787
788#ifdef G4VERBOSE
789 if ( fpVerboseL4 )
790 fpVerboseL4->Message("finish", "ntuple", fNtupleBooking->m_name);
791#endif
792
793 G4String path = "/";
795 fNtuple->write_header(path, fNtupleBooking->m_name, fNtupleBooking->m_title);
796
798
799#ifdef G4VERBOSE
800 if ( fpVerboseL2 )
801 fpVerboseL2->Message("finish", "ntuple", fNtupleBooking->m_name);
802#endif
803}
804
805
806//_____________________________________________________________________________
808{
809 tools::histo::h1d* h1d = GetH1InFunction(id, "FillH1", true, false);
810 if ( ! h1d ) return false;
811
812 if ( fActivation && ( ! GetActivation(kH1, id) ) ) {
813 //G4cout << "Skipping FillH1 for " << id << G4endl;
814 return false;
815 }
816
818 h1d->fill(info->fXFcn(value/info->fXUnit), weight);
819#ifdef G4VERBOSE
820 if ( fpVerboseL4 ) {
821 G4ExceptionDescription description;
822 description << " id " << id << " value " << value/GetXUnit(kH1, id);
823 fpVerboseL4->Message("fill", "H1", description);
824 }
825#endif
826 return true;
827}
828
829//_____________________________________________________________________________
831 G4double xvalue, G4double yvalue,
832 G4double weight)
833{
834 tools::histo::h2d* h2d = GetH2InFunction(id, "FillH2", true, false);
835 if ( ! h2d ) return false;
836
837 if ( fActivation && ( ! GetActivation(kH2, id) ) ) return false;
838
840 h2d->fill(info->fXFcn(xvalue/info->fXUnit),
841 info->fYFcn(yvalue/info->fYUnit), weight);
842#ifdef G4VERBOSE
843 if ( fpVerboseL4 ) {
844 G4ExceptionDescription description;
845 description << " id " << id
846 << " xvalue " << xvalue/GetXUnit(kH2, id)
847 << " yvalue " << yvalue/GetYUnit(kH2, id);
848 fpVerboseL4->Message("fill", "H2", description);
849 }
850#endif
851 return true;
852}
853
854//_____________________________________________________________________________
856{
857 tools::waxml::ntuple::column<int>* column = GetNtupleIColumn(id);
858 if ( ! column ) {
859 G4ExceptionDescription description;
860 description << " " << "column " << id << " does not exist.";
861 G4Exception("G4XmlAnalysisManager::FillNtupleIColumn()",
862 "Analysis_W009", JustWarning, description);
863 return false;
864 }
865
866 column->fill(value);
867#ifdef G4VERBOSE
868 if ( fpVerboseL4 ) {
869 G4ExceptionDescription description;
870 description << " id " << id << " value " << value;
871 fpVerboseL4->Message("fill", "ntuple I column", description);
872 }
873#endif
874 return true;
875}
876//_____________________________________________________________________________
878{
879 tools::waxml::ntuple::column<float>* column = GetNtupleFColumn(id);
880 if ( ! column ) {
881 G4ExceptionDescription description;
882 description << " " << "column " << id << " does not exist.";
883 G4Exception("G4XmlAnalysisManager::FillNtupleFColumn()",
884 "Analysis_W009", JustWarning, description);
885 return false;
886 }
887
888 column->fill(value);
889#ifdef G4VERBOSE
890 if ( fpVerboseL4 ) {
891 G4ExceptionDescription description;
892 description << " id " << id << " value " << value;
893 fpVerboseL4->Message("fill", "ntuple F column", description);
894 }
895#endif
896 return true;
897}
898
899//_____________________________________________________________________________
901{
902 tools::waxml::ntuple::column<double>* column = GetNtupleDColumn(id);
903 if ( ! column ) {
904 G4ExceptionDescription description;
905 description << " " << "column " << id << " does not exist.";
906 G4Exception("G4XmlAnalysisManager::FillNtupleDColumn()",
907 "Analysis_W009", JustWarning, description);
908 return false;
909 }
910
911 column->fill(value);
912#ifdef G4VERBOSE
913 if ( fpVerboseL4 ) {
914 G4ExceptionDescription description;
915 description << " id " << id << " value " << value;
916 fpVerboseL4->Message("fill", "ntuple D column", description);
917 }
918#endif
919 return true;
920}
921
922//_____________________________________________________________________________
924{
925#ifdef G4VERBOSE
926 if ( fpVerboseL4 )
927 fpVerboseL4->Message("add", "ntuple row", "");
928#endif
929
930 if ( ! fNtuple ) {
931 G4ExceptionDescription description;
932 description << " " << "ntuple does not exist. ";
933 G4Exception("G4XmlAnalysisManager::AddNtupleRow()",
934 "Analysis_W008", JustWarning, description);
935 return false;
936 }
937
938 fNtuple->add_row();
939#ifdef G4VERBOSE
940 if ( fpVerboseL4 )
941 fpVerboseL4->Message("add", "ntuple row", "");
942#endif
943
944 return true;
945}
946
947//_____________________________________________________________________________
948tools::histo::h1d* G4XmlAnalysisManager::GetH1(G4int id, G4bool warn,
949 G4bool onlyIfActive) const
950{
951 return GetH1InFunction(id, "GetH1", warn, onlyIfActive);
952}
953
954//_____________________________________________________________________________
955tools::histo::h2d* G4XmlAnalysisManager::GetH2(G4int id, G4bool warn,
956 G4bool onlyIfActive) const
957{
958 return GetH2InFunction(id, "GetH2", warn, onlyIfActive);
959}
960
961//_____________________________________________________________________________
963{
964 std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
965 if ( it == fH1NameIdMap.end() ) {
966 if ( warn) {
967 G4String inFunction = "G4RootAnalysisManager::GetH1Id";
968 G4ExceptionDescription description;
969 description << " " << "histogram " << name << " does not exist.";
970 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
971 }
972 return -1;
973 }
974 return it->second;
975}
976
977//_____________________________________________________________________________
979{
980 std::map<G4String, G4int>::const_iterator it = fH2NameIdMap.find(name);
981 if ( it == fH2NameIdMap.end() ) {
982 if ( warn) {
983 G4String inFunction = "G4RootAnalysisManager::GetH2Id";
984 G4ExceptionDescription description;
985 description << " " << "histogram " << name << " does not exist.";
986 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
987 }
988 return -1;
989 }
990 return it->second;
991}
992
993//_____________________________________________________________________________
994tools::waxml::ntuple* G4XmlAnalysisManager::GetNtuple() const
995{
996 return fNtuple;
997}
998
999//_____________________________________________________________________________
1001{
1002 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Nbins");
1003 if ( ! h1d ) return 0;
1004
1005 return h1d->axis().bins();
1006}
1007
1008//_____________________________________________________________________________
1010{
1011// Returns xmin value with applied unit and histogram function
1012
1013 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmin");
1014 if ( ! h1d ) return 0;
1015
1016 G4HnInformation* info = GetInformation(kH1, id);
1017 return info->fXFcn(h1d->axis().lower_edge()*info->fXUnit);
1018}
1019
1020//_____________________________________________________________________________
1022{
1023 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmax");
1024 if ( ! h1d ) return 0;
1025
1026 G4HnInformation* info = GetInformation(kH1, id);
1027 return info->fXFcn(h1d->axis().upper_edge()*info->fXUnit);
1028}
1029
1030//_____________________________________________________________________________
1032{
1033 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XWidth", true, false);
1034 if ( ! h1d ) return 0;
1035
1036 G4int nbins = h1d->axis().bins();
1037 if ( ! nbins ) {
1038 G4ExceptionDescription description;
1039 description << " nbins = 0 (for h1 id = " << id << ").";
1040 G4Exception("G4XmlAnalysisManager::GetH1Width",
1041 "Analysis_W014", JustWarning, description);
1042 return 0;
1043 }
1044
1045 G4HnInformation* info = GetInformation(kH1, id);
1046 return ( info->fXFcn(h1d->axis().upper_edge())
1047 - info->fXFcn(h1d->axis().lower_edge()))*info->fXUnit/nbins;
1048}
1049
1050//_____________________________________________________________________________
1052{
1053 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2NXbins");
1054 if ( ! h2d ) return 0;
1055
1056 return h2d->axis_x().bins();
1057}
1058
1059//_____________________________________________________________________________
1061{
1062// Returns xmin value with applied unit and histogram function
1063
1064 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Xmin");
1065 if ( ! h2d ) return 0;
1066
1067 G4HnInformation* info = GetInformation(kH2, id);
1068 return info->fXFcn(h2d->axis_x().lower_edge()*info->fXUnit);
1069}
1070
1071//_____________________________________________________________________________
1073{
1074 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Xmax");
1075 if ( ! h2d ) return 0;
1076
1077 G4HnInformation* info = GetInformation(kH2, id);
1078 return info->fXFcn(h2d->axis_x().upper_edge()*info->fXUnit);
1079}
1080
1081//_____________________________________________________________________________
1083{
1084 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2XWidth", true, false);
1085 if ( ! h2d ) return 0;
1086
1087 G4int nbins = h2d->axis_x().bins();
1088 if ( ! nbins ) {
1089 G4ExceptionDescription description;
1090 description << " nbins = 0 (for h1 id = " << id << ").";
1091 G4Exception("G4XmlAnalysisManager::GetH2Width",
1092 "Analysis_W014", JustWarning, description);
1093 return 0;
1094 }
1095
1096 G4HnInformation* info = GetInformation(kH2, id);
1097 return ( info->fXFcn(h2d->axis_x().upper_edge())
1098 - info->fXFcn(h2d->axis_x().lower_edge()))*info->fXUnit/nbins;
1099}
1100
1101//_____________________________________________________________________________
1103{
1104 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2NYbins");
1105 if ( ! h2d ) return 0;
1106
1107 return h2d->axis_y().bins();
1108}
1109
1110//_____________________________________________________________________________
1112{
1113// Returns xmin value with applied unit and histogram function
1114
1115 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Ymin");
1116 if ( ! h2d ) return 0;
1117
1118 G4HnInformation* info = GetInformation(kH2, id);
1119 return info->fYFcn(h2d->axis_y().lower_edge()*info->fYUnit);
1120}
1121
1122//_____________________________________________________________________________
1124{
1125 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Ymax");
1126 if ( ! h2d ) return 0;
1127
1128 G4HnInformation* info = GetInformation(kH2, id);
1129 return info->fYFcn(h2d->axis_y().upper_edge()*info->fYUnit);
1130}
1131
1132//_____________________________________________________________________________
1134{
1135 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2YWidth", true, false);
1136 if ( ! h2d ) return 0;
1137
1138 G4int nbins = h2d->axis_y().bins();
1139 if ( ! nbins ) {
1140 G4ExceptionDescription description;
1141 description << " nbins = 0 (for h1 id = " << id << ").";
1142 G4Exception("G4XmlAnalysisManager::GetH2Width",
1143 "Analysis_W014", JustWarning, description);
1144 return 0;
1145 }
1146
1147 G4HnInformation* info = GetInformation(kH2, id);
1148 return ( info->fYFcn(h2d->axis_y().upper_edge())
1149 - info->fYFcn(h2d->axis_y().lower_edge()))*info->fYUnit/nbins;
1150}
1151
1152//_____________________________________________________________________________
1154{
1155 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1Title");
1156 if ( ! h1d ) return false;
1157
1158 return h1d->set_title(title);
1159}
1160
1161//_____________________________________________________________________________
1163{
1164 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1XAxisTitle");
1165 if ( ! h1d ) return false;
1166
1167 h1d->add_annotation(tools::histo::key_axis_x_title(), title);
1168 return true;
1169}
1170
1171//_____________________________________________________________________________
1173{
1174 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1YAxisTitle");
1175 if ( ! h1d ) return false;
1176
1177 h1d->add_annotation(tools::histo::key_axis_y_title(), title);
1178 return true;
1179}
1180
1181//_____________________________________________________________________________
1183{
1184 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2Title");
1185 if ( ! h2d ) return false;
1186
1187 return h2d->set_title(title);
1188}
1189
1190//_____________________________________________________________________________
1192{
1193 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2XAxisTitle");
1194 if ( ! h2d ) return false;
1195
1196 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1197 return true;
1198}
1199
1200//_____________________________________________________________________________
1202{
1203 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2YAxisTitle");
1204 if ( ! h2d ) return false;
1205
1206 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1207 return true;
1208}
1209
1210//_____________________________________________________________________________
1212{
1213 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2ZAxisTitle");
1214 if ( ! h2d ) return false;
1215
1216 h2d->add_annotation(tools::histo::key_axis_z_title(), title);
1217 return true;
1218}
1219
1220//_____________________________________________________________________________
1222{
1223 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Title");
1224 if ( ! h1d ) return "";
1225
1226 return h1d->title();
1227}
1228
1229//_____________________________________________________________________________
1231{
1232 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XAxisTitle");
1233 if ( ! h1d ) return "";
1234
1235 G4String title;
1236 G4bool result = h1d->annotation(tools::histo::key_axis_x_title(), title);
1237 if ( ! result ) {
1238 G4ExceptionDescription description;
1239 description << " Failed to get x_axis title for h1 id = " << id << ").";
1240 G4Exception("G4XmlAnalysisManager::GetH1XAxisTitle",
1241 "Analysis_W014", JustWarning, description);
1242 return "";
1243 }
1244
1245 return title;
1246}
1247
1248//_____________________________________________________________________________
1250{
1251 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1YAxisTitle");
1252 if ( ! h1d ) return "";
1253
1254 G4String title;
1255 G4bool result = h1d->annotation(tools::histo::key_axis_y_title(), title);
1256 if ( ! result ) {
1257 G4ExceptionDescription description;
1258 description << " Failed to get y_axis title for h1 id = " << id << ").";
1259 G4Exception("G4XmlAnalysisManager::GetH1YAxisTitle",
1260 "Analysis_W014", JustWarning, description);
1261 return "";
1262 }
1263
1264 return title;
1265}
1266
1267//_____________________________________________________________________________
1269{
1270 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Title");
1271 if ( ! h2d ) return "";
1272
1273 return h2d->title();
1274}
1275
1276
1277//_____________________________________________________________________________
1279{
1280 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2XAxisTitle");
1281 if ( ! h2d ) return "";
1282
1283 G4String title;
1284 G4bool result = h2d->annotation(tools::histo::key_axis_x_title(), title);
1285 if ( ! result ) {
1286 G4ExceptionDescription description;
1287 description << " Failed to get x_axis title for h2 id = " << id << ").";
1288 G4Exception("G4XmlAnalysisManager::GetH2XAxisTitle",
1289 "Analysis_W014", JustWarning, description);
1290 return "";
1291 }
1292
1293 return title;
1294}
1295
1296//_____________________________________________________________________________
1298{
1299 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2YAxisTitle");
1300 if ( ! h2d ) return "";
1301
1302 G4String title;
1303 G4bool result = h2d->annotation(tools::histo::key_axis_y_title(), title);
1304 if ( ! result ) {
1305 G4ExceptionDescription description;
1306 description << " Failed to get y_axis title for h2 id = " << id << ").";
1307 G4Exception("G4XmlAnalysisManager::GetH2YAxisTitle",
1308 "Analysis_W014", JustWarning, description);
1309 return "";
1310 }
1311
1312 return title;
1313}
1314
1315//_____________________________________________________________________________
1317{
1318 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2ZAxisTitle");
1319 if ( ! h2d ) return "";
1320
1321 G4String title;
1322 G4bool result = h2d->annotation(tools::histo::key_axis_z_title(), title);
1323 if ( ! result ) {
1324 G4ExceptionDescription description;
1325 description << " Failed to get z_axis title for h2 id = " << id << ").";
1326 G4Exception("G4XmlAnalysisManager::GetH2ZAxisTitle",
1327 "Analysis_W014", JustWarning, description);
1328 return "";
1329 }
1330
1331 return title;
1332}
@ JustWarning
@ FatalException
G4double(* G4Fcn)(G4double)
Definition: G4Fcn.hh:36
double G4double
Definition: G4Types.hh:64
float G4float
Definition: G4Types.hh:65
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true)
G4String & append(const G4String &)
G4double GetUnitValue(const G4String &unit) const
G4bool GetActivation() const
void AddH1Information(const G4String &name, const G4String &unitName, const G4String &fcnName, G4double unit, G4Fcn fx)
G4AnalysisVerbose * fpVerboseL4
void AddH2Information(const G4String &name, const G4String &xunitName, const G4String &yunitName, const G4String &xfcnName, const G4String &yfcnName, G4double xunit, G4double yunit, G4Fcn fx, G4Fcn fy)
G4AnalysisVerbose * fpVerboseL2
G4HnInformation * GetH2Information(G4int id) const
G4double GetYUnit(ObjectType type, G4int id) const
G4HnInformation * GetInformation(ObjectType type, G4int id) const
G4AnalysisVerbose * fpVerboseL3
void SetActivation(G4bool activation)
G4HnInformation * GetH1Information(G4int id) const
G4AnalysisVerbose * fpVerboseL1
G4double GetXUnit(ObjectType type, G4int id) const
G4String GetFileType() const
virtual G4String GetFullFileName() const
G4Fcn GetFunction(const G4String &fcnName) const
virtual G4bool SetH2ZAxisTitle(G4int id, const G4String &title)
virtual tools::histo::h2d * GetH2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual G4bool SetH1Title(G4int id, const G4String &title)
virtual G4double GetH1Xmin(G4int id) const
virtual G4double GetH2Xmin(G4int id) const
virtual G4bool FillNtupleIColumn(G4int id, G4int value)
virtual G4bool SetH2Title(G4int id, const G4String &title)
virtual tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual G4String GetH1Title(G4int id) const
virtual G4double GetH2Ymax(G4int id) const
virtual G4double GetH1Xmax(G4int id) const
virtual G4bool SetH2XAxisTitle(G4int id, const G4String &title)
static G4XmlAnalysisManager * Instance()
virtual G4bool ScaleH2(G4int id, G4double factor)
virtual G4double GetH1Width(G4int id) const
virtual G4bool ScaleH1(G4int id, G4double factor)
virtual G4bool SetH1YAxisTitle(G4int id, const G4String &title)
virtual G4bool SetH2(G4int id, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none")
virtual G4int GetH2Nybins(G4int id) const
virtual G4String GetH1XAxisTitle(G4int id) const
virtual G4bool SetH2YAxisTitle(G4int id, const G4String &title)
virtual void CreateNtuple(const G4String &name, const G4String &title)
virtual G4String GetH2Title(G4int id) const
virtual G4double GetH2XWidth(G4int id) const
virtual G4double GetH2Xmax(G4int id) const
virtual G4double GetH2YWidth(G4int id) const
virtual G4String GetH2XAxisTitle(G4int id) const
virtual G4bool SetH1(G4int id, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none")
virtual G4bool FillNtupleFColumn(G4int id, G4float value)
virtual G4bool FillNtupleDColumn(G4int id, G4double value)
virtual G4int CreateH2(const G4String &name, const G4String &title, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none")
virtual G4bool WriteOnAscii(std::ofstream &output)
virtual G4String GetH2ZAxisTitle(G4int id) const
virtual G4bool FillH2(G4int id, G4double xvalue, G4double yvalue, G4double weight=1.0)
virtual G4int GetH2Id(const G4String &name, G4bool warn=true) const
virtual G4int GetH2Nxbins(G4int id) const
virtual G4bool OpenFile()
virtual G4int GetH1Id(const G4String &name, G4bool warn=true) const
virtual G4int CreateNtupleIColumn(const G4String &name)
virtual G4bool SetH1XAxisTitle(G4int id, const G4String &title)
virtual G4int CreateNtupleFColumn(const G4String &name)
virtual G4bool AddNtupleRow()
virtual G4String GetH1YAxisTitle(G4int id) const
virtual G4int CreateNtupleDColumn(const G4String &name)
virtual G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
virtual G4double GetH2Ymin(G4int id) const
virtual G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none")
virtual G4String GetH2YAxisTitle(G4int id) const
virtual G4int GetH1Nbins(G4int id) const
virtual tools::waxml::ntuple * GetNtuple() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76