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 //
26 // $Id$
27 //
28 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30 
31 #include "HistoManager.hh"
32 #include "HistoMessenger.hh"
33 #include "G4UnitsTable.hh"
34 
35 #ifdef G4ANALYSIS_USE
36 #include "AIDA/AIDA.h"
37 #endif
38 
39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
40 
42 :factoryOn(false),af(0),tree(0)
43 {
44 #ifdef G4ANALYSIS_USE
45  // Creating the analysis factory
46  af = AIDA_createAnalysisFactory();
47  if(!af) {
48  G4cout << " HistoManager::HistoManager :"
49  << " problem creating the AIDA analysis factory."
50  << G4endl;
51  }
52 #endif
53 
54  fileName[0] = "ecal";
55  fileType = "root";
56  fileOption = "export=root";
57 
58  // histograms
59  for (G4int k=0; k<MaxHisto; k++) {
60  histo[k] = 0;
61  exist[k] = false;
62  Unit[k] = 1.0;
63  Width[k] = 1.0;
64  ascii[k] = false;
65  }
66 
67  // ntuples
68  for (G4int k=0; k<MaxNtupl; k++) {
69  ntupl[k] = 0;
70  existNt[k] = false;
71  }
72 
73  histoMessenger = new HistoMessenger(this);
74 }
75 
76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
77 
79 {
80  delete histoMessenger;
81 
82 #ifdef G4ANALYSIS_USE
83  delete af;
84 #endif
85 }
86 
87 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
88 
90 {
91 #ifdef G4ANALYSIS_USE
92  if(!af) return;
93 
94  // Creating a tree mapped to an histogram file.
95  fileName[1] = fileName[0] + "." + fileType;
96  G4bool readOnly = false;
97  G4bool createNew = true;
98  AIDA::ITreeFactory* tf = af->createTreeFactory();
99  tree = tf->create(fileName[1], fileType, readOnly, createNew, fileOption);
100  delete tf;
101  if(!tree) {
102  G4cout << " HistoManager::book :"
103  << " problem creating the AIDA tree with "
104  << " storeName = " << fileName[1]
105  << " storeType = " << fileType
106  << " readOnly = " << readOnly
107  << " createNew = " << createNew
108  << " options = " << fileOption
109  << G4endl;
110  return;
111  }
112 
113  // Creating a histogram factory, whose histograms will be handled by the tree
114  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
115 
116  // create selected histograms
117  for (G4int k=1; k<MaxHisto; k++) {
118  if (exist[k]) {
119  histo[k] = hf->createHistogram1D( Label[k], Title[k],
120  Nbins[k], Vmin[k], Vmax[k]);
121  factoryOn = true;
122  }
123  }
124  delete hf;
125 
126  // Creating a ntuple factory, handled by the tree
127  AIDA::ITupleFactory* ntf = af->createTupleFactory(*tree);
128 
129  // create selected ntuples
130  for (G4int k=1; k<MaxNtupl; k++) {
131  if (existNt[k]) {
132  ntupl[k] = ntf->create( LabelNt[k], TitleNt[k], ColumnNt[k]);
133  factoryOn = true;
134  }
135  }
136 
137  delete ntf;
138 
139  if (factoryOn)
140  G4cout << "\n----> Histogram Tree is opened in " << fileName[1] << G4endl;
141 #endif
142 }
143 
144 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
145 
147 {
148 #ifdef G4ANALYSIS_USE
149  if (factoryOn) {
150  saveAscii(); // Write ascii file, if any
151  tree->commit(); // Writing the histograms to the file
152  tree->close(); // and closing the tree (and the file)
153  G4cout << "\n----> Histogram Tree is saved in " << fileName[1] << G4endl;
154 
155  delete tree;
156  tree = 0;
157  factoryOn = false;
158  }
159 #endif
160 }
161 
162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
163 
165 {
166  if (ih >= MaxHisto) {
167  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
168  << " xbin= " << xbin << " weight= " << weight << G4endl;
169  return;
170  }
171 #ifdef G4ANALYSIS_USE
172  if(exist[ih]) histo[ih]->fill(xbin/Unit[ih], weight);
173 #endif
174 }
175 
176 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
177 
179  G4int nbins, G4double valmin, G4double valmax, const G4String& unit)
180 {
181  if (ih < 1 || ih >= MaxHisto) {
182  G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
183  << "does not exist" << G4endl;
184  return;
185  }
186 
187  const G4String id[] = { "0", "1", "2", "3", "4", "5" };
188  const G4String title[] =
189  { "dummy", //0
190  "total Evis in Ecal", //1
191  "total Edep in Ecal", //2
192  "Evis profile", //3
193  "Edep profile", //4
194  "Nb of Radiation Length" //5
195  };
196 
197  G4String titl = title[ih];
198  G4double vmin = valmin, vmax = valmax;
199  Unit[ih] = 1.;
200 
201  if (unit != "none") {
202  titl = title[ih] + " (" + unit + ")";
203  Unit[ih] = G4UnitDefinition::GetValueOf(unit);
204  vmin = valmin/Unit[ih]; vmax = valmax/Unit[ih];
205  }
206 
207  exist[ih] = true;
208  Label[ih] = id[ih];
209  Title[ih] = titl;
210  Nbins[ih] = nbins;
211  Vmin[ih] = vmin;
212  Vmax[ih] = vmax;
213  Width[ih] = (valmax-valmin)/nbins;
214 
215  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
216  << nbins << " bins from "
217  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
218 
219 }
220 
221 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
222 
224 {
225  if (ih >= MaxHisto) {
226  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
227  << " fac= " << fac << G4endl;
228  return;
229  }
230 #ifdef G4ANALYSIS_USE
231  if(exist[ih]) histo[ih]->scale(fac);
232 #endif
233 }
234 
235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
236 
238 {
239  if (ih >= MaxHisto) {
240  G4cout << "---> warning from HistoManager::RemoveHisto() : histo " << ih
241  << "does not exist" << G4endl;
242  return;
243  }
244 
245  histo[ih] = 0; exist[ih] = false;
246 }
247 
248 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
249 
251 {
252  if (ih < MaxHisto) { ascii[ih] = true; ascii[0] = true; }
253  else
254  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
255  << "does not exist" << G4endl;
256 }
257 
258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
259 
260 #include <fstream>
261 
262 void HistoManager::saveAscii()
263 {
264 #ifdef G4ANALYSIS_USE
265 
266  if (!ascii[0]) return;
267 
268  G4String name = fileName[0] + ".ascii";
269  std::ofstream File(name, std::ios::out);
270  File.setf( std::ios::scientific, std::ios::floatfield );
271 
272  //write selected histograms
273  for (G4int ih=0; ih<MaxHisto; ih++) {
274  if (exist[ih] && ascii[ih]) {
275  File << "\n 1D histogram " << ih << ": " << Title[ih]
276  << "\n \n \t X \t\t Y" << G4endl;
277 
278  for (G4int iBin=0; iBin<Nbins[ih]; iBin++) {
279  File << " " << iBin << "\t"
280  << 0.5*(histo[ih]->axis().binLowerEdge(iBin) +
281  histo[ih]->axis().binUpperEdge(iBin)) << "\t"
282  << histo[ih]->binHeight(iBin)
283  << G4endl;
284  }
285  }
286  }
287 #endif
288 }
289 
290 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
291 
293 {
294  if (nt < 1 || nt >= MaxNtupl) {
295  G4cout << "---> warning from HistoManager::SetNtuple() : Ntuple " << nt
296  << "does not exist" << G4endl;
297  return;
298  }
299 
300  const G4String id[] = { "100", "101" };
301  const G4String title[] =
302  { "dummy", //0
303  "Energy deposit in subModule" //1
304  };
305 
306  G4String column[MaxNtupl];
307 
308  column[0] = " int dum=0 "; //0
309 
310  column[1] =
311  "double Evis0, Evis1, Evis2, Evis3, Evis4, Evis5, Evis6, Evis7, Evis8, Evis9";
312  column[1] +=
313  ", Evis10, Evis11, Evis12, Evis13, Evis14, Evis15, Evis16, Evis17";
314  column[1] +=
315  ", Edep0, Edep1, Edep2, Edep3, Edep4, Edep5, Edep6, Edep7, Edep8, Edep9";
316  column[1] +=
317  ", Edep10, Edep11, Edep12, Edep13, Edep14, Edep15, Edep16, Edep17";
318 
319  G4String titl = title[nt];
320 
321  existNt[nt] = true;
322  LabelNt[nt] = id[nt];
323  TitleNt[nt] = titl;
324  ColumnNt[nt] = column[nt];
325 
326  G4cout << "----> SetNtuple " << nt << ": " << titl << "; " << G4endl;
327 
328 }
329 
330 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
331 
333 {
334  if (nt >= MaxNtupl) {
335  G4cout << "---> warning from HistoManager::FillNtuple() : Ntuple " << nt
336  << " does not exist " << column << value << G4endl;
337  return;
338  }
339 #ifdef G4ANALYSIS_USE
340  if(existNt[nt]) ntupl[nt]->fill(column, value);
341 #endif
342 }
343 
344 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
345 
347 {
348  if (nt >= MaxNtupl) {
349  G4cout << "---> warning from HistoManager::AddRowNtuple() : Ntuple " << nt
350  << " do not exist" << G4endl;
351  return;
352  }
353 #ifdef G4ANALYSIS_USE
354  if(existNt[nt]) ntupl[nt]->addRow();
355 #endif
356 }
357 
358 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
359 
360