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