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