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 "HistoMessenger.hh"
36 #include "G4UnitsTable.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),factoryOn(false)
46 {
47 #ifdef G4ANALYSIS_USE
48  // Creating the analysis factory
49  af = AIDA_createAnalysisFactory();
50  if(!af) {
51  G4cout << " HistoManager::HistoManager() :"
52  << " problem creating the AIDA analysis factory."
53  << G4endl;
54  }
55 #endif
56 
57  fileName[0] = "Pol01.aida";
58  fileType = "xml";
59  fileOption = "";
60  // histograms
61  for (G4int k=0; k<MaxHisto; k++) {
62  histo[k] = 0;
63  exist[k] = false;
64  Unit[k] = 1.0;
65  Width[k] = 1.0;
66  }
67 
68  histoMessenger = new HistoMessenger(this);
69 }
70 
71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 
74 {
75  delete histoMessenger;
76 
77 #ifdef G4ANALYSIS_USE
78  delete af;
79 #endif
80 }
81 
82 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
83 
84 void HistoManager::book()
85 {
86 #ifdef G4ANALYSIS_USE
87  if(!af) return;
88 
89  // Creating a tree mapped to an aida file.
90  if (fileName[0].find(".")==G4String::npos) {
91  fileName[1] = fileName[0] + "." + fileType;
92  }
93  else {
94  fileName[1] = fileName[0];
95  }
96 
97  G4bool readOnly = false;
98  G4bool createNew = true;
99  AIDA::ITreeFactory* tf = af->createTreeFactory();
100  tree = tf->create(fileName[1], fileType, readOnly, createNew, fileOption);
101  delete tf;
102  if(!tree) {
103  G4cout << "HistoManager::book() :"
104  << " problem creating the AIDA tree with "
105  << " storeName = " << fileName[1]
106  << " storeType = " << fileType
107  << " readOnly = " << readOnly
108  << " createNew = " << createNew
109  << " options = " << fileOption
110  << G4endl;
111  return;
112  }
113 
114  // Creating a histogram factory, whose histograms will be handled by the tree
115  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
116 
117  // create selected histograms
118  for (G4int k=0; k<MaxHisto; k++) {
119  if (exist[k]) {
120  histo[k] = hf->createHistogram1D( Label[k], Title[k],
121  Nbins[k], Vmin[k], Vmax[k]);
122  factoryOn = true;
123  }
124  }
125  delete hf;
126  if(factoryOn)
127  G4cout << "\n----> Histogram Tree is opened " << fileName[1] << G4endl;
128 #endif
129 }
130 
131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
132 
133 void HistoManager::save()
134 {
135 #ifdef G4ANALYSIS_USE
136  if (factoryOn) {
137  tree->commit(); // Writing the histograms to the file
138  tree->close(); // and closing the tree (and the file)
139  G4cout << "\n----> Histogram Tree is saved in " << fileName[1] << G4endl;
140 
141  delete tree;
142  tree = 0;
143  factoryOn = false;
144  }
145 #endif
146 }
147 
148 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
149 
150 void HistoManager::FillHistos(const G4String & particleName,
151  G4double kinEnergy, G4double costheta,
152  G4double phi,
153  G4double longitudinalPolarization)
154 {
155  G4int id=1;
156  if (particleName=="gamma") id=1;
157  else if (particleName=="e-") id=5;
158  else if (particleName=="e+") id=9;
159  else return;
160 
161  if (costheta>=1.) costheta=.99999999;
162  if (costheta<-1.) costheta=-1.;
163  FillHisto(id,kinEnergy);
164  FillHisto(id+1,costheta);
165  FillHisto(id+2,phi);
166  FillHisto(id+3,longitudinalPolarization);
167 }
168 
170 {
171  if (ih > MaxHisto) {
172  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
173  << "does not exist; e= " << e << " w= " << weight << G4endl;
174  return;
175  }
176 #ifdef G4ANALYSIS_USE
177  if(exist[ih]) histo[ih]->fill(e/Unit[ih], weight);
178 #endif
179 }
180 
181 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
182 
183 void HistoManager::SetHisto(G4int ih, G4int nbins, G4double valmin,
184  G4double valmax, const G4String& unit)
185 {
186  if (ih > MaxHisto) {
187  G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
188  << "does not exist" << G4endl;
189  return;
190  }
191 
192  const G4String id[] = { "0", "1", "2", "3", "4", "5",
193  "6", "7", "8", "9", "10", "11", "12"};
194  const G4String title[] =
195  { "dummy", //0
196  "Gamma Energy distribution", //1
197  "Gamma Cos(Theta) distribution", //2
198  "Gamma Phi angular distribution", //3
199  "Gamma longitudinal Polarization", //4
200  "Electron Energy distribution", //5
201  "Electron Cos(Theta) distribution", //6
202  "Electron Phi angular distribution", //7
203  "Electron longitudinal Polarization", //8
204  "Positron Energy distribution", //9
205  "Positron Cos(Theta) distribution", //10
206  "Positron Phi angular distribution", //11
207  "Positron longitudinal Polarization" //12
208  };
209 
210 
211  G4String titl = title[ih];
212  G4double vmin = valmin, vmax = valmax;
213  Unit[ih] = 1.;
214 
215  if (unit != "none") {
216  titl = title[ih] + " (" + unit + ")";
217  Unit[ih] = G4UnitDefinition::GetValueOf(unit);
218  vmin = valmin/Unit[ih]; vmax = valmax/Unit[ih];
219  }
220 
221  exist[ih] = true;
222  Label[ih] = id[ih];
223  Title[ih] = titl;
224  Nbins[ih] = nbins;
225  Vmin[ih] = vmin;
226  Vmax[ih] = vmax;
227  Width[ih] = (valmax-valmin)/nbins;
228 
229  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
230  << nbins << " bins from "
231  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
232 
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