Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistoManager.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 //
28 //
29 // $Id: HistoManager.cc 98059 2016-07-01 16:23:33Z gcosmo $
30 //
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 
34 #include "HistoManager.hh"
35 #include "G4UnitsTable.hh"
36 #include "G4SystemOfUnits.hh"
37 
38 #include "AIDA/AIDA.h"
39 
40 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
41 
43 :fAF(0),fTree(0), fNtuple1(0), fNtuple2(0)
44 {
45  // Creating the analysis factory
46  //
47  fAF = AIDA_createAnalysisFactory();
48  if(!fAF) {
49  G4cout << " HistoManager::HistoManager :"
50  << " problem creating the AIDA analysis factory."
51  << G4endl;
52  }
53 
54  // histograms
55  for (G4int k=0; k<kMaxHisto; k++) fHisto[k] = 0;
56 }
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
61 {
62  delete fAF;
63 }
64 
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
66 
67 void HistoManager::Book()
68 {
69  if (! fAF) return;
70 
71  // Creating a tree container to handle histograms and ntuples.
72  // This tree is associated to an output file.
73  //
74  G4String fileName = "AnaEx03";
75  G4String fileType = "root"; // hbook root xml
76  G4String fileOption = " ";
79 
80  fileName = fileName + "." + fileType;
81  G4bool readOnly = false;
82  G4bool createNew = true;
83  AIDA::ITreeFactory* tf = fAF->createTreeFactory();
84  fTree = tf->create(fileName, fileType, readOnly, createNew, fileOption);
85  delete tf;
86  if(!fTree) {
87  G4cout << " HistoManager::book :"
88  << " problem creating the AIDA tree with "
89  << " storeName = " << fileName
90  << " storeType = " << fileType
91  << " readOnly = " << readOnly
92  << " createNew = " << createNew
93  << " options = " << fileOption
94  << G4endl;
95  return;
96  }
97 
98  // Creating a histogram factory, whose histograms will be handled by the tree
99  //
100  AIDA::IHistogramFactory* hf = fAF->createHistogramFactory(*fTree);
101 
102  // create histos in subdirectory "histograms"
103  //
104  fTree->mkdir("histograms");
105  fTree->cd("histograms");
106 
107  // id = 0
108  fHisto[0] = hf->createHistogram1D("EAbs", "EAbs: Edep in absorber", 100, 0., 800*MeV);
109  // id = 1
110  fHisto[1] = hf->createHistogram1D("EGap", "EGap: Edep in gap", 100, 0., 100*MeV);
111  // id = 2
112  fHisto[2] = hf->createHistogram1D("LAbs", "LAbs: trackL in absorber", 100, 0., 1*m);
113  // id = 3
114  fHisto[3] = hf->createHistogram1D("LGap", "LGap: trackL in gap", 100, 0., 50*cm);
115 
116  for ( G4int i=0; i<kMaxHisto; ++i ) {
117  if (! fHisto[i]) G4cout << "\n can't create histo " << i << G4endl;
118  }
119 
120  delete hf;
121  fTree->cd("..");
122 
123  // Creating a ntuple factory, handled by the tree
124  //
125  AIDA::ITupleFactory* ntf = fAF->createTupleFactory(*fTree);
126 
127  // create 1 ntuple in subdirectory "tuples"
128  //
129  fTree->mkdir("tuples");
130  fTree->cd("tuples");
131 
132  fNtuple1 = ntf->create("Ntuple1", "Edep", "double Eabs, Egap");
133  fNtuple2 = ntf->create("Ntuple2", "TrackL", "double Labs, Lgap");
134 
135  delete ntf;
136  fTree->cd("..");
137 
138  G4cout << "\n----> Output file is open in " << fileName << G4endl;
139 }
140 
141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
142 
143 void HistoManager::Save()
144 {
145  if (! (fAF && fTree)) return;
146 
147  fTree->commit(); // Writing the histograms to the file
148  fTree->close(); // and closing the tree (and the file)
149  G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
150 
151  delete fTree;
152  fTree = 0;
153 }
154 
155 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
156 
157 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
158 {
159  if (ih >= kMaxHisto) {
160  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
161  << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
162  << G4endl;
163  return;
164  }
165 
166  if (fHisto[ih]) fHisto[ih]->fill(xbin, weight);
167 }
168 
169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
170 
172 {
173  if (ih >= kMaxHisto) {
174  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
175  << " does not exist. (fac=" << fac << ")" << G4endl;
176  return;
177  }
178 
179  if (fHisto[ih]) fHisto[ih]->scale(fac);
180 }
181 
182 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
183 
184 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
185  G4double trackLAbs, G4double trackLGap)
186 {
187  if (fNtuple1) {
188  fNtuple1->fill(0, energyAbs);
189  fNtuple1->fill(1, energyGap);
190  fNtuple1->addRow();
191  }
192  if (fNtuple2) {
193  fNtuple2->fill(0, trackLAbs);
194  fNtuple2->fill(1, trackLGap);
195  fNtuple2->addRow();
196  }
197 }
198 
200 {
201  G4cout << "\n ----> print histograms statistic \n" << G4endl;
202  for ( G4int i=0; i<kMaxHisto; ++i ) {
203  AIDA::IHistogram1D* h1 = fHisto[i];
204  G4String title = h1->title();
205  // extract name as first 4 characters from title, as aida seems not to keep
206  // histogram name
207  const G4String name = title(0,4);
208 
209  G4String unitCategory;
210  if (name[0] == 'E' ) unitCategory = "Energy";
211  if (name[0] == 'L' ) unitCategory = "Length";
212 
213  G4cout << name
214  << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
215  << " rms = " << G4BestUnit(h1->rms(), unitCategory )
216  << G4endl;
217  }
218 }
219 
220 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
221 
222 
const XML_Char * name
Definition: expat.h:151
void FillHisto(G4int id, G4double e, G4double weight=1.0)
void PrintStatistic()
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
void Normalize(G4int id, G4double fac)
G4GLOB_DLL std::ostream G4cout
static constexpr double m
Definition: G4SIunits.hh:129
bool G4bool
Definition: G4Types.hh:79
static constexpr double cm
Definition: G4SIunits.hh:119
static const G4double fac
#define G4endl
Definition: G4ios.hh:61
static constexpr double MeV
Definition: G4SIunits.hh:214
void FillNtuple(G4double EnergyAbs, G4double EnergyGap, G4double TrackLAbs, G4double TrackLGap)
double G4double
Definition: G4Types.hh:76
const G4int kMaxHisto
Definition: HistoManager.hh:46
subroutine title
Definition: hijing1.383.f:5980
tuple hf
Definition: gammaraytel.py:8