Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4CsvAnalysisManager.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 (ivana@ipno.in2p3.fr)
29 
30 #include "G4CsvAnalysisManager.hh"
31 #include "G4UnitsTable.hh"
32 
33 #include "tools/waxml/begend"
34 #include "tools/waxml/histos"
35 
36 #include <iostream>
37 
38 G4CsvAnalysisManager* G4CsvAnalysisManager::fgInstance = 0;
39 
40 //_____________________________________________________________________________
42 {
43  if ( fgInstance == 0 ) {
44  fgInstance = new G4CsvAnalysisManager();
45  }
46 
47  return fgInstance;
48 }
49 
50 //_____________________________________________________________________________
52  : G4VAnalysisManager("Csv"),
53  fFile(0),
54  fNtuple(0),
55  fNtupleBooking(0),
56  fNtupleIColumnMap(),
57  fNtupleFColumnMap(),
58  fNtupleDColumnMap()
59 {
60  if ( fgInstance ) {
61  G4ExceptionDescription description;
62  description << " "
63  << "G4CsvAnalysisManager already exists."
64  << "Cannot create another instance.";
65  G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()",
66  "Analysis_F001", FatalException, description);
67  }
68 
69  fgInstance = this;
70 }
71 
72 //_____________________________________________________________________________
74 {
75  delete fNtuple;
76  delete fNtupleBooking;
77  delete fFile;
78 
79  fgInstance = 0;
80 }
81 
82 //
83 // private methods
84 //
85 
86 //_____________________________________________________________________________
87 tools::wcsv::ntuple::column<int>*
88 G4CsvAnalysisManager::GetNtupleIColumn(G4int id) const
89 {
90  std::map<G4int, tools::wcsv::ntuple::column<int>* >::const_iterator it
91  = fNtupleIColumnMap.find(id);
92  if ( it == fNtupleIColumnMap.end() ) {
93  G4ExceptionDescription description;
94  description << " " << "column " << id << " does not exist.";
95  G4Exception("G4CsvAnalysisManager::GetNtupleIColumn()",
96  "Analysis_W009", JustWarning, description);
97  return 0;
98  }
99 
100  return it->second;
101 }
102 
103 //_____________________________________________________________________________
104 tools::wcsv::ntuple::column<float>*
105 G4CsvAnalysisManager::GetNtupleFColumn(G4int id) const
106 {
107  std::map<G4int, tools::wcsv::ntuple::column<float>* >::const_iterator it
108  = fNtupleFColumnMap.find(id);
109  if ( it == fNtupleFColumnMap.end() ) {
110  G4ExceptionDescription description;
111  description << " " << "column " << id << " does not exist.";
112  G4Exception("G4CsvAnalysisManager::GetNtupleFColumn()",
113  "Analysis_W009", JustWarning, description);
114  return 0;
115  }
116 
117  return it->second;
118 }
119 
120 
121 //_____________________________________________________________________________
122 tools::wcsv::ntuple::column<double>*
123 G4CsvAnalysisManager::GetNtupleDColumn(G4int id) const
124 {
125  std::map<G4int, tools::wcsv::ntuple::column<double>* >::const_iterator it
126  = fNtupleDColumnMap.find(id);
127  if ( it == fNtupleDColumnMap.end() ) {
128  G4ExceptionDescription description;
129  description << " " << "column " << id << " does not exist.";
130  G4Exception("G4CsvAnalysisManager::GetNtupleDColumn()",
131  "Analysis_W009", JustWarning, description);
132  return 0;
133  }
134 
135  return it->second;
136 }
137 
138 //_____________________________________________________________________________
139 G4bool G4CsvAnalysisManager::Reset()
140 {
141  delete fNtuple;
142  fNtuple = 0;
143 
144  return true;
145 }
146 
147 //_____________________________________________________________________________
148 void G4CsvAnalysisManager::ExceptionForHistograms(
149  const G4String& functionName) const
150 {
151  G4String inFunction = "G4CsvAnalysisManager::";
152  inFunction += functionName;
153  G4ExceptionDescription description;
154  description << " "
155  << "Histograms are not supported." ;
156  G4Exception(inFunction, "Analysis_W005", JustWarning, description);
157 }
158 
159 //
160 // protected methods
161 //
162 
163 //_____________________________________________________________________________
164 G4bool G4CsvAnalysisManager::WriteOnAscii(std::ofstream& /*output*/)
165 {
166 // Write selected objects on ASCII file
167 // To be added: ntuple
168 
169  return true;
170 }
171 
172 //
173 // public methods
174 //
175 
176 //_____________________________________________________________________________
178 {
179  // Keep file name
180  fFileName = fileName;
181 
182  // Add file extension .csv if no extension is given
183  G4String name(fileName);
184  if ( name.find(".") == std::string::npos ) {
185  name.append(".");
186  name.append(GetFileType());
187  }
188 
189 #ifdef G4VERBOSE
190  if ( fpVerboseL4 )
191  fpVerboseL4->Message("open", "analysis file", name);
192 #endif
193 
194  // delete a previous file if it exists
195  if ( fFile ) delete fFile;
196 
197  fFile = new std::ofstream(name);
198  if ( fFile->fail() ) {
199  G4ExceptionDescription description;
200  description << " " << "Cannot open file " << fileName;
201  G4Exception("G4CsvAnalysisManager::OpenFile()",
202  "Analysis_W001", JustWarning, description);
203  return false;
204  }
205 
206  // Create ntuple if it was already booked
207  if ( fNtupleBooking && ( ! fNtuple ) ) {
208 #ifdef G4VERBOSE
209  if ( fpVerboseL4 )
210  fpVerboseL4->Message("create from booking", "ntuple", name);
211 #endif
212  fNtuple = new tools::wcsv::ntuple(*fFile, G4cerr, *fNtupleBooking);
213  if ( fNtupleBooking->m_columns.size() ) {
214  // store ntuple columns in local maps
215  const std::vector<tools::ntuple_booking::col_t>& columns
216  = fNtupleBooking->m_columns;
217  std::vector<tools::ntuple_booking::col_t>::const_iterator it;
218  G4int index = 0;
219  for ( it = columns.begin(); it!=columns.end(); ++it) {
220  if ( (*it).second == tools::_cid(int(0) ) ) {
221  G4cout << "adding int " << fNtuple->find_column<int>((*it).first) << G4endl;
222  fNtupleIColumnMap[index++] = fNtuple->find_column<int>((*it).first);
223  }
224  else if( (*it).second == tools::_cid(float(0) ) ) {
225  fNtupleFColumnMap[index++] = fNtuple->find_column<float>((*it).first);
226  }
227  else if((*it).second== tools::_cid(double(0))) {
228  fNtupleDColumnMap[index++] = fNtuple->find_column<double>((*it).first);
229  }
230  else {
231  G4ExceptionDescription description;
232  description << " "
233  << "Unsupported column type " << (*it).first;
234  G4Exception("G4CsvAnalysisManager::OpenFile()",
235  "Analysis_W004", JustWarning, description);
236  }
237  }
238  }
239  }
240 
241  fLockFileName = true;
242 
243 #ifdef G4VERBOSE
244  if ( fpVerboseL1 )
245  fpVerboseL1->Message("open", "analysis file", name);
246 #endif
247 
248  return true;
249 }
250 
251 //_____________________________________________________________________________
253 {
254  // nothing to be done for Csv file
255  G4bool result = true;
256 
257  // Write ASCII if activated
258  if ( IsAscii() ) {
259  result = WriteAscii();
260  }
261 
262  return result;
263 }
264 
265 //_____________________________________________________________________________
267 {
268  G4bool result = true;
269 
270 #ifdef G4VERBOSE
271  if ( fpVerboseL4 )
272  fpVerboseL4->Message("close", "file", GetFullFileName());
273 #endif
274 
275  // reset data
276  result = Reset();
277  if ( ! result ) {
278  G4ExceptionDescription description;
279  description << " " << "Resetting data failed";
280  G4Exception("G4CsvAnalysisManager::CloseFile()",
281  "Analysis_W002", JustWarning, description);
282  result = false;
283  }
284 
285  // close file
286  fFile->close();
287  fLockFileName = false;
288 
289 #ifdef G4VERBOSE
290  if ( fpVerboseL1 )
291  fpVerboseL1->Message("close", "file", GetFullFileName());
292 #endif
293 
294  return true;
295 }
296 
297 //_____________________________________________________________________________
299  const G4String& /*title*/,
300  G4int /*nbins*/,
301  G4double /*xmin*/, G4double /*xmax*/,
302  const G4String& /*unitName*/,
303  const G4String& /*fcnName*/)
304 {
305  ExceptionForHistograms("CreateH1");
306  return 0;
307 }
308 
309 //_____________________________________________________________________________
311  const G4String& /*title*/,
312  G4int /*nxbins*/,
313  G4double /*xmin*/, G4double /*xmax*/,
314  G4int /*nybins*/,
315  G4double /*ymin*/, G4double /*ymax*/,
316  const G4String& /*xunitName*/,
317  const G4String& /*yunitName*/,
318  const G4String& /*xfcnName*/,
319  const G4String& /*yfcnName*/)
320 {
321  ExceptionForHistograms("CreateH2");
322  return 0;
323 }
324 
325 //_____________________________________________________________________________
327  G4int /*nbins*/,
328  G4double /*xmin*/, G4double /*xmax*/,
329  const G4String& /*unitName*/,
330  const G4String& /*fcnName*/)
331 {
332  ExceptionForHistograms("SetH1");
333  return false;
334 }
335 
336 //_____________________________________________________________________________
338  G4int /*nxbins*/,
339  G4double /*xmin*/, G4double /*xmax*/,
340  G4int /*nybins*/,
341  G4double /*ymin*/, G4double /*ymax*/,
342  const G4String& /*xunitName*/,
343  const G4String& /*yunitName*/,
344  const G4String& /*xfcnName*/,
345  const G4String& /*yfcnName*/)
346 {
347  ExceptionForHistograms("SetH2");
348  return false;
349 }
350 
351 //_____________________________________________________________________________
353 {
354  ExceptionForHistograms("ScaleH1");
355  return false;
356 }
357 
358 //_____________________________________________________________________________
360 {
361  ExceptionForHistograms("ScaleH2");
362  return false;
363 }
364 
365 //_____________________________________________________________________________
367  const G4String& title)
368 {
369  if ( fNtupleBooking ) {
370  G4ExceptionDescription description;
371  description << " "
372  << "Ntuple already exists. "
373  << "(Only one ntuple is currently supported.)";
374  G4Exception("G4CsvAnalysisManager::CreateNtuple()",
375  "Analysis_W006", JustWarning, description);
376  return;
377  }
378 
379 #ifdef G4VERBOSE
380  if ( fpVerboseL4 )
381  fpVerboseL4->Message("create", "ntuple", name);
382 #endif
383 
384  // Create ntuple booking
385  fNtupleBooking = new tools::ntuple_booking();
386  fNtupleBooking->m_name = name;
387  fNtupleBooking->m_title = title;
388  // ntuple booking object is deleted in destructor
389 
390  // Create ntuple if the file is open
391  if ( fFile ) {
392  fNtuple = new tools::wcsv::ntuple(*fFile);
393  // ntuple object is deleted when closing a file
394  }
395 
396 #ifdef G4VERBOSE
397  if ( fpVerboseL2 )
398  fpVerboseL2->Message("create", "ntuple", name);
399 #endif
400 }
401 
402 //_____________________________________________________________________________
404 {
405 #ifdef G4VERBOSE
406  if ( fpVerboseL4 )
407  fpVerboseL4->Message("create", "ntuple I column", name);
408 #endif
409 
410  if ( ! fNtupleBooking ) {
411  G4ExceptionDescription description;
412  description << " "
413  << "Ntuple has to be created first. ";
414  G4Exception("G4CsvAnalysisManager::CreateNtupleIColumn()",
415  "Analysis_W005", JustWarning, description);
416  return -1;
417  }
418 
419  // Save column info in booking
420  G4int index = fNtupleBooking->m_columns.size();
421  fNtupleBooking->add_column<int>(name);
422 
423  // Create column if ntuple already exists
424  if ( fNtuple ) {
425  tools::wcsv::ntuple::column<int>* column
426  = fNtuple->create_column<int>(name);
427  fNtupleIColumnMap[index] = column;
428  }
429 
431 
432 #ifdef G4VERBOSE
433  if ( fpVerboseL2 )
434  fpVerboseL2->Message("create", "ntuple I column", name);
435 #endif
436 
437  return index + fFirstNtupleColumnId;
438 }
439 
440 //_____________________________________________________________________________
442 {
443 #ifdef G4VERBOSE
444  if ( fpVerboseL4 )
445  fpVerboseL4->Message("create", "ntuple F column", name);
446 #endif
447 
448  if ( ! fNtupleBooking ) {
449  G4ExceptionDescription description;
450  description << " "
451  << "Ntuple has to be created first. ";
452  G4Exception("G4CsvAnalysisManager::CreateNtupleFColumn()",
453  "Analysis_W005", JustWarning, description);
454  return -1;
455  }
456 
457  // Save column info in booking
458  G4int index = fNtupleBooking->m_columns.size();
459  fNtupleBooking->add_column<float>(name);
460 
461  // Create column if ntuple already exists
462  if ( fNtuple ) {
463  tools::wcsv::ntuple::column<float>* column
464  = fNtuple->create_column<float>(name);
465  fNtupleFColumnMap[index] = column;
466  }
467 
469 
470 #ifdef G4VERBOSE
471  if ( fpVerboseL2 )
472  fpVerboseL2->Message("create", "ntuple F column", name);
473 #endif
474 
475  return index + fFirstNtupleColumnId;
476 }
477 
478 //_____________________________________________________________________________
480 {
481 #ifdef G4VERBOSE
482  if ( fpVerboseL4 )
483  fpVerboseL4->Message("create", "ntuple D column", name);
484 #endif
485 
486  if ( ! fNtupleBooking ) {
487  G4ExceptionDescription description;
488  description << " "
489  << "Ntuple has to be created first. ";
490  G4Exception("G4CsvAnalysisManager::CreateNtupleDColumn()",
491  "Analysis_W005", JustWarning, description);
492  return -1;
493  }
494 
495  // Save column info in booking
496  G4int index = fNtupleBooking->m_columns.size();
497  fNtupleBooking->add_column<double>(name);
498 
499  // Create column if ntuple already exists
500  if ( fNtuple ) {
501  tools::wcsv::ntuple::column<double>* column
502  = fNtuple->create_column<double>(name);
503  fNtupleDColumnMap[index] = column;
504  }
505 
507 
508 #ifdef G4VERBOSE
509  if ( fpVerboseL2 )
510  fpVerboseL2->Message("create", "ntuple D column", name);
511 #endif
512 
513  return index + fFirstNtupleColumnId;
514 }
515 
516 //_____________________________________________________________________________
518 {
519  // nothing to be done here
520 }
521 
522 
523 //_____________________________________________________________________________
525  G4double /*value*/, G4double /*weight*/)
526 {
527  G4ExceptionDescription description;
528  description << " "
529  << "Histograms are not supported." ;
530  G4Exception("G4CsvAnalysisManager::FillH1()",
531  "Analysis_W007", JustWarning, description);
532  return false;
533 }
534 
535 //_____________________________________________________________________________
537  G4double /*xvalue*/, G4double /*yvalue*/,
538  G4double /*weight*/)
539 {
540  G4ExceptionDescription description;
541  description << " "
542  << "Histograms are not supported." ;
543  G4Exception("G4CsvAnalysisManager::FillH2()",
544  "Analysis_W007", JustWarning, description);
545  return false;
546 }
547 
548 //_____________________________________________________________________________
550 {
551  tools::wcsv::ntuple::column<int>* column = GetNtupleIColumn(id);
552  if ( ! column ) {
553  G4ExceptionDescription description;
554  description << " " << "column " << id << " does not exist.";
555  G4Exception("G4CsvAnalysisManager::FillNtupleIColumn()",
556  "Analysis_W009", JustWarning, description);
557  return false;
558  }
559 
560  column->fill(value);
561 #ifdef G4VERBOSE
562  if ( fpVerboseL4 ) {
563  G4ExceptionDescription description;
564  description << " id " << id << " value " << value;
565  fpVerboseL4->Message("fill", "ntuple I column", description);
566  }
567 #endif
568  return true;
569 }
570 //_____________________________________________________________________________
572 {
573  tools::wcsv::ntuple::column<float>* column = GetNtupleFColumn(id);
574  if ( ! column ) {
575  G4ExceptionDescription description;
576  description << " " << "column " << id << " does not exist.";
577  G4Exception("G4CsvAnalysisManager::FillNtupleFColumn()",
578  "Analysis_W009", JustWarning, description);
579  return false;
580  }
581 
582  column->fill(value);
583 #ifdef G4VERBOSE
584  if ( fpVerboseL4 ) {
585  G4ExceptionDescription description;
586  description << " id " << id << " value " << value;
587  fpVerboseL4->Message("fill", "ntuple F column", description);
588  }
589 #endif
590  return true;
591 }
592 
593 //_____________________________________________________________________________
595 {
596  tools::wcsv::ntuple::column<double>* column = GetNtupleDColumn(id);
597  if ( ! column ) {
598  G4ExceptionDescription description;
599  description << " " << "column " << id << " does not exist.";
600  G4Exception("G4CsvAnalysisManager::FillNtupleDColumn()",
601  "Analysis_W009", JustWarning, description);
602  return false;
603  }
604 
605  column->fill(value);
606 #ifdef G4VERBOSE
607  if ( fpVerboseL4 ) {
608  G4ExceptionDescription description;
609  description << " id " << id << " value " << value;
610  fpVerboseL4->Message("fill", "ntuple D column", description);
611  }
612 #endif
613  return true;
614 }
615 
616 //_____________________________________________________________________________
618 {
619 #ifdef G4VERBOSE
620  if ( fpVerboseL4 )
621  fpVerboseL4->Message("add", "ntuple row", "");
622 #endif
623 
624  if ( ! fNtuple ) {
625  G4ExceptionDescription description;
626  description << " " << "ntuple does not exist. ";
627  G4Exception("G4CsvAnalysisManager::AddNtupleRow()",
628  "Analysis_W008", JustWarning, description);
629  return false;
630  }
631 
632  fNtuple->add_row();
633 #ifdef G4VERBOSE
634  if ( fpVerboseL4 )
635  fpVerboseL4->Message("add", "ntuple row", "");
636 #endif
637 
638  return true;
639 }
640 
641 //_____________________________________________________________________________
643 {
644  return fNtuple;
645 }
646 
647 
648 //_____________________________________________________________________________
650 {
651  ExceptionForHistograms("GetH1Nbins");
652  return 0;
653 }
654 
655 //_____________________________________________________________________________
657 {
658  ExceptionForHistograms("GetH1Xmin");
659  return 0;
660 }
661 
662 //_____________________________________________________________________________
664 {
665  ExceptionForHistograms("GetH1Xmax");
666  return 0;
667 }
668 
669 //_____________________________________________________________________________
671 {
672  ExceptionForHistograms("GetH1Xwidth");
673  return 0;
674 }
675 
676 //_____________________________________________________________________________
678 {
679  ExceptionForHistograms("GetH2NXbins");
680  return 0;
681 }
682 
683 //_____________________________________________________________________________
685 {
686  ExceptionForHistograms("GetH2Xmin");
687  return 0;
688 }
689 
690 //_____________________________________________________________________________
692 {
693  ExceptionForHistograms("GetH2Xmin");
694  return 0;
695 }
696 
697 //_____________________________________________________________________________
699 {
700  ExceptionForHistograms("GetH2XWidth");
701  return 0;
702 }
703 
704 //_____________________________________________________________________________
706 {
707  ExceptionForHistograms("GetH2NYbins");
708  return 0;
709 }
710 
711 //_____________________________________________________________________________
713 {
714  ExceptionForHistograms("GetH2Ymin");
715  return 0;
716 }
717 
718 //_____________________________________________________________________________
720 {
721  ExceptionForHistograms("GetH2Ymax");
722  return 0;
723 }
724 
725 //_____________________________________________________________________________
727 {
728  ExceptionForHistograms("GetH2YWidth");
729  return 0;
730 }
731 
732 //_____________________________________________________________________________
734  const G4String& /*title*/)
735 {
736  ExceptionForHistograms("SetH1Title");
737  return false;
738 }
739 
740 //_____________________________________________________________________________
742  const G4String& /*title*/)
743 {
744  ExceptionForHistograms("SetH1XAxisTitle");
745  return false;
746 }
747 
748 //_____________________________________________________________________________
750  const G4String& /*title*/)
751 {
752  ExceptionForHistograms("SetH1YAxisTitle");
753  return false;
754 }
755 
756 //_____________________________________________________________________________
758  const G4String& /*title*/)
759 {
760  ExceptionForHistograms("SetH2Title");
761  return false;
762 }
763 
764 //_____________________________________________________________________________
766  const G4String& /*title*/)
767 {
768  ExceptionForHistograms("SetH2XAxisTitle");
769  return false;
770 }
771 
772 //_____________________________________________________________________________
774  const G4String& /*title*/)
775 {
776  ExceptionForHistograms("SetH2YAxisTitle");
777  return false;
778 }
779 
780 //_____________________________________________________________________________
782  const G4String& /*title*/)
783 {
784  ExceptionForHistograms("SetH2ZAxisTitle");
785  return false;
786 }
787 
788 //_____________________________________________________________________________
790 {
791  ExceptionForHistograms("GetH1XAxisTitle");
792  return "";
793 }
794 
795 //_____________________________________________________________________________
797 {
798  ExceptionForHistograms("GetH1Title");
799  return "";
800 }
801 
802 //_____________________________________________________________________________
804 {
805  ExceptionForHistograms("GetH1YAxisTitle");
806  return "";
807 }
808 
809 
810 //_____________________________________________________________________________
812 {
813  ExceptionForHistograms("GetH2Title");
814  return "";
815 }
816 
817 //_____________________________________________________________________________
819 {
820  ExceptionForHistograms("GetH2XAxisTitle");
821  return "";
822 }
823 
824 //_____________________________________________________________________________
826 {
827  ExceptionForHistograms("GetH2YAxisTitle");
828  return "";
829 }
830 
831 //_____________________________________________________________________________
833 {
834  ExceptionForHistograms("GetH2ZAxisTitle");
835  return "";
836 }
837