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