Geant4  9.6.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$
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 #ifdef G4ANALYSIS_USE
39 #include "AIDA/AIDA.h"
40 #endif
41 
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 
45 :af(0),tree(0)
46 {
47 #ifdef G4ANALYSIS_USE
48  // Creating the analysis factory
49  //
50  af = AIDA_createAnalysisFactory();
51  if(!af) {
52  G4cout << " HistoManager::HistoManager :"
53  << " problem creating the AIDA analysis factory."
54  << G4endl;
55  }
56 #endif
57 
58  // histograms
59  for (G4int k=0; k<MaxHisto; k++) histo[k] = 0;
60 
61  // ntuple
62  ntupl = 0;
63 }
64 
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
66 
68 {
69 #ifdef G4ANALYSIS_USE
70  delete af;
71 #endif
72 }
73 
74 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
75 
76 void HistoManager::book()
77 {
78 #ifdef G4ANALYSIS_USE
79  if(!af) return;
80 
81  // Creating a tree container to handle histograms and ntuples.
82  // This tree is associated to an output file.
83  //
84  G4String fileName = "AnaEx03";
85  G4String fileType = "root"; // hbook root xml
86  G4String fileOption = " ";
89 
90  fileName = fileName + "." + fileType;
91  G4bool readOnly = false;
92  G4bool createNew = true;
93  AIDA::ITreeFactory* tf = af->createTreeFactory();
94  tree = tf->create(fileName, fileType, readOnly, createNew, fileOption);
95  delete tf;
96  if(!tree) {
97  G4cout << " HistoManager::book :"
98  << " problem creating the AIDA tree with "
99  << " storeName = " << fileName
100  << " storeType = " << fileType
101  << " readOnly = " << readOnly
102  << " createNew = " << createNew
103  << " options = " << fileOption
104  << G4endl;
105  return;
106  }
107 
108  // Creating a histogram factory, whose histograms will be handled by the tree
109  //
110  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
111 
112  // create histos in subdirectory "histograms"
113  //
114  tree->mkdir("histograms");
115  tree->cd("histograms");
116 
117  histo[1] = hf->createHistogram1D("1", "Edep in absorber", 100, 0., 800*MeV);
118  if (!histo[1]) G4cout << "\n can't create histo 1" << G4endl;
119  histo[2] = hf->createHistogram1D("2", "Edep in gap", 100, 0., 100*MeV);
120  if (!histo[2]) G4cout << "\n can't create histo 2" << G4endl;
121  histo[3] = hf->createHistogram1D("3", "trackL in absorber", 100, 0., 1*m);
122  if (!histo[3]) G4cout << "\n can't create histo 3" << G4endl;
123  histo[4] = hf->createHistogram1D("4", "trackL in gap", 100, 0., 50*cm);
124  if (!histo[4]) G4cout << "\n can't create histo 4" << G4endl;
125 
126  delete hf;
127  tree->cd("..");
128 
129  // Creating a ntuple factory, handled by the tree
130  //
131  AIDA::ITupleFactory* ntf = af->createTupleFactory(*tree);
132 
133  // create 1 ntuple in subdirectory "tuples"
134  //
135  tree->mkdir("tuples");
136  tree->cd("tuples");
137 
138  ntupl = ntf->create("101", "Edep and TrackL", "double Eabs, Egap, Labs, Lgap");
139 
140  delete ntf;
141  tree->cd("..");
142 
143  G4cout << "\n----> Histogram Tree is opened in " << fileName << G4endl;
144 #endif
145 }
146 
147 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
148 
149 void HistoManager::save()
150 {
151 #ifdef G4ANALYSIS_USE
152  if (af && tree) {
153  tree->commit(); // Writing the histograms to the file
154  tree->close(); // and closing the tree (and the file)
155  G4cout << "\n----> Histogram Tree is saved \n" << G4endl;
156 
157  delete tree;
158  tree = 0;
159  }
160 #endif
161 }
162 
163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
164 
166 {
167  if (ih >= MaxHisto) {
168  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
169  << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
170  << G4endl;
171  return;
172  }
173 #ifdef G4ANALYSIS_USE
174  if (histo[ih]) histo[ih]->fill(xbin, weight);
175 #endif
176 }
177 
178 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
179 
181 {
182  if (ih >= MaxHisto) {
183  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
184  << " does not exist. (fac=" << fac << ")" << G4endl;
185  return;
186  }
187 #ifdef G4ANALYSIS_USE
188  if (histo[ih]) histo[ih]->scale(fac);
189 #endif
190 }
191 
192 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
193 
194 #ifdef G4ANALYSIS_USE
195 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
196  G4double trackLAbs, G4double trackLGap)
197 {
198  if (ntupl) {
199  ntupl->fill(0, energyAbs);
200  ntupl->fill(1, energyGap);
201  ntupl->fill(2, trackLAbs);
202  ntupl->fill(3, trackLGap);
203  ntupl->addRow();
204  }
205 }
206 #else
208 {
209  return;
210 }
211 #endif
212 
214 {
215 #ifdef G4ANALYSIS_USE
216  if(histo[1]) {
217  G4cout << "\n ----> print histograms statistic \n" << G4endl;
218 
219  G4cout
220  << " EAbs : mean = " << G4BestUnit(histo[1]->mean(), "Energy")
221  << " rms = " << G4BestUnit(histo[1]->rms(), "Energy") << G4endl;
222  G4cout
223  << " EGap : mean = " << G4BestUnit(histo[2]->mean(), "Energy")
224  << " rms = " << G4BestUnit(histo[2]->rms(), "Energy") << G4endl;
225  G4cout
226  << " LAbs : mean = " << G4BestUnit(histo[3]->mean(), "Length")
227  << " rms = " << G4BestUnit(histo[3]->rms(), "Length") << G4endl;
228  G4cout
229  << " LGap : mean = " << G4BestUnit(histo[4]->mean(), "Length")
230  << " rms = " << G4BestUnit(histo[4]->rms(), "Length") << G4endl;
231 
232  }
233 #endif
234 }
235 
236 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
237 
238