Geant4  10.00.p01
Histo.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: Histo.cc 77519 2013-11-25 10:54:57Z gcosmo $
30 //
31 //---------------------------------------------------------------------------
32 //
33 // ClassName: Histo - Generic histogram/ntuple manager class
34 //
35 //
36 // Author: V.Ivanchenko 30.10.03
37 //
38 //----------------------------------------------------------------------------
39 //
40 
41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
43 
44 #include "Histo.hh"
45 #include "HistoMessenger.hh"
46 #include "G4RootAnalysisManager.hh"
47 #include "G4SystemOfUnits.hh"
48 
49 
50 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
52 
54 {
55  fManager = 0;
56  fMessenger = new HistoMessenger(this);
57 
58  fHistName = "test";
59  fHistType = "root";
60  fTupleName = "tuple";
61  fTupleTitle = "test";
62  fNHisto = 0;
63  fVerbose = 0;
64  fDefaultAct = true;
65  fHistoActive= false;
66  fNtupleActive= false;
67 }
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
70 
72 {
73  delete fMessenger;
74  delete fManager;
75 }
76 
77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
78 
79 void Histo::Book()
80 {
81  if(!(fHistoActive || fNtupleActive)) { return; }
82 
83  // Always creating analysis manager
85 
86  // Creating a tree mapped to a new hbook file.
87  G4String nam = fHistName + "." + fHistType;
88 
89  // Open file histogram file
90  if(!fManager->OpenFile(nam)) {
91  G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
92  fHistoActive = false;
93  fNtupleActive = false;
94  return;
95  }
96  G4cout << "### Histo::Save: Opended file <" << nam << "> for "
97  << fNHisto << " histograms " << G4endl;
98 
99  // Creating an 1-dimensional histograms in the root directory of the tree
100  for(G4int i=0; i<fNHisto; ++i) {
101  if(fActive[i]) {
102  G4String ss = "h" + fIds[i];
103  fHisto[i] = fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
104  if(fVerbose > 0) {
105  G4cout << "Created histogram #" << i << " id= " << fHisto[i]
106  << " " << ss << " " << fTitles[i] << G4endl;
107  }
108  }
109  }
110  // Creating a tuple factory, whose tuples will be handled by the tree
111  if(fNtupleActive) {
113  G4int i;
114  G4int n = fNtupleI.size();
115  for(i=0; i<n; ++i) {
116  if(fTupleI[i] == -1) {
118  }
119  }
120  n = fNtupleF.size();
121  for(i=0; i<n; ++i) {
122  if(fTupleF[i] == -1) {
124  }
125  }
126  n = fNtupleD.size();
127  for(i=0; i<n; ++i) {
128  if(fTupleD[i] == -1) {
130  }
131  }
132  }
133 }
134 
135 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
136 
137 void Histo::Save()
138 {
139  if(!(fHistoActive || fNtupleActive)) { return; }
140 
141  // Creating a tree mapped to a new hbook file.
142  G4String nam = fHistName + "." + fHistType;
143 
144  // Write histogram file
145  if(!fManager->Write()) {
146  G4cout << "Histo::Save: FATAL ERROR writing ROOT file" << G4endl;
147  exit(1);
148  }
149  if(fVerbose > 0) {
150  G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
151  }
152  if(fManager->CloseFile() && fVerbose > 0) {
153  G4cout << " File is closed" << G4endl;
154  }
156  fManager = 0;
157 }
158 
159 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
160 
161 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb,
162  G4double x1, G4double x2, G4double u)
163 {
164  if(fVerbose > 0) {
165  G4cout << "Histo::Add1D: New histogram will be booked: #"
166  << id << " <" << name
167  << " " << nb << " " << x1 << " " << x2 << " " << u
168  << G4endl;
169  }
170  ++fNHisto;
171  x1 /= u;
172  x2 /= u;
173  fActive.push_back(fDefaultAct);
174  fBins.push_back(nb);
175  fXmin.push_back(x1);
176  fXmax.push_back(x2);
177  fUnit.push_back(u);
178  fIds.push_back(id);
179  fTitles.push_back(name);
180  fHisto.push_back(-1);
181 }
182 
183 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
184 
185 void
187 {
188  if(i>=0 && i<fNHisto) {
189  if(fVerbose > 0) {
190  G4cout << "Histo::SetHisto1D: #" << i
191  << " " << nb << " " << x1 << " " << x2 << " " << u
192  << G4endl;
193  }
194  fBins[i] = nb;
195  fXmin[i] = x1;
196  fXmax[i] = x2;
197  fUnit[i] = u;
198  fActive[i] = true;
199  fHistoActive = true;
200  } else {
201  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index "
202  << i << G4endl;
203  }
204 }
205 
206 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
207 
208 void Histo::Activate(G4int i, G4bool val)
209 {
210  if(fVerbose > 1) {
211  G4cout << "Histo::Activate: Histogram: #" << i << " "
212  << val << G4endl;
213  }
214  if(i>=0 && i<fNHisto) {
215  fActive[i] = val;
216  if(val) { fHistoActive = true; }
217  }
218 }
219 
220 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
221 
222 void Histo::Fill(G4int i, G4double x, G4double w)
223 {
224  if(!fHistoActive) { return; }
225  if(fVerbose > 1) {
226  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
227  << " weight= " << w
228  << G4endl;
229  }
230  if(i>=0 && i<fNHisto) {
231  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
232  } else {
233  G4cout << "Histo::Fill: WARNING! wrong histogram index "
234  << i << G4endl;
235  }
236 }
237 
238 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
239 
240 void Histo::ScaleH1(G4int i, G4double x)
241 {
242  if(!fHistoActive) { return; }
243  if(fVerbose > 0) {
244  G4cout << "Histo::Scale: Histogram: #" << i << " by factor "
245  << x << G4endl;
246  }
247  if(i>=0 && i<fNHisto) {
248  if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
249  } else {
250  G4cout << "Histo::Scale: WARNING! wrong histogram index "
251  << i << G4endl;
252  }
253 }
254 
255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
256 
257 void Histo::AddTuple(const G4String& w1)
258 {
259  fTupleTitle = w1;
260 }
261 
262 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
263 
264 void Histo::AddTupleI(const G4String& w1)
265 {
266  fNtupleActive = true;
267  fNtupleI.push_back(w1);
268  fTupleI.push_back(-1);
269 }
270 
271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
272 
273 void Histo::AddTupleF(const G4String& w1)
274 {
275  fNtupleActive = true;
276  fNtupleF.push_back(w1);
277  fTupleF.push_back(-1);
278 }
279 
280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
281 
282 void Histo::AddTupleD(const G4String& w1)
283 {
284  fNtupleActive = true;
285  fNtupleD.push_back(w1);
286  fTupleD.push_back(-1);
287 }
288 
289 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
290 
291 void Histo::FillTupleI(G4int i, G4int x)
292 {
293  if(!fNtupleActive) { return; }
294  G4int n = fNtupleI.size();
295  if(i >= 0 && i < n) {
296  if(fVerbose > 1) {
297  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
298  << " <" << fNtupleI[i] << "> = " << x << G4endl;
299  }
301  } else {
302  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index "
303  << i << G4endl;
304  }
305 }
306 
307 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
308 
310 {
311  if(!fNtupleActive) { return; }
312  G4int n = fNtupleF.size();
313  if(i >= 0 && i < n) {
314  if(fVerbose > 1) {
315  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
316  << " <" << fNtupleF[i] << "> = " << x << G4endl;
317  }
319  } else {
320  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index "
321  << i << G4endl;
322  }
323 }
324 
325 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
326 
328 {
329  if(!fNtupleActive) { return; }
330  G4int n = fNtupleD.size();
331  if(i >= 0 && i < n) {
332  if(fVerbose > 1) {
333  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
334  << " <" << fNtupleD[i] << "> = " << x << G4endl;
335  }
337  } else {
338  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
339  << i << G4endl;
340  }
341 }
342 
343 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
344 
345 void Histo::AddRow()
346 {
347  if(!fNtupleActive) { return; }
349 }
350 
351 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
352 
353 void Histo::SetFileName(const G4String& nam)
354 {
355  fHistName = nam;
356  fHistoActive = true;
357 }
358 
359 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
360 
361 void Histo::SetFileType(const G4String& nam)
362 {
363  if(nam == "root" || nam == "ROOT" ) { fHistType = "root"; }
364  else if(nam == "xml" || nam == "XML") { fHistType = "xml"; }
365  else if(nam == "ascii" || nam == "ASCII" ||
366  nam == "Csv" || nam == "csv" || nam == "CSV")
367  { fHistType = "ascii"; }
368 }
369 
370 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
371 
void AddTupleD(const G4String &)
Definition: Histo.cc:269
void AddTuple(const G4String &)
Definition: Histo.cc:244
G4int CreateNtupleIColumn(const G4String &name)
~Histo()
Definition: Histo.cc:70
G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
void AddTupleI(const G4String &)
Definition: Histo.cc:251
Histo()
Definition: Histo.cc:51
G4String name
Definition: TRTMaterials.hh:40
float G4float
Definition: G4Types.hh:77
G4bool fHistoActive
Definition: Histo.hh:122
std::vector< G4int > fTupleF
Definition: Histo.hh:127
G4int CreateNtuple(const G4String &name, const G4String &title)
G4int fNHisto
Definition: Histo.hh:119
std::vector< G4int > fTupleD
Definition: Histo.hh:128
std::vector< G4String > fNtupleI
Definition: Histo.hh:136
void Activate(G4int, G4bool)
Definition: Histo.cc:198
std::vector< G4bool > fActive
Definition: Histo.hh:130
void Book()
Definition: Histo.cc:78
int G4int
Definition: G4Types.hh:78
G4String fTupleName
Definition: Histo.hh:117
std::vector< G4String > fIds
Definition: Histo.hh:134
std::vector< G4String > fTitles
Definition: Histo.hh:135
G4bool FillNtupleFColumn(G4int id, G4float value)
void Fill(G4int, G4double, G4double)
Definition: Histo.cc:212
G4bool fDefaultAct
Definition: Histo.hh:121
std::vector< G4double > fXmin
Definition: Histo.hh:131
void FillTupleF(G4int, G4float)
Definition: Histo.cc:295
G4bool FillNtupleIColumn(G4int id, G4int value)
std::vector< G4String > fNtupleD
Definition: Histo.hh:138
G4GLOB_DLL std::ostream G4cout
HistoMessenger * fMessenger
Definition: Histo.hh:113
G4bool FillNtupleDColumn(G4int id, G4double value)
bool G4bool
Definition: G4Types.hh:79
void ScaleH1(G4int, G4double)
Definition: Histo.cc:229
const G4int n
void Add1D(const G4String &, const G4String &, G4int nb, G4double x1, G4double x2, G4double u=1.)
Definition: Histo.cc:154
std::vector< G4int > fBins
Definition: Histo.hh:129
std::vector< G4int > fTupleI
Definition: Histo.hh:126
std::vector< G4String > fNtupleF
Definition: Histo.hh:137
std::vector< G4double > fUnit
Definition: Histo.hh:133
void AddTupleF(const G4String &)
Definition: Histo.cc:260
G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
void SetHisto1D(G4int, G4int, G4double, G4double, G4double)
Definition: Histo.cc:177
G4int fVerbose
Definition: Histo.hh:120
void FillTupleD(G4int, G4double)
Definition: Histo.cc:312
G4int CreateNtupleFColumn(const G4String &name)
static G4RootAnalysisManager * Instance()
G4String fHistType
Definition: Histo.hh:116
G4String fHistName
Definition: Histo.hh:115
#define G4endl
Definition: G4ios.hh:61
void AddRow()
Definition: Histo.cc:329
G4int CreateNtupleDColumn(const G4String &name)
std::vector< G4int > fHisto
Definition: Histo.hh:125
void SetFileType(const G4String &)
Definition: Histo.cc:345
double G4double
Definition: G4Types.hh:76
G4String fTupleTitle
Definition: Histo.hh:118
G4RootAnalysisManager * fManager
Definition: Histo.hh:108
void FillTupleI(G4int, G4int)
Definition: Histo.cc:278
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
void Save()
Definition: Histo.cc:130
G4bool fNtupleActive
Definition: Histo.hh:123
void SetFileName(const G4String &)
Definition: Histo.cc:337
std::vector< G4double > fXmax
Definition: Histo.hh:132