Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExG4HbookAnalysisManager.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 //
30 
31 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr)
32 
33 #ifdef G4_USE_HBOOK
34 
35 #include "ExG4HbookAnalysisManager.hh"
36 #include "G4UnitsTable.hh"
37 
38 #include <iostream>
39 
40 extern "C" int setpawc();
41 extern "C" int setntuc();
42 
43 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::fgInstance = 0;
44 const G4int ExG4HbookAnalysisManager::fgkDefaultH2HbookIdOffset = 100;
45 const G4int ExG4HbookAnalysisManager::fgkDefaultNtupleHbookId = 1;
46 const G4String ExG4HbookAnalysisManager::fgkDefaultNtupleDirectoryName = "ntuple";
47 
48 //_____________________________________________________________________________
49 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Instance()
50 {
51  if ( fgInstance == 0 ) {
52  fgInstance = new ExG4HbookAnalysisManager();
53  }
54 
55  return fgInstance;
56 }
57 
58 //_____________________________________________________________________________
59 ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()
60  : G4VAnalysisManager("Hbook"),
61  fH1HbookIdOffset(-1),
62  fH2HbookIdOffset(-1),
63  fNtupleHbookId(-1),
64  fFile(0),
65  fH1Vector(),
66  fH2Vector(),
67  fH1BookingVector(),
68  fH2BookingVector(),
69  fH1NameIdMap(),
70  fH2NameIdMap(),
71  fNtuple(0),
72  fNtupleBooking(0),
73  fNtupleIColumnMap(),
74  fNtupleFColumnMap(),
75  fNtupleDColumnMap()
76 {
77  if ( fgInstance ) {
78  G4ExceptionDescription description;
79  description << " "
80  << "G4HbookAnalysisManager already exists."
81  << "Cannot create another instance.";
82  G4Exception("G4HbookAnalysisManager::G4HbookAnalysisManager()",
83  "Analysis_F001", FatalException, description);
84  }
85 
86  fgInstance = this;
87 
88  // Initialize HBOOK :
89  tools::hbook::CHLIMIT(setpawc());
90  setntuc(); //for ntuple.
91 }
92 
93 //_____________________________________________________________________________
94 ExG4HbookAnalysisManager::~ExG4HbookAnalysisManager()
95 {
96  // Delete h1, h2, ntuple
97  Reset();
98 
99  // Delete h1, h2 booking
100  std::vector<h1_booking*>::iterator it;
101  for ( it = fH1BookingVector.begin(); it != fH1BookingVector.end(); it++ ) {
102  delete *it;
103  }
104  std::vector<h2_booking*>::iterator it2;
105  for ( it2 = fH2BookingVector.begin(); it2 != fH2BookingVector.end(); it2++ ) {
106  delete *it2;
107  }
108 
109  delete fNtupleBooking;
110  delete fFile;
111 
112  fgInstance = 0;
113 }
114 
115 //
116 // private methods
117 //
118 
119 //_____________________________________________________________________________
120 void ExG4HbookAnalysisManager::SetH1HbookIdOffset()
121 {
122 // Set fH1HbookIdOffset if needed
123 
124  if ( fH1HbookIdOffset == -1 ) {
125  if ( fFirstHistoId > 0 )
126  fH1HbookIdOffset = 0;
127  else
128  fH1HbookIdOffset = 1;
129 
130  if ( fH1HbookIdOffset > 0 ) {
131  G4ExceptionDescription description;
132  description << "H1 will be defined in HBOOK with ID = G4_firstHistoId + 1";
133  G4Exception("ExG4HbookAnalysisManager::SetH1HbookIdOffset()",
134  "Analysis_W011", JustWarning, description);
135  }
136  }
137 }
138 
139 //_____________________________________________________________________________
140 void ExG4HbookAnalysisManager::SetH2HbookIdOffset()
141 {
142 // Set fH2HbookIdOffset if needed
143 
144  if ( fH2HbookIdOffset == -1 ) {
145  if ( fFirstHistoId > 0 )
146  fH2HbookIdOffset = 0;
147  else
148  fH2HbookIdOffset = 1;
149 
150  if ( fH2HbookIdOffset > 0 ) {
151  G4ExceptionDescription description;
152  description << "H2 will be defined in HBOOK with ID = G4_firstHistoId + 1";
153  G4Exception("ExG4HbookAnalysisManager::SetH1HbookIdOffset",
154  "Analysis_W011", JustWarning, description);
155  }
156  }
157 }
158 
159 //_____________________________________________________________________________
160 void ExG4HbookAnalysisManager::CreateH1FromBooking()
161 {
162 // Create h1 from h1_booking.
163 
164  // Do nothing if any h1 histogram already exists
165  // or no h1 histograms are booked
166  if ( fH1Vector.size() || ( fH1BookingVector.size() == 0 ) ) return;
167 
168  // Go to histograms directory if defined
169  if ( fHistoDirectoryName != "" ) {
170  G4String histoPath = "//PAWC/LUN1/";
171  histoPath.append(fHistoDirectoryName.data());
172  tools::hbook::CHCDIR(histoPath.data()," ");
173  }
174 
175  // Create histograms
176  G4int index = 0;
177  std::vector<h1_booking*>::const_iterator it;
178  for ( it = fH1BookingVector.begin(); it != fH1BookingVector.end(); ++it) {
179  // Get information
180  G4int id = index + fFirstHistoId;
181  G4HnInformation* info = GetH1Information(id);
182  // Hbook index
183  G4int hbookIndex = fH1HbookIdOffset + index + fFirstHistoId;
184  ++index;
185 
186 #ifdef G4VERBOSE
187  if ( fpVerboseL4 )
188  fpVerboseL4->Message("create from booking", "h1", info->fName);
189 #endif
190 
191  // Create h1
193  = new tools::hbook::h1(hbookIndex, (*it)->fTitle,
194  (*it)->fNbins, (*it)->fXmin, (*it)->fXmax);
195  fH1Vector.push_back(h1);
196 
197 #ifdef G4VERBOSE
198  if ( fpVerboseL3 ) {
199  G4ExceptionDescription description;
200  description << " name : " << info->fName << " hbook index : " << hbookIndex;
201  fpVerboseL3->Message("create from booking", "h1", description);
202  }
203 #endif
204  }
205 
206  if ( fHistoDirectoryName != "" ) {
207  // Return to //PAWC/LUN1 :
208  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
209  }
210 }
211 
212 //_____________________________________________________________________________
213 void ExG4HbookAnalysisManager::CreateH2FromBooking()
214 {
215 // Create h2 from h2_booking.
216 
217  // Do nothing if any h2 histogram already exists
218  // or no h2 histograms are booked
219  if ( fH2Vector.size() || ( fH2BookingVector.size() == 0 ) ) return;
220 
221  // Go to histograms directory
222  if ( fHistoDirectoryName != "" ) {
223  G4String histoPath = "//PAWC/LUN1/";
224  histoPath.append(fHistoDirectoryName.data());
225  tools::hbook::CHCDIR(histoPath.data()," ");
226  }
227 
228  // Create histograms
229  G4int index = 0;
230  std::vector<h2_booking*>::const_iterator it;
231  for ( it = fH2BookingVector.begin(); it != fH2BookingVector.end(); ++it) {
232  // Get information
233  G4int id = index + fFirstHistoId;
234  G4HnInformation* info = GetH2Information(id);
235  // Hbook index
236  G4int hbookIndex = fH2HbookIdOffset + index + fFirstHistoId;
237  ++index;
238 
239 #ifdef G4VERBOSE
240  if ( fpVerboseL3 )
241  fpVerboseL3->Message("create from booking", "h2", info->fName);
242 #endif
243 
244  // Create h2
246  = new tools::hbook::h2(hbookIndex, (*it)->fTitle,
247  (*it)->fNxbins, (*it)->fXmin, (*it)->fXmax,
248  (*it)->fNybins, (*it)->fYmin, (*it)->fYmax);
249  fH2Vector.push_back(h2);
250 
251 #ifdef G4VERBOSE
252  if ( fpVerboseL3 ) {
253  G4ExceptionDescription description;
254  description << " name : " << info->fName << " hbook index : " << hbookIndex;
255  fpVerboseL3->Message("create from booking", "h2", description);
256  }
257 #endif
258  }
259 
260  if ( fHistoDirectoryName != "" ) {
261  // Return to //PAWC/LUN1 :
262  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
263  }
264 }
265 
266 //_____________________________________________________________________________
267 void ExG4HbookAnalysisManager::CreateNtupleFromBooking()
268 {
269 // Create ntuple from ntuple_booking.
270 
271  if ( fNtuple || (! fNtupleBooking) ) return;
272 
273 #ifdef G4VERBOSE
274  if ( fpVerboseL4 )
275  fpVerboseL4->Message("create from booking", "ntuple", fNtupleBooking->m_name);
276 #endif
277 
278  // Create an "ntuple" directory both in memory and in the file
279  fFile->cd_home(); //go under //PAWC/LUN1
280  if ( fNtupleDirectoryName == "" )
281  fFile->mkcd(fgkDefaultNtupleDirectoryName.data());
282  else
283  fFile->mkcd(fNtupleDirectoryName.data());
284  fLockNtupleDirectoryName = true;
285 
286  // Define ntuple ID in HBOOK
287  if ( fNtupleHbookId == -1 ) fNtupleHbookId = fgkDefaultNtupleHbookId;
288 
289  // We should be under //PAWC/LUN1/ntuple
290  fNtuple = new tools::hbook::wntuple(fNtupleHbookId, G4cout, *fNtupleBooking);
291  if ( fNtupleBooking->m_columns.size() ) {
292  // store ntuple columns in local maps
293  const std::vector<tools::ntuple_booking::col_t>& columns
294  = fNtupleBooking->m_columns;
295  std::vector<tools::ntuple_booking::col_t>::const_iterator it;
296  G4int index = 0;
297  for ( it = columns.begin(); it!=columns.end(); ++it) {
298  if ( (*it).second == tools::_cid(int(0) ) ) {
299  G4cout << "adding int " << fNtuple->find_column<int>((*it).first) << G4endl;
300  fNtupleIColumnMap[index++] = fNtuple->find_column<int>((*it).first);
301  }
302  else if( (*it).second == tools::_cid(float(0) ) ) {
303  fNtupleFColumnMap[index++] = fNtuple->find_column<float>((*it).first);
304  }
305  else if((*it).second== tools::_cid(double(0))) {
306  fNtupleDColumnMap[index++] = fNtuple->find_column<double>((*it).first);
307  }
308  else {
309  G4ExceptionDescription description;
310  description << " "
311  << "Unsupported column type " << (*it).first;
312  G4Exception("G4HbookAnalysisManager::CreateNtupleFromBooking()",
313  "Analysis_W004", JustWarning, description);
314  }
315  }
316  }
317  FinishNtuple();
318 
319 #ifdef G4VERBOSE
320  if ( fpVerboseL3 )
321  fpVerboseL3->Message("create from booking", "ntuple", fNtupleBooking->m_name);
322 #endif
323 }
324 
325 //_____________________________________________________________________________
326 tools::hbook::wntuple::column<int>*
327 ExG4HbookAnalysisManager::GetNtupleIColumn(G4int id) const
328 {
329  std::map<G4int, tools::hbook::wntuple::column<int>* >::const_iterator it
330  = fNtupleIColumnMap.find(id);
331  if ( it == fNtupleIColumnMap.end() ) {
332  G4ExceptionDescription description;
333  description << " " << "column " << id << " does not exist.";
334  G4Exception("G4HbookAnalysisManager::GetNtupleIColumn()",
335  "Analysis_W009", JustWarning, description);
336  return 0;
337  }
338 
339  return it->second;
340 }
341 
342 //_____________________________________________________________________________
343 tools::hbook::wntuple::column<float>*
344 ExG4HbookAnalysisManager::GetNtupleFColumn(G4int id) const
345 {
346  std::map<G4int, tools::hbook::wntuple::column<float>* >::const_iterator it
347  = fNtupleFColumnMap.find(id);
348  if ( it == fNtupleFColumnMap.end() ) {
349  G4ExceptionDescription description;
350  description << " " << "column " << id << " does not exist.";
351  G4Exception("G4HbookAnalysisManager::GetNtupleFColumn()",
352  "Analysis_W009", JustWarning, description);
353  return 0;
354  }
355 
356  return it->second;
357 }
358 
359 //_____________________________________________________________________________
361 {
362 // Reset histograms and ntuple
363 
364  // Delete histograms
365  std::vector<tools::hbook::h1*>::iterator it;
366  for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
367  delete *it;
368  }
369 
370  std::vector<tools::hbook::h2*>::iterator it2;
371  for (it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
372  delete *it2;
373  }
374 
375  // Clear vectors
376  fH1Vector.clear();
377  fH2Vector.clear();
378 
379  // Delete ntuple
380  delete fNtuple;
381  fNtuple = 0;
382 }
383 
384 //_____________________________________________________________________________
385 void ExG4HbookAnalysisManager::UpdateTitle(G4String& title,
386  const G4String& unitName,
387  const G4String& fcnName) const
388 {
389 // The units are not within [ ] as this causes strange font effect in
390 // browsing the hbook file in PAW
391 
392  if ( fcnName != "none" ) { title += " "; title += fcnName; title += "("; }
393  if ( unitName != "none" ) { title += " "; title += unitName; title += " ";}
394  if ( fcnName != "none" ) { title += ")"; }
395 }
396 
397 //_____________________________________________________________________________
398 h1_booking* ExG4HbookAnalysisManager::GetH1Booking(G4int id, G4bool warn) const
399 {
400  G4int index = id - fFirstHistoId;
401  if ( index < 0 || index >= G4int(fH1BookingVector.size()) ) {
402  if ( warn) {
403  G4ExceptionDescription description;
404  description << " " << "histo " << id << " does not exist.";
405  G4Exception("G4HbookAnalysisManager::GetH1Booking()",
406  "Analysis_W007", JustWarning, description);
407  }
408  return 0;
409  }
410 
411  return fH1BookingVector[index];
412 }
413 
414 //_____________________________________________________________________________
415 h2_booking* ExG4HbookAnalysisManager::GetH2Booking(G4int id, G4bool warn) const
416 {
417  G4int index = id - fFirstHistoId;
418  if ( index < 0 || index >= G4int(fH2BookingVector.size()) ) {
419  if ( warn) {
420  G4ExceptionDescription description;
421  description << " " << "histo " << id << " does not exist.";
422  G4Exception("G4HbookAnalysisManager::GetH2Booking()",
423  "Analysis_W007", JustWarning, description);
424  }
425  return 0;
426  }
427 
428  return fH2BookingVector[index];
429 }
430 
431 //
432 // protected methods
433 //
434 
435 //_____________________________________________________________________________
436 G4bool ExG4HbookAnalysisManager::WriteOnAscii(std::ofstream& output)
437 {
438 // Write selected objects on ASCII file
439 // (Only H1 implemented by now)
440 // According to the implementation by Michel Maire, originally in
441 // extended examples.
442 
443  // h1 histograms
444  for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
445  G4int id = i + fFirstHistoId;
446  G4HnInformation* info = GetH1Information(id);
447  // skip writing if activation is enabled and H1 is inactivated
448  if ( ! info->fAscii ) continue;
449  tools::hbook::h1* h1 = fH1Vector[i];
450 
451 #ifdef G4VERBOSE
452  if ( fpVerboseL3 )
453  fpVerboseL3->Message("write on ascii", "h1", info->fName);
454 #endif
455 
456  output << "\n 1D histogram " << id << ": " << h1->title()
457  << "\n \n \t X \t\t Y" << G4endl;
458 
459  for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
460  output << " " << j << "\t"
461  << h1->axis().bin_center(j) << "\t"
462  << h1->bin_height(j) << G4endl;
463  }
464  }
465 
466  return true;
467 }
468 
469 //_____________________________________________________________________________
470 tools::hbook::h1* ExG4HbookAnalysisManager::GetH1InFunction(G4int id,
471  G4String functionName, G4bool warn,
472  G4bool onlyIfActive) const
473 {
474  G4int index = id - fFirstHistoId;
475  if ( index < 0 || index >= G4int(fH1Vector.size()) ) {
476  if ( warn) {
477  G4String inFunction = "ExG4HbookAnalysisManager::";
478  inFunction += functionName;
479  G4ExceptionDescription description;
480  description << " " << "histogram " << id << " does not exist.";
481  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
482  }
483  return 0;
484  }
485 
486  // Do not return histogram if inactive
487  if ( fActivation && onlyIfActive && ( ! GetActivation(kH1, id) ) ) {
488  return 0;
489  }
490 
491  return fH1Vector[index];
492 }
493 
494 //_____________________________________________________________________________
495 tools::hbook::h2* ExG4HbookAnalysisManager::GetH2InFunction(G4int id,
496  G4String functionName, G4bool warn,
497  G4bool onlyIfActive) const
498 {
499  G4int index = id - fFirstHistoId;
500  if ( index < 0 || index >= G4int(fH2Vector.size()) ) {
501  if ( warn) {
502  G4String inFunction = "ExG4HbookAnalysisManager::";
503  inFunction += functionName;
504  G4ExceptionDescription description;
505  description << " " << "histogram " << id << " does not exist.";
506  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
507  }
508  return 0;
509  }
510 
511  // Do not return histogram if inactive
512  if ( fActivation && onlyIfActive && ( ! GetActivation(kH2, id) ) ) {
513  return 0;
514  }
515 
516  return fH2Vector[index];
517 }
518 
519 //_____________________________________________________________________________
520 tools::hbook::wntuple::column<double>*
521 ExG4HbookAnalysisManager::GetNtupleDColumn(G4int id) const
522 {
523  std::map<G4int, tools::hbook::wntuple::column<double>* >::const_iterator it
524  = fNtupleDColumnMap.find(id);
525  if ( it == fNtupleDColumnMap.end() ) {
526  G4ExceptionDescription description;
527  description << " " << "column " << id << " does not exist.";
528  G4Exception("G4HbookAnalysisManager::GetNtupleDColumn()",
529  "Analysis_W009", JustWarning, description);
530  return 0;
531  }
532 
533  return it->second;
534 }
535 
536 //
537 // public methods
538 //
539 
540 //_____________________________________________________________________________
541 G4bool ExG4HbookAnalysisManager::OpenFile(const G4String& fileName)
542 {
543  // Keep file name
544  fFileName = fileName;
545 
546  // Add file extension .root if no extension is given
547  G4String name(fileName);
548  if ( name.find(".") == std::string::npos ) {
549  name.append(".");
550  name.append(GetFileType());
551  }
552 
553 #ifdef G4VERBOSE
554  if ( fpVerboseL4 )
555  fpVerboseL4->Message("open", "analysis file", name);
556 #endif
557 
558  // delete a previous file if it exists
559  if ( fFile ) delete fFile;
560 
561  tools::hbook::CHCDIR("//PAWC"," ");
562 
563  unsigned int unit = 1;
564  fFile = new tools::hbook::wfile(std::cout, name, unit);
565  if ( ! fFile->is_valid() ) {
566  G4ExceptionDescription description;
567  description << " " << "Cannot open file " << fileName;
568  G4Exception("G4HbookAnalysisManager::OpenFile()",
569  "Analysis_W001", JustWarning, description);
570  return false;
571  }
572 
573  // At this point, in HBOOK, we should have :
574  // - created a //LUN1 directory attached to the file
575  // - created a //PAWC/LUN1 in memory
576  // - be in the directory //PAWC/LUN1.
577 
578  // create an "histo" HBOOK directory both in memory and in the file :
579  if ( fHistoDirectoryName != "" ) {
580  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
581  tools::hbook::CHMDIR(fHistoDirectoryName.data()," ");
582  tools::hbook::CHCDIR("//LUN1"," ");
583  tools::hbook::CHMDIR(fHistoDirectoryName.data()," ");
584  }
585  // the five upper lines could have been done with :
586  //fFile->cd_home();
587  //fFile->mkcd("histo");
588 
589  // Create h1 histrograms if any is booked
590  if ( ( fH1Vector.size() == 0 ) && ( fH1BookingVector.size() ) )
591  CreateH1FromBooking();
592 
593  // Create h2 histrograms if any is booked
594  if ( ( fH2Vector.size() == 0 ) && ( fH2BookingVector.size() ) )
595  CreateH2FromBooking();
596 
597  // Create ntuple if it is booked
598  if ( fNtupleBooking && ( ! fNtuple ) )
599  CreateNtupleFromBooking();
600 
601  fLockFileName = true;
602  fLockHistoDirectoryName = true;
603 
604 #ifdef G4VERBOSE
605  if ( fpVerboseL1 )
606  fpVerboseL1->Message("open", "analysis file", name);
607 #endif
608 
609  return true;
610 }
611 
612 //_____________________________________________________________________________
613 G4bool ExG4HbookAnalysisManager::Write()
614 {
615 #ifdef G4VERBOSE
616  if ( fpVerboseL4 )
617  fpVerboseL4->Message("write", "file", GetFullFileName());
618 #endif
619 
620  // ntuple
621  //if ( fNtuple ) fNtuple->add_row_end();
622 
623  // Return to //PAWC/LUN1 :
624  //tools::hbook::CHCDIR("//PAWC/LUN1"," ");
625  G4bool result = fFile->write();
626 
627 #ifdef G4VERBOSE
628  if ( fpVerboseL1 )
629  fpVerboseL1->Message("write", "file", GetFullFileName(), result);
630 #endif
631 
632  // Write ASCII if activated
633  if ( IsAscii() ) {
634  G4bool result2 = WriteAscii();
635  result = result && result2;
636  }
637 
638  return result;
639 }
640 
641 //_____________________________________________________________________________
642 G4bool ExG4HbookAnalysisManager::CloseFile()
643 {
644 #ifdef G4VERBOSE
645  if ( fpVerboseL4 )
646  fpVerboseL4->Message("close", "file", GetFullFileName());
647 #endif
648 
649  // reset data
650  Reset();
651 
652  // close file
653  G4bool result = fFile->close();
654  fLockFileName = false;
655 
656 #ifdef G4VERBOSE
657  if ( fpVerboseL1 )
658  fpVerboseL1->Message("close", "file", GetFullFileName(), result);
659 #endif
660 
661  return result;
662 }
663 
664 //_____________________________________________________________________________
665 G4int ExG4HbookAnalysisManager::CreateH1(const G4String& name, const G4String& title,
666  G4int nbins, G4double xmin, G4double xmax,
667  const G4String& unitName, const G4String& fcnName)
668 {
669 #ifdef G4VERBOSE
670  if ( fpVerboseL4 )
671  fpVerboseL4->Message("create", "H1", name);
672 #endif
673 
674  // Create h1 booking & information
675  G4int index = fH1BookingVector.size();
676  G4double unit = GetUnitValue(unitName);
677  G4Fcn fcn = GetFunction(fcnName);
678  G4String newTitle(title);
679  UpdateTitle(newTitle, unitName, fcnName);
680  h1_booking* h1Booking = new h1_booking(nbins, fcn(xmin), fcn(xmax));
681  // h1_booking object is deleted in destructor
682  h1Booking->fTitle = newTitle;
683  fH1BookingVector.push_back(h1Booking);
684  AddH1Information(name, unitName, fcnName, unit, fcn);
685 
686  // Set fH1HbookIdOffset if needed
687  SetH1HbookIdOffset();
688 
689  // Hbook index
690  G4int hbookIndex = fH1HbookIdOffset + index + fFirstHistoId;
691 
692  // Create h1 if the file is open
693  if ( fFile) {
694  // Go to histograms directory
695  G4String histoPath = "//PAWC/LUN1/";
696  if ( fHistoDirectoryName != "" ) {
697  histoPath.append(fHistoDirectoryName.data());
698  tools::hbook::CHCDIR(histoPath.data()," ");
699  }
700  tools::hbook::CHCDIR(histoPath.data()," ");
701 
702  // Create histogram
703  tools::hbook::h1* h1
704  = new tools::hbook::h1(hbookIndex, newTitle, nbins, fcn(xmin), fcn(xmax));
705  // h1 objects are deleted when closing a file.
706  fH1Vector.push_back(h1);
707 
708  if ( fHistoDirectoryName != "" ) {
709  // Return to //PAWC/LUN1 :
710  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
711  }
712  }
713 
714  fLockFirstHistoId = true;
715 
716 #ifdef G4VERBOSE
717  if ( fpVerboseL2 ) {
718  G4ExceptionDescription description;
719  description << " name : " << name << " hbook index : " << hbookIndex;
720  fpVerboseL2->Message("create", "H1", description);
721  }
722 #endif
723 
724  fH1NameIdMap[name] = index + fFirstHistoId;
725  return index + fFirstHistoId;
726 }
727 
728 //_____________________________________________________________________________
729 G4int ExG4HbookAnalysisManager::CreateH2(const G4String& name, const G4String& title,
730  G4int nxbins, G4double xmin, G4double xmax,
731  G4int nybins, G4double ymin, G4double ymax,
732  const G4String& xunitName, const G4String& yunitName,
733  const G4String& xfcnName, const G4String& yfcnName)
734 {
735 #ifdef G4VERBOSE
736  if ( fpVerboseL4 )
737  fpVerboseL4->Message("create", "H2", name);
738 #endif
739 
740  // Create h2 booking & information
741  G4int index = fH2BookingVector.size();
742  G4double xunit = GetUnitValue(xunitName);
743  G4double yunit = GetUnitValue(yunitName);
744  G4Fcn xfcn = GetFunction(xfcnName);
745  G4Fcn yfcn = GetFunction(yfcnName);
746  G4String newTitle(title);
747  UpdateTitle(newTitle, xunitName, xfcnName);
748  UpdateTitle(newTitle, yunitName, yfcnName);
749 
750  h2_booking* h2Booking = new h2_booking(nxbins, xfcn(xmin), xfcn(xmax),
751  nybins, yfcn(ymin), yfcn(ymax));
752  // h2_booking object is deleted in destructor
753  h2Booking->fTitle = newTitle;
754  fH2BookingVector.push_back(h2Booking);
755  AddH2Information(name, xunitName, yunitName, xfcnName, yfcnName,
756  xunit, yunit, xfcn, yfcn);
757 
758  // Set fH1HbookIdOffset if needed
759  SetH2HbookIdOffset();
760 
761  // Hbook index
762  G4int hbookIndex = fH2HbookIdOffset + index + fFirstHistoId;
763 
764  // Create h2 if the file is open
765  if ( fFile) {
766  // Go to histograms directory
767  G4String histoPath = "//PAWC/LUN1/";
768  if ( fHistoDirectoryName != "" ) {
769  histoPath.append(fHistoDirectoryName.data());
770  }
771  tools::hbook::CHCDIR(histoPath.data()," ");
772 
773  // Create histogram
774  tools::hbook::h2* h2
775  = new tools::hbook::h2(hbookIndex, title,
776  nxbins, xfcn(xmin), xfcn(xmax),
777  nybins, yfcn(ymin), yfcn(ymax));
778  // h2 objects are deleted when closing a file.
779  fH2Vector.push_back(h2);
780 
781  // Return to //PAWC/LUN1
782  if ( fHistoDirectoryName != "" ) {
783  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
784  }
785  }
786 
787  fLockFirstHistoId = true;
788 
789 #ifdef G4VERBOSE
790  if ( fpVerboseL2 ) {
791  G4ExceptionDescription description;
792  description << " name : " << name << " hbook index : " << hbookIndex;
793  fpVerboseL2->Message("create", "H2", description);
794  }
795 #endif
796 
797  fH2NameIdMap[name] = index + fFirstHistoId;
798  return index + fFirstHistoId;
799 }
800 
801 //_____________________________________________________________________________
802 G4bool ExG4HbookAnalysisManager::SetH1(G4int id,
803  G4int nbins, G4double xmin, G4double xmax,
804  const G4String& unitName,
805  const G4String& fcnName)
806 {
807  h1_booking* h1Booking = GetH1Booking(id, false);
808  if ( ! h1Booking ) {
809  G4ExceptionDescription description;
810  description << " " << "histogram " << id << " does not exist.";
811  G4Exception("G4HbookAnalysisManager::SetH1()",
812  "Analysis_W007", JustWarning, description);
813  return false;
814  }
815 
816  G4HnInformation* info = GetH1Information(id);
817 #ifdef G4VERBOSE
818  if ( fpVerboseL4 )
819  fpVerboseL4->Message("configure", "H1", info->fName);
820 #endif
821 
822  // Keep new parameters in booking & information
823  G4double unit = GetUnitValue(unitName);
824  G4Fcn fcn = GetFunction(fcnName);
825  h1Booking->fNbins = nbins;
826  h1Booking->fXmin = fcn(xmin);
827  h1Booking->fXmax = fcn(xmax);
828  info->fXUnitName = unitName;
829  info->fYUnitName = unitName;
830  info->fXFcnName = fcnName;
831  info->fYFcnName = fcnName;
832  info->fXUnit = unit;
833  info->fYUnit = unit;
834  info->fXFcn = fcn;
835  info->fYFcn = fcn;
836  SetActivation(kH1, id, true);
837 
838  G4String newTitle(h1Booking->fTitle);
839  UpdateTitle(newTitle, unitName, fcnName);
840  h1Booking->fTitle = newTitle;
841 
842  // Re-configure histogram if it was already defined
843  if ( fH1Vector.size() ) {
844  tools::hbook::h1* h1 = GetH1(id);
845  h1->configure(nbins, fcn(xmin), fcn(xmax));
846  G4cout << " h1 = " << h1 << G4endl;
847  }
848 
849  return true;
850 }
851 
852 //_____________________________________________________________________________
853 G4bool ExG4HbookAnalysisManager::SetH2(G4int id,
854  G4int nxbins, G4double xmin, G4double xmax,
855  G4int nybins, G4double ymin, G4double ymax,
856  const G4String& xunitName, const G4String& yunitName,
857  const G4String& xfcnName, const G4String& yfcnName)
858 {
859  h2_booking* h2Booking = GetH2Booking(id, false);
860  if ( ! h2Booking ) {
861  G4ExceptionDescription description;
862  description << " " << "histogram " << id << " does not exist.";
863  G4Exception("G4HbookAnalysisManager::SetH2()",
864  "Analysis_W007", JustWarning, description);
865  return false;
866  }
867 
868  G4HnInformation* info = GetH2Information(id);
869 #ifdef G4VERBOSE
870  if ( fpVerboseL4 )
871  fpVerboseL4->Message("configure", "H2", info->fName);
872 #endif
873 
874  // Keep new parameters in booking & information
875  G4double xunit = GetUnitValue(xunitName);
876  G4double yunit = GetUnitValue(yunitName);
877  G4Fcn xfcn = GetFunction(xfcnName);
878  G4Fcn yfcn = GetFunction(yfcnName);
879 
880  h2Booking->fNxbins = nxbins;
881  h2Booking->fXmin = xfcn(xmin);
882  h2Booking->fXmax = xfcn(xmax);
883  h2Booking->fNybins = nybins;
884  h2Booking->fYmin = yfcn(ymin);
885  h2Booking->fYmax = yfcn(ymax);
886 
887  info->fXUnitName = xunitName;
888  info->fYUnitName = yunitName;
889  info->fXFcnName = xfcnName;
890  info->fYFcnName = yfcnName;
891  info->fXUnit = xunit;
892  info->fYUnit = yunit;
893  info->fXFcn = xfcn;
894  info->fYFcn = yfcn;
895  SetActivation(kH2, id, true);
896 
897  G4String newTitle(h2Booking->fTitle);
898  UpdateTitle(newTitle, xunitName, xfcnName);
899  UpdateTitle(newTitle, yunitName, yfcnName);
900  h2Booking->fTitle = newTitle;
901 
902  // Re-configure histogram if it was already defined
903  if ( fH2Vector.size() ) {
904  tools::hbook::h2* h2 = GetH2(id);
905  h2->configure(nxbins, xfcn(xmin), xfcn(xmax),
906  nybins, yfcn(ymin), yfcn(ymax));
907  }
908 
909  return true;
910 }
911 
912 //_____________________________________________________________________________
913 G4bool ExG4HbookAnalysisManager::ScaleH1(G4int id, G4double factor)
914 {
915  tools::hbook::h1* h1 = GetH1InFunction(id, "ScaleH1", false, false);
916  if ( ! h1 ) return false;
917 
918  return h1->scale(factor);
919 }
920 
921 //_____________________________________________________________________________
922 G4bool ExG4HbookAnalysisManager::ScaleH2(G4int id, G4double factor)
923 {
924  tools::hbook::h2* h2 = GetH2InFunction(id, "ScaleH2", false, false);
925  if ( ! h2 ) return false;
926 
927  return h2->scale(factor);
928 }
929 
930 //_____________________________________________________________________________
931 void ExG4HbookAnalysisManager::CreateNtuple(const G4String& name,
932  const G4String& title)
933 {
934 #ifdef G4VERBOSE
935  if ( fpVerboseL4 )
936  fpVerboseL4->Message("create", "ntuple", name);
937 #endif
938 
939  if ( fNtupleBooking ) {
940  G4ExceptionDescription description;
941  description << " "
942  << "Ntuple already exists. "
943  << "(Only one ntuple is currently supported.)";
944  G4Exception("G4HbookAnalysisManager::CreateNtuple()",
945  "Analysis_W006", JustWarning, description);
946  return;
947  }
948 
949  // Create an "ntuple" directory both in memory and in the file
950  if ( fFile ) {
951  fFile->cd_home(); //go under //PAWC/LUN1
952  if ( fNtupleDirectoryName == "" )
953  fFile->mkcd(fgkDefaultNtupleDirectoryName.data());
954  else
955  fFile->mkcd(fNtupleDirectoryName.data());
956  fLockNtupleDirectoryName = true;
957  }
958 
959 #ifdef G4VERBOSE
960  if ( fpVerboseL4 )
961  fpVerboseL4->Message("create", "ntuple", name);
962 #endif
963 
964  // Define ntuple ID in HBOOK
965  if ( fNtupleHbookId == -1 ) fNtupleHbookId = fgkDefaultNtupleHbookId;
966 
967  // Create ntuple booking
968  fNtupleBooking = new tools::ntuple_booking();
969  fNtupleBooking->m_name = name;
970  fNtupleBooking->m_title = title;
971  // ntuple booking object is deleted in destructor
972 
973  // Create ntuple if the file is open
974  // We should be under //PAWC/LUN1/ntuple
975  if ( fFile ) {
976  fNtuple = new tools::hbook::wntuple(fNtupleHbookId, name);
977  // ntuple object is deleted when closing a file
978  }
979 
980 #ifdef G4VERBOSE
981  if ( fpVerboseL2 ) {
982  G4ExceptionDescription description;
983  description << " name : " << name << " hbook index : " << fNtupleHbookId;
984  fpVerboseL2->Message("create", "ntuple", description);
985  }
986 #endif
987 }
988 
989 //_____________________________________________________________________________
990 G4int ExG4HbookAnalysisManager::CreateNtupleIColumn(const G4String& name)
991 {
992 #ifdef G4VERBOSE
993  if ( fpVerboseL4 )
994  fpVerboseL4->Message("create", "ntuple I column", name);
995 #endif
996 
997  if ( ! fNtupleBooking ) {
998  G4ExceptionDescription description;
999  description << " "
1000  << "Ntuple has to be created first. ";
1001  G4Exception("G4HbookAnalysisManager::CreateNtupleIColumn()",
1002  "Analysis_W005", JustWarning, description);
1003  return -1;
1004  }
1005 
1006  // Save column info in booking
1007  G4int index = fNtupleBooking->m_columns.size();
1008  fNtupleBooking->add_column<int>(name);
1009 
1010  // Create column if ntuple already exists
1011  if ( fNtuple ) {
1012  tools::hbook::wntuple::column<int>* column
1013  = fNtuple->create_column<int>(name);
1014  fNtupleIColumnMap[index] = column;
1015  }
1016 
1017  fLockFirstNtupleColumnId = true;
1018 
1019 #ifdef G4VERBOSE
1020  if ( fpVerboseL2 )
1021  fpVerboseL2->Message("create", "ntuple I column", name);
1022 #endif
1023 
1024  return index + fFirstNtupleColumnId;
1025 }
1026 
1027 //_____________________________________________________________________________
1028 G4int ExG4HbookAnalysisManager::CreateNtupleFColumn(const G4String& name)
1029 {
1030 #ifdef G4VERBOSE
1031  if ( fpVerboseL4 )
1032  fpVerboseL4->Message("create", "ntuple F column", name);
1033 #endif
1034 
1035  if ( ! fNtupleBooking ) {
1036  G4ExceptionDescription description;
1037  description << " "
1038  << "Ntuple has to be created first. ";
1039  G4Exception("G4HbookAnalysisManager::CreateNtupleFColumn()",
1040  "Analysis_W005", JustWarning, description);
1041  return -1;
1042  }
1043 
1044  // Save column info in booking
1045  G4int index = fNtupleBooking->m_columns.size();
1046  fNtupleBooking->add_column<float>(name);
1047 
1048  // Create column if ntuple already exists
1049  if ( fNtuple ) {
1050  tools::hbook::wntuple::column<float>* column
1051  = fNtuple->create_column<float>(name);
1052  fNtupleFColumnMap[index] = column;
1053  }
1054 
1055  fLockFirstNtupleColumnId = true;
1056 
1057 #ifdef G4VERBOSE
1058  if ( fpVerboseL2 )
1059  fpVerboseL2->Message("create", "ntuple F column", name);
1060 #endif
1061 
1062  return index + fFirstNtupleColumnId;
1063 }
1064 
1065 
1066 //_____________________________________________________________________________
1067 G4int ExG4HbookAnalysisManager::CreateNtupleDColumn(const G4String& name)
1068 {
1069 #ifdef G4VERBOSE
1070  if ( fpVerboseL4 )
1071  fpVerboseL4->Message("create", "ntuple D column", name);
1072 #endif
1073 
1074  if ( ! fNtupleBooking ) {
1075  G4ExceptionDescription description;
1076  description << " "
1077  << "Ntuple has to be created first. ";
1078  G4Exception("G4HbookAnalysisManager::CreateNtupleDColumn()",
1079  "Analysis_W005", JustWarning, description);
1080  return -1;
1081  }
1082 
1083  // Save column info in booking
1084  G4int index = fNtupleBooking->m_columns.size();
1085  fNtupleBooking->add_column<double>(name);
1086 
1087  // Create column if ntuple already exists
1088  if ( fNtuple ) {
1089  tools::hbook::wntuple::column<double>* column
1090  = fNtuple->create_column<double>(name);
1091  fNtupleDColumnMap[index] = column;
1092  }
1093 
1094  fLockFirstNtupleColumnId = true;
1095 
1096 #ifdef G4VERBOSE
1097  if ( fpVerboseL2 )
1098  fpVerboseL2->Message("create", "ntuple D column", name);
1099 #endif
1100 
1101  return index + fFirstNtupleColumnId;
1102 }
1103 
1104 //_____________________________________________________________________________
1105 void ExG4HbookAnalysisManager::FinishNtuple()
1106 {
1107  if ( ! fNtuple ) return;
1108 
1109 #ifdef G4VERBOSE
1110  if ( fpVerboseL4 )
1111  fpVerboseL4->Message("finish", "ntuple", fNtupleBooking->m_name);
1112 #endif
1113 
1114  // Return to //PAWC/LUN1 :
1115  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
1116 
1117  //fNtuple->add_row_beg();
1118 #ifdef G4VERBOSE
1119  if ( fpVerboseL2 )
1120  fpVerboseL2->Message("finish", "ntuple", fNtupleBooking->m_name);
1121 #endif
1122 }
1123 
1124 //_____________________________________________________________________________
1125 G4bool ExG4HbookAnalysisManager::FillH1(G4int id, G4double value, G4double weight)
1126 {
1127  tools::hbook::h1* h1 = GetH1InFunction(id, "FillH1", true, false);
1128  if ( ! h1 ) return false;
1129 
1130  if ( fActivation && ( ! GetActivation(kH1, id) ) ) {
1131  //G4cout << "Skipping FillH1 for " << id << G4endl;
1132  return false;
1133  }
1134 
1135  G4HnInformation* info = GetInformation(kH1, id);
1136  h1->fill(info->fXFcn(value/info->fXUnit), weight);
1137 #ifdef G4VERBOSE
1138  if ( fpVerboseL4 ) {
1139  G4ExceptionDescription description;
1140  description << " id " << id << " value " << value;
1141  fpVerboseL4->Message("fill", "H1", description);
1142  }
1143 #endif
1144  return true;
1145 }
1146 
1147 //_____________________________________________________________________________
1148 G4bool ExG4HbookAnalysisManager::FillH2(G4int id,
1149  G4double xvalue, G4double yvalue,
1150  G4double weight)
1151 {
1152  tools::hbook::h2* h2 = GetH2InFunction(id, "FillH2", true, false);
1153  if ( ! h2 ) return false;
1154 
1155  if ( fActivation && ( ! GetActivation(kH2, id) ) ) return false;
1156 
1157  G4HnInformation* info = GetInformation(kH2, id);
1158  h2->fill(info->fXFcn(xvalue/info->fXUnit),
1159  info->fYFcn(yvalue/info->fYUnit), weight);
1160 #ifdef G4VERBOSE
1161  if ( fpVerboseL4 ) {
1162  G4ExceptionDescription description;
1163  description << " id " << id
1164  << " xvalue " << xvalue << " yvalue " << yvalue;
1165  fpVerboseL4->Message("fill", "H2", description);
1166  }
1167 #endif
1168  return true;
1169 }
1170 
1171 //_____________________________________________________________________________
1172 G4bool ExG4HbookAnalysisManager::FillNtupleIColumn(G4int id, G4int value)
1173 {
1174  tools::hbook::wntuple::column<int>* column = GetNtupleIColumn(id);
1175  if ( ! column ) {
1176  G4ExceptionDescription description;
1177  description << " " << "column " << id << " does not exist.";
1178  G4Exception("G4HbookAnalysisManager::FillNtupleIColumn()",
1179  "Analysis_W009", JustWarning, description);
1180  return false;
1181  }
1182 
1183  column->fill(value);
1184  #ifdef G4VERBOSE
1185  if ( fpVerboseL4 ) {
1186  G4ExceptionDescription description;
1187  description << " id " << id << " value " << value;
1188  fpVerboseL4->Message("fill", "ntuple I column", description);
1189  }
1190 #endif
1191  return true;
1192 }
1193 //_____________________________________________________________________________
1194 G4bool ExG4HbookAnalysisManager::FillNtupleFColumn(G4int id, G4float value)
1195 {
1196  tools::hbook::wntuple::column<float>* column = GetNtupleFColumn(id);
1197  if ( ! column ) {
1198  G4ExceptionDescription description;
1199  description << " " << "column " << id << " does not exist.";
1200  G4Exception("G4HbookAnalysisManager::FillNtupleFColumn()",
1201  "Analysis_W009", JustWarning, description);
1202  return false;
1203  }
1204 
1205  column->fill(value);
1206 #ifdef G4VERBOSE
1207  if ( fpVerboseL4 ) {
1208  G4ExceptionDescription description;
1209  description << " id " << id << " value " << value;
1210  fpVerboseL4->Message("fill", "ntuple F column", description);
1211  }
1212 #endif
1213  return true;
1214 }
1215 //_____________________________________________________________________________
1216 G4bool ExG4HbookAnalysisManager::FillNtupleDColumn(G4int id, G4double value)
1217 {
1218  tools::hbook::wntuple::column<double>* column = GetNtupleDColumn(id);
1219  if ( ! column ) {
1220  G4ExceptionDescription description;
1221  description << " " << "column " << id << " does not exist.";
1222  G4Exception("G4HbookAnalysisManager::FillNtupleDColumn()",
1223  "Analysis_W009", JustWarning, description);
1224  return false;
1225  }
1226 
1227  column->fill(value);
1228 #ifdef G4VERBOSE
1229  if ( fpVerboseL4 ) {
1230  G4ExceptionDescription description;
1231  description << " id " << id << " value " << value;
1232  fpVerboseL4->Message("fill", "ntuple D column", description);
1233  }
1234 #endif
1235  return true;
1236 }
1237 
1238 //_____________________________________________________________________________
1239 G4bool ExG4HbookAnalysisManager::AddNtupleRow()
1240 {
1241 #ifdef G4VERBOSE
1242  if ( fpVerboseL4 )
1243  fpVerboseL4->Message("add", "ntuple row", "");
1244 #endif
1245 
1246  //G4cout << "Hbook: Going to add Ntuple row ..." << G4endl;
1247  if ( ! fNtuple ) {
1248  G4ExceptionDescription description;
1249  description << " " << "ntuple does not exist. ";
1250  G4Exception("G4HbookAnalysisManager::AddNtupleRow()",
1251  "Analysis_W008", JustWarning, description);
1252  return false;
1253  }
1254 
1255  //fNtuple->add_row_fast();
1256  fNtuple->add_row();
1257 #ifdef G4VERBOSE
1258  if ( fpVerboseL3 )
1259  fpVerboseL3->Message("add", "ntuple row", "");
1260 #endif
1261  return true;
1262 }
1263 
1264 //_____________________________________________________________________________
1265 tools::hbook::h1* ExG4HbookAnalysisManager::GetH1(G4int id, G4bool warn,
1266  G4bool onlyIfActive) const
1267 {
1268  return GetH1InFunction(id, "GetH1", warn, onlyIfActive);
1269 }
1270 
1271 //_____________________________________________________________________________
1272 tools::hbook::h2* ExG4HbookAnalysisManager::GetH2(G4int id, G4bool warn,
1273  G4bool onlyIfActive) const
1274 {
1275  return GetH2InFunction(id, "GetH2", warn, onlyIfActive);
1276 }
1277 
1278 //_____________________________________________________________________________
1279 G4int ExG4HbookAnalysisManager::GetH1Id(const G4String& name, G4bool warn) const
1280 {
1281  std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
1282  if ( it == fH1NameIdMap.end() ) {
1283  if ( warn) {
1284  G4String inFunction = "ExG4HbookAnalysisManager::GetH1Id";
1285  G4ExceptionDescription description;
1286  description << " " << "histogram " << name << " does not exist.";
1287  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
1288  }
1289  return -1;
1290  }
1291  return it->second;
1292 }
1293 
1294 //_____________________________________________________________________________
1295 G4int ExG4HbookAnalysisManager::GetH2Id(const G4String& name, G4bool warn) const
1296 {
1297  std::map<G4String, G4int>::const_iterator it = fH2NameIdMap.find(name);
1298  if ( it == fH2NameIdMap.end() ) {
1299  if ( warn) {
1300  G4String inFunction = "ExG4HbookAnalysisManager::GetH2Id";
1301  G4ExceptionDescription description;
1302  description << " " << "histogram " << name << " does not exist.";
1303  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
1304  }
1305  return -1;
1306  }
1307  return it->second;
1308 }
1309 
1310 //_____________________________________________________________________________
1311 tools::hbook::wntuple* ExG4HbookAnalysisManager::GetNtuple() const
1312 {
1313  return fNtuple;
1314 }
1315 
1316 //_____________________________________________________________________________
1317 G4int ExG4HbookAnalysisManager::GetH1Nbins(G4int id) const
1318 {
1319  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Nbins");
1320  if ( ! h1 ) return 0;
1321 
1322  return h1->axis().bins();
1323 }
1324 
1325 //_____________________________________________________________________________
1326 G4double ExG4HbookAnalysisManager::GetH1Xmin(G4int id) const
1327 {
1328 // Returns xmin value with applied unit and histogram function
1329 
1330  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Xmin");
1331  if ( ! h1 ) return 0;
1332 
1333  G4HnInformation* info = GetInformation(kH1, id);
1334  return info->fXFcn(h1->axis().lower_edge()*info->fXUnit);
1335 }
1336 
1337 //_____________________________________________________________________________
1338 G4double ExG4HbookAnalysisManager::GetH1Xmax(G4int id) const
1339 {
1340  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Xmax");
1341  if ( ! h1 ) return 0;
1342 
1343  G4HnInformation* info = GetInformation(kH1, id);
1344  return info->fXFcn(h1->axis().upper_edge()*info->fXUnit);
1345 }
1346 
1347 //_____________________________________________________________________________
1348 G4double ExG4HbookAnalysisManager::GetH1Width(G4int id) const
1349 {
1350  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1XWidth", true, false);
1351  if ( ! h1 ) return 0;
1352 
1353  G4int nbins = h1->axis().bins();
1354  if ( ! nbins ) {
1355  G4ExceptionDescription description;
1356  description << " nbins = 0 (for h1 id = " << id << ").";
1357  G4Exception("ExG4HbookAnalysisManager::GetH1Width",
1358  "Analysis_W014", JustWarning, description);
1359  return 0;
1360  }
1361 
1362  G4HnInformation* info = GetInformation(kH1, id);
1363  return ( info->fXFcn(h1->axis().upper_edge())
1364  - info->fXFcn(h1->axis().lower_edge()))*info->fXUnit/nbins;
1365 }
1366 
1367 //_____________________________________________________________________________
1368 G4int ExG4HbookAnalysisManager::GetH2Nxbins(G4int id) const
1369 {
1370  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2NXbins");
1371  if ( ! h2 ) return 0;
1372 
1373  return h2->axis_x().bins();
1374 }
1375 
1376 //_____________________________________________________________________________
1377 G4double ExG4HbookAnalysisManager::GetH2Xmin(G4int id) const
1378 {
1379 // Returns xmin value with applied unit and histogram function
1380 
1381  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2Xmin");
1382  if ( ! h2 ) return 0;
1383 
1384  G4HnInformation* info = GetInformation(kH2, id);
1385  return info->fXFcn(h2->axis_x().lower_edge()*info->fXUnit);
1386 }
1387 
1388 //_____________________________________________________________________________
1389 G4double ExG4HbookAnalysisManager::GetH2Xmax(G4int id) const
1390 {
1391  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2Xmax");
1392  if ( ! h2 ) return 0;
1393 
1394  G4HnInformation* info = GetInformation(kH2, id);
1395  return info->fXFcn(h2->axis_x().upper_edge()*info->fXUnit);
1396 }
1397 
1398 //_____________________________________________________________________________
1399 G4double ExG4HbookAnalysisManager::GetH2XWidth(G4int id) const
1400 {
1401  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2XWidth", true, false);
1402  if ( ! h2 ) return 0;
1403 
1404  G4int nbins = h2->axis_x().bins();
1405  if ( ! nbins ) {
1406  G4ExceptionDescription description;
1407  description << " nbins = 0 (for h1 id = " << id << ").";
1408  G4Exception("ExG4HbookAnalysisManager::GetH2Width",
1409  "Analysis_W014", JustWarning, description);
1410  return 0;
1411  }
1412 
1413  G4HnInformation* info = GetInformation(kH2, id);
1414  return ( info->fXFcn(h2->axis_x().upper_edge())
1415  - info->fXFcn(h2->axis_x().lower_edge()))*info->fXUnit/nbins;
1416 }
1417 
1418 //_____________________________________________________________________________
1419 G4int ExG4HbookAnalysisManager::GetH2Nybins(G4int id) const
1420 {
1421  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2NYbins");
1422  if ( ! h2 ) return 0;
1423 
1424  return h2->axis_y().bins();
1425 }
1426 
1427 //_____________________________________________________________________________
1428 G4double ExG4HbookAnalysisManager::GetH2Ymin(G4int id) const
1429 {
1430 // Returns xmin value with applied unit and histogram function
1431 
1432  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2Ymin");
1433  if ( ! h2 ) return 0;
1434 
1435  G4HnInformation* info = GetInformation(kH2, id);
1436  return info->fYFcn(h2->axis_y().lower_edge()*info->fYUnit);
1437 }
1438 
1439 //_____________________________________________________________________________
1440 G4double ExG4HbookAnalysisManager::GetH2Ymax(G4int id) const
1441 {
1442  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2Ymax");
1443  if ( ! h2 ) return 0;
1444 
1445  G4HnInformation* info = GetInformation(kH2, id);
1446  return info->fYFcn(h2->axis_y().upper_edge()*info->fYUnit);
1447 }
1448 
1449 //_____________________________________________________________________________
1450 G4double ExG4HbookAnalysisManager::GetH2YWidth(G4int id) const
1451 {
1452  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2YWidth", true, false);
1453  if ( ! h2 ) return 0;
1454 
1455  G4int nbins = h2->axis_y().bins();
1456  if ( ! nbins ) {
1457  G4ExceptionDescription description;
1458  description << " nbins = 0 (for h1 id = " << id << ").";
1459  G4Exception("ExG4HbookAnalysisManager::GetH2Width",
1460  "Analysis_W014", JustWarning, description);
1461  return 0;
1462  }
1463 
1464  G4HnInformation* info = GetInformation(kH2, id);
1465  return ( info->fYFcn(h2->axis_y().upper_edge())
1466  - info->fYFcn(h2->axis_y().lower_edge()))*info->fYUnit/nbins;
1467 }
1468 
1469 //_____________________________________________________________________________
1470 G4bool ExG4HbookAnalysisManager::SetH1Title(G4int id, const G4String& title)
1471 {
1472  h1_booking* h1Booking = GetH1Booking(id, false);
1473  if ( ! h1Booking ) {
1474  G4ExceptionDescription description;
1475  description << " " << "histogram " << id << " does not exist.";
1476  G4Exception("G4HbookAnalysisManager::SetH1Title()",
1477  "Analysis_W007", JustWarning, description);
1478  return false;
1479  }
1480 
1481  h1Booking->fTitle = title;
1482  return true;
1483 }
1484 
1485 //_____________________________________________________________________________
1486 G4bool ExG4HbookAnalysisManager::SetH1XAxisTitle(G4int id, const G4String& title)
1487 {
1488  tools::hbook::h1* h1 = GetH1InFunction(id, "SetH1XAxisTitle");
1489  if ( ! h1 ) return false;
1490 
1491  h1->add_annotation(tools::hbook::key_axis_x_title(), title);
1492  return true;
1493 }
1494 
1495 //_____________________________________________________________________________
1496 G4bool ExG4HbookAnalysisManager::SetH1YAxisTitle(G4int id, const G4String& title)
1497 {
1498  tools::hbook::h1* h1 = GetH1InFunction(id, "SetH1YAxisTitle");
1499  if ( ! h1 ) return false;
1500 
1501  h1->add_annotation(tools::hbook::key_axis_y_title(), title);
1502  return true;
1503 }
1504 
1505 //_____________________________________________________________________________
1506 G4bool ExG4HbookAnalysisManager::SetH2Title(G4int id, const G4String& title)
1507 {
1508  h2_booking* h2Booking = GetH2Booking(id, false);
1509  if ( ! h2Booking ) {
1510  G4ExceptionDescription description;
1511  description << " " << "histogram " << id << " does not exist.";
1512  G4Exception("G4HbookAnalysisManager::SetH2Title()",
1513  "Analysis_W007", JustWarning, description);
1514  return false;
1515  }
1516 
1517  h2Booking->fTitle = title;
1518  return true;
1519 }
1520 
1521 //_____________________________________________________________________________
1522 G4bool ExG4HbookAnalysisManager::SetH2XAxisTitle(G4int id, const G4String& title)
1523 {
1524  tools::hbook::h2* h2 = GetH2InFunction(id, "SetH2XAxisTitle");
1525  if ( ! h2 ) return false;
1526 
1527  h2->add_annotation(tools::hbook::key_axis_x_title(), title);
1528  return true;
1529 }
1530 
1531 //_____________________________________________________________________________
1532 G4bool ExG4HbookAnalysisManager::SetH2YAxisTitle(G4int id, const G4String& title)
1533 {
1534  tools::hbook::h2* h2 = GetH2InFunction(id, "SetH2YAxisTitle");
1535  if ( ! h2 ) return false;
1536 
1537  h2->add_annotation(tools::hbook::key_axis_x_title(), title);
1538  return true;
1539 }
1540 
1541 //_____________________________________________________________________________
1542 G4bool ExG4HbookAnalysisManager::SetH2ZAxisTitle(G4int id, const G4String& title)
1543 {
1544  tools::hbook::h2* h2 = GetH2InFunction(id, "SetH2ZAxisTitle");
1545  if ( ! h2 ) return false;
1546 
1547  h2->add_annotation(tools::hbook::key_axis_z_title(), title);
1548  return true;
1549 }
1550 
1551 //_____________________________________________________________________________
1552 G4String ExG4HbookAnalysisManager::GetH1Title(G4int id) const
1553 {
1554  h1_booking* h1Booking = GetH1Booking(id, false);
1555  if ( ! h1Booking ) {
1556  G4ExceptionDescription description;
1557  description << " " << "histogram " << id << " does not exist.";
1558  G4Exception("G4HbookAnalysisManager::GetH1Title()",
1559  "Analysis_W007", JustWarning, description);
1560  return "";
1561  }
1562 
1563  return h1Booking->fTitle;
1564 }
1565 
1566 
1567 //_____________________________________________________________________________
1568 G4String ExG4HbookAnalysisManager::GetH1XAxisTitle(G4int id) const
1569 {
1570  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1XAxisTitle");
1571  if ( ! h1 ) return "";
1572 
1573  G4String title;
1574  G4bool result = h1->annotation(tools::hbook::key_axis_x_title(), title);
1575  if ( ! result ) {
1576  G4ExceptionDescription description;
1577  description << " Failed to get x_axis title for h1 id = " << id << ").";
1578  G4Exception("ExG4HbookAnalysisManager::GetH1XAxisTitle",
1579  "Analysis_W014", JustWarning, description);
1580  return "";
1581  }
1582 
1583  return title;
1584 }
1585 
1586 //_____________________________________________________________________________
1587 G4String ExG4HbookAnalysisManager::GetH1YAxisTitle(G4int id) const
1588 {
1589  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1YAxisTitle");
1590  if ( ! h1 ) return "";
1591 
1592  G4String title;
1593  G4bool result = h1->annotation(tools::hbook::key_axis_y_title(), title);
1594  if ( ! result ) {
1595  G4ExceptionDescription description;
1596  description << " Failed to get y_axis title for h1 id = " << id << ").";
1597  G4Exception("ExG4HbookAnalysisManager::GetH1YAxisTitle",
1598  "Analysis_W014", JustWarning, description);
1599  return "";
1600  }
1601 
1602  return title;
1603 }
1604 
1605 //_____________________________________________________________________________
1606 G4String ExG4HbookAnalysisManager::GetH2Title(G4int id) const
1607 {
1608  h2_booking* h2Booking = GetH2Booking(id, false);
1609  if ( ! h2Booking ) {
1610  G4ExceptionDescription description;
1611  description << " " << "histogram " << id << " does not exist.";
1612  G4Exception("G4HbookAnalysisManager::GetH2Title()",
1613  "Analysis_W007", JustWarning, description);
1614  return "";
1615  }
1616 
1617  return h2Booking->fTitle;
1618 }
1619 
1620 
1621 //_____________________________________________________________________________
1622 G4String ExG4HbookAnalysisManager::GetH2XAxisTitle(G4int id) const
1623 {
1624  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2XAxisTitle");
1625  if ( ! h2 ) return "";
1626 
1627  G4String title;
1628  G4bool result = h2->annotation(tools::hbook::key_axis_x_title(), title);
1629  if ( ! result ) {
1630  G4ExceptionDescription description;
1631  description << " Failed to get x_axis title for h2 id = " << id << ").";
1632  G4Exception("ExG4HbookAnalysisManager::GetH2XAxisTitle",
1633  "Analysis_W014", JustWarning, description);
1634  return "";
1635  }
1636 
1637  return title;
1638 }
1639 
1640 //_____________________________________________________________________________
1641 G4String ExG4HbookAnalysisManager::GetH2YAxisTitle(G4int id) const
1642 {
1643  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2YAxisTitle");
1644  if ( ! h2 ) return "";
1645 
1646  G4String title;
1647  G4bool result = h2->annotation(tools::hbook::key_axis_y_title(), title);
1648  if ( ! result ) {
1649  G4ExceptionDescription description;
1650  description << " Failed to get y_axis title for h2 id = " << id << ").";
1651  G4Exception("ExG4HbookAnalysisManager::GetH2YAxisTitle",
1652  "Analysis_W014", JustWarning, description);
1653  return "";
1654  }
1655 
1656  return title;
1657 }
1658 
1659 //_____________________________________________________________________________
1660 G4String ExG4HbookAnalysisManager::GetH2ZAxisTitle(G4int id) const
1661 {
1662  tools::hbook::h2* h2 = GetH2InFunction(id, "GetH2ZAxisTitle");
1663  if ( ! h2 ) return "";
1664 
1665  G4String title;
1666  G4bool result = h2->annotation(tools::hbook::key_axis_z_title(), title);
1667  if ( ! result ) {
1668  G4ExceptionDescription description;
1669  description << " Failed to get z_axis title for h2 id = " << id << ").";
1670  G4Exception("ExG4HbookAnalysisManager::GetH2ZAxisTitle",
1671  "Analysis_W014", JustWarning, description);
1672  return "";
1673  }
1674 
1675  return title;
1676 }
1677 
1678 //_____________________________________________________________________________
1679 G4bool ExG4HbookAnalysisManager::SetH1HbookIdOffset(G4int offset)
1680 {
1681  if ( fH1Vector.size() ) {
1682  G4ExceptionDescription description;
1683  description
1684  << "Cannot set H1HbookIdOffset as some H1 histogramms already exist.";
1685  G4Exception("G4HbookAnalysisManager::SetH1HbookIdOffset()",
1686  "Analysis_W009", JustWarning, description);
1687  return false;
1688  }
1689 
1690  if ( fFirstHistoId + offset < 1 ) {
1691  G4ExceptionDescription description;
1692  description << "The first histogram HBOOK id must be >= 1.";
1693  G4Exception("G4HbookAnalysisManager::SetH1HbookIdOffset()",
1694  "Analysis_W009", JustWarning, description);
1695  return false;
1696  }
1697 
1698  fH1HbookIdOffset = offset;
1699  return true;
1700 }
1701 
1702 //_____________________________________________________________________________
1703 G4bool ExG4HbookAnalysisManager::SetH2HbookIdOffset(G4int offset)
1704 {
1705  if ( fH2Vector.size() ) {
1706  G4ExceptionDescription description;
1707  description
1708  << "Cannot set H2HbookIdOffset as some H2 histogramms already exist.";
1709  G4Exception("G4HbookAnalysisManager::SetH2HbookIdOffset()",
1710  "Analysis_W009", JustWarning, description);
1711  return false;
1712  }
1713 
1714  if ( fFirstHistoId + offset < 1 ) {
1715  G4ExceptionDescription description;
1716  description << "The first histogram HBOOK id must be >= 1.";
1717  G4Exception("G4HbookAnalysisManager::SetH1HbookIdOffset()",
1718  "Analysis_W009", JustWarning, description);
1719  return false;
1720  }
1721 
1722  fH2HbookIdOffset = offset;
1723  return true;
1724 }
1725 
1726 //_____________________________________________________________________________
1727 G4bool ExG4HbookAnalysisManager::SetNtupleHbookId(G4int ntupleId)
1728 {
1729  if ( fNtuple ) {
1730  G4ExceptionDescription description;
1731  description
1732  << "Cannot set NtupleHbookId as an ntuple already exists.";
1733  G4Exception("G4HbookAnalysisManager::SetNtupleHbookId()",
1734  "Analysis_W010", JustWarning, description);
1735  return false;
1736  }
1737 
1738  if ( ntupleId < 1 ) {
1739  G4ExceptionDescription description;
1740  description << "The ntuple HBOOK id must be >= 1.";
1741  G4Exception("G4HbookAnalysisManager::SetNtupleHbookId()",
1742  "Analysis_W010", JustWarning, description);
1743  return false;
1744  }
1745 
1746  fNtupleHbookId = ntupleId;
1747  return true;
1748 }
1749 
1750 #endif