source: trunk/examples/extended/electromagnetic/TestEm5/src/HistoManager.cc @ 1309

Last change on this file since 1309 was 1230, checked in by garnier, 14 years ago

update to geant4.9.3

File size: 10.1 KB
Line 
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: HistoManager.cc,v 1.26 2008/09/13 17:05:40 maire Exp $
27// GEANT4 tag $Name: geant4-09-03-cand-01 $
28//
29//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31
32#include "HistoManager.hh"
33#include "HistoMessenger.hh"
34#include "G4UnitsTable.hh"
35
36#ifdef G4ANALYSIS_USE
37#include "AIDA/AIDA.h"
38#endif
39
40//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
41
42HistoManager::HistoManager()
43:af(0),tree(0),factoryOn(false)
44{
45#ifdef G4ANALYSIS_USE
46  // Creating the analysis factory
47  af = AIDA_createAnalysisFactory();
48  if(!af) {
49    G4cout << " HistoManager::HistoManager() :" 
50           << " problem creating the AIDA analysis factory."
51           << G4endl;
52  }         
53#endif
54 
55  fileName[0] = "testem5";
56  fileType    = "root";
57  fileOption  = "--noErrors export=root uncompress"; 
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  histoMessenger = new HistoMessenger(this);
68}
69
70//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
71
72HistoManager::~HistoManager()
73{
74  delete histoMessenger;
75 
76#ifdef G4ANALYSIS_USE 
77  delete af;
78#endif   
79}
80
81//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
82
83void HistoManager::book()
84{
85#ifdef G4ANALYSIS_USE
86  if(!af) return;
87 
88  // Creating a tree mapped to an hbook file.
89  fileName[1] = fileName[0] + "." + fileType; 
90  G4bool readOnly  = false;
91  G4bool createNew = true;
92  AIDA::ITreeFactory* tf  = af->createTreeFactory();
93  tree = tf->create(fileName[1], fileType, readOnly, createNew, fileOption);
94  delete tf;
95  if(!tree) {
96    G4cout << "HistoManager::book() :" 
97           << " problem creating the AIDA tree with "
98           << " storeName = " << fileName[1]
99           << " storeType = " << fileType
100           << " readOnly = "  << readOnly
101           << " createNew = " << createNew
102           << " options = "   << fileOption
103           << G4endl;
104    return;
105  }
106
107  // Creating a histogram factory, whose histograms will be handled by the tree
108  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
109
110  // create selected histograms
111  for (G4int k=0; k<MaxHisto; k++) {
112    if (exist[k]) {
113      histo[k] = hf->createHistogram1D( Label[k], Title[k],
114                                                  Nbins[k], Vmin[k], Vmax[k]);
115      factoryOn = true;
116    }
117  }
118  delete hf; 
119  if(factoryOn) 
120      G4cout << "\n----> Histogram Tree is opened in " << fileName[1] << G4endl;
121
122#endif
123}
124
125//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
126
127void HistoManager::save()
128{
129#ifdef G4ANALYSIS_USE
130  if (factoryOn) {
131    saveAscii();          // Write ascii file, if any     
132    tree->commit();       // Writing the histograms to the file
133    tree->close();        // and closing the tree (and the file)
134    G4cout << "\n----> Histogram Tree is saved in " << fileName[1] << G4endl;
135   
136    delete tree;
137    tree = 0;
138    factoryOn = false;
139  }
140#endif
141}
142
143//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
144
145void HistoManager::FillHisto(G4int ih, G4double e, G4double weight)
146{
147  if (ih > MaxHisto) {
148    G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
149           << "does not exist; e= " << e << " w= " << weight << G4endl;
150    return;
151  }
152#ifdef G4ANALYSIS_USE
153  if(exist[ih]) histo[ih]->fill(e/Unit[ih], weight);
154#endif
155}
156
157//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
158
159void HistoManager::SetHisto(G4int ih,
160            G4int nbins, G4double valmin, G4double valmax, const G4String& unit)
161{
162  if (ih > MaxHisto) {
163    G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
164           << "does not exist" << G4endl;
165    return;
166  }
167
168  const G4String id[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
169                         "10","11","12","13","14","15","16","17","18","19",
170                         "20","21","22","23","24","25","26","27","28","29",
171                         "30","31","32","33","34","35","36","37","38","39",
172                         "40","41","42","43","44","45","46","47","48","49" 
173                        };
174                       
175  const G4String title[] =
176                { "dummy",                                              //0
177                  "energy deposit in absorber",                         //1
178                  "energy of charged secondaries at creation",          //2
179                  "energy of gammas at creation (std::log10(ekin/MeV))",//3
180                  "x_vertex of charged secondaries (all)",              //4
181                  "x_vertex of charged secondaries (not absorbed)",     //5
182                  "dummy","dummy","dummy","dummy",                      //6-9
183                  "(transmit, charged) : kinetic energy at exit",       //10
184                  "(transmit, charged) : ener fluence: dE(MeV)/dOmega", //11
185                  "(transmit, charged) : space angle: dN/dOmega",       //12
186                  "(transmit, charged) : projected angle at exit",      //13
187                  "(transmit, charged) : projected position at exit",   //14
188                  "(transmit, charged) : radius at exit",               //15
189                  "dummy","dummy","dummy","dummy",                      //16-19
190                  "(transmit, neutral) : kinetic energy at exit",       //20
191                  "(transmit, neutral) : ener fluence: dE(MeV)/dOmega", //21
192                  "(transmit, neutral) : space angle: dN/dOmega",       //22
193                  "(transmit, neutral) : projected angle at exit",      //23
194                  "dummy","dummy","dummy","dummy","dummy","dummy",      //24-29
195                  "(reflect , charged) : kinetic energy at exit",       //30
196                  "(reflect , charged) : ener fluence: dE(MeV)/dOmega", //31
197                  "(reflect , charged) : space angle: dN/dOmega",       //32
198                  "(reflect , charged) : projected angle at exit",      //33
199                  "dummy","dummy","dummy","dummy","dummy","dummy",      //34-39
200                  "(reflect , neutral) : kinetic energy at exit",       //40
201                  "(reflect , neutral) : ener fluence: dE(MeV)/dOmega", //41
202                  "(reflect , neutral) : space angle: dN/dOmega",       //42
203                  "(reflect , neutral) : projected angle at exit",      //43
204                  "dummy","dummy","dummy","dummy","dummy","dummy"       //44-49
205                 };
206
207  G4String titl = title[ih];
208  G4double vmin = valmin, vmax = valmax;
209  Unit[ih] = 1.;
210
211  if (ih == 3) { vmin = std::log10(valmin/MeV); vmax = std::log10(valmax/MeV);}
212  else if (unit != "none") {
213    titl = title[ih] + " (" + unit + ")";
214    Unit[ih] = G4UnitDefinition::GetValueOf(unit);
215    vmin = valmin/Unit[ih]; vmax = valmax/Unit[ih];
216  }
217
218  exist[ih] = true;
219  Label[ih] = id[ih];
220  Title[ih] = titl;
221  Nbins[ih] = nbins;
222  Vmin[ih]  = vmin;
223  Vmax[ih]  = vmax;
224  Width[ih] = (valmax-valmin)/nbins;
225
226  G4cout << "----> SetHisto " << ih << ": " << titl << ";  "
227         << nbins << " bins from "
228         << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
229
230}
231
232//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
233
234void HistoManager::RemoveHisto(G4int ih)
235{
236 if (ih > MaxHisto) {
237    G4cout << "---> warning from HistoManager::RemoveHisto() : histo " << ih
238           << "does not exist" << G4endl;
239    return;
240  }
241
242  histo[ih] = 0;  exist[ih] = false;
243}
244
245//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
246
247void HistoManager::Scale(G4int ih, G4double fac)
248{
249 if (ih > MaxHisto) {
250    G4cout << "---> warning from HistoManager::Scale() : histo " << ih
251           << "does not exist.  (fac = " << fac << ")" << G4endl;
252    return;
253  }
254#ifdef G4ANALYSIS_USE
255  if(exist[ih]) histo[ih]->scale(fac);
256#endif
257}
258
259//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
260
261void HistoManager::PrintHisto(G4int ih)
262{
263 if (ih < MaxHisto) { ascii[ih] = true; ascii[0] = true; }
264 else
265    G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
266           << "does not exist" << G4endl;
267}
268
269//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
270
271#include <fstream>
272
273void HistoManager::saveAscii()
274{
275#ifdef G4ANALYSIS_USE
276
277 if (!ascii[0]) return;
278 
279 G4String name = fileName[0] + ".ascii";
280 std::ofstream File(name, std::ios::out);
281 File.setf( std::ios::scientific, std::ios::floatfield );
282 
283 //write selected histograms
284 for (G4int ih=0; ih<MaxHisto; ih++) {
285    if (exist[ih] && ascii[ih]) {
286      File << "\n  1D histogram " << ih << ": " << Title[ih] 
287           << "\n \n \t     X \t\t     Y" << G4endl;
288     
289      for (G4int iBin=0; iBin<Nbins[ih]; iBin++) {
290         File << "  " << iBin << "\t" 
291              << 0.5*(histo[ih]->axis().binLowerEdge(iBin) +
292                      histo[ih]->axis().binUpperEdge(iBin)) << "\t"           
293              << histo[ih]->binHeight(iBin) 
294              << G4endl;
295      } 
296    }
297 }
298#endif
299}
300
301//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
302
303
Note: See TracBrowser for help on using the repository browser.