Geant4  10.02.p02
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 88154 2015-02-02 12:25:20Z 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 "G4XmlAnalysisManager.hh"
48 #include "G4ios.hh"
49 #include "G4SystemOfUnits.hh"
50 
51 
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
56 {
57  fManager = 0;
58  fMessenger = new HistoMessenger(this);
59 
60  fHistName = "test";
61  fHistType = "root";
62  fTupleName = "tuple";
63  fTupleTitle = "test";
64  fNHisto = 0;
65  fVerbose = 0;
66  fDefaultAct = true;
67  fHistoActive= false;
68  fNtupleActive= false;
69 }
70 
71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 
74 {
75  delete fMessenger;
76  delete fManager;
77 }
78 
79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
80 
81 void Histo::Book()
82 {
83  if(!(fHistoActive || fNtupleActive)) { return; }
84 
85  // Always creating analysis manager
86  if ( fHistType == "root" ) {
88  } else {
90  }
91 
92  // Creating a tree mapped to a new hbook file.
93  G4String nam = fHistName;// + "." + fHistType;
94 
95  // Open file histogram file
96  if(!fManager->OpenFile(nam)) {
97  G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
98  fHistoActive = false;
99  fNtupleActive = false;
100  return;
101  }
102  G4cout << "### Histo::Save: Opended file <" << nam << "> for "
103  << fNHisto << " histograms " << G4endl;
104 
105  // Creating an 1-dimensional histograms in the root directory of the tree
106  for(G4int i=0; i<fNHisto; ++i) {
107  if(fActive[i]) {
108  G4String ss = "h" + fIds[i];
109  fHisto[i] = fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
110  if(fVerbose > 0) {
111  G4cout << "Created histogram #" << i << " id= " << fHisto[i]
112  << " " << ss << " " << fTitles[i] << G4endl;
113  }
114  }
115  }
116  // Creating a tuple factory, whose tuples will be handled by the tree
117  if(fNtupleActive) {
119  G4int i;
120  G4int n = fNtupleI.size();
121  for(i=0; i<n; ++i) {
122  if(fTupleI[i] == -1) {
124  }
125  }
126  n = fNtupleF.size();
127  for(i=0; i<n; ++i) {
128  if(fTupleF[i] == -1) {
130  }
131  }
132  n = fNtupleD.size();
133  for(i=0; i<n; ++i) {
134  if(fTupleD[i] == -1) {
136  }
137  }
138  }
139 }
140 
141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
142 
143 void Histo::Save()
144 {
145  if(!(fHistoActive || fNtupleActive)) { return; }
146 
147  // Creating a tree mapped to a new hbook file.
148  G4String nam = fHistName + "." + fHistType;
149 
150  // Write histogram file
151  if(!fManager->Write()) {
152  G4ExceptionDescription message;
153  message << "Histo::Save: FATAL ERROR writing ROOT file.";
154  G4Exception("Histo::Save()",
155  "Hadr02Code001", FatalException, message);
156  }
157  if(fVerbose > 0) {
158  G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
159  }
160  if(fManager->CloseFile() && fVerbose > 0) {
161  G4cout << " File is closed" << G4endl;
162  }
164  fManager = 0;
165 }
166 
167 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
168 
169 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb,
170  G4double x1, G4double x2, G4double u)
171 {
172  if(fVerbose > 0) {
173  G4cout << "Histo::Add1D: New histogram will be booked: #"
174  << id << " <" << name
175  << " " << nb << " " << x1 << " " << x2 << " " << u
176  << G4endl;
177  }
178  ++fNHisto;
179  x1 /= u;
180  x2 /= u;
181  fActive.push_back(fDefaultAct);
182  fBins.push_back(nb);
183  fXmin.push_back(x1);
184  fXmax.push_back(x2);
185  fUnit.push_back(u);
186  fIds.push_back(id);
187  fTitles.push_back(name);
188  fHisto.push_back(-1);
189 }
190 
191 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
192 
193 void
195 {
196  if(i>=0 && i<fNHisto) {
197  if(fVerbose > 0) {
198  G4cout << "Histo::SetHisto1D: #" << i
199  << " " << nb << " " << x1 << " " << x2 << " " << u
200  << G4endl;
201  }
202  fBins[i] = nb;
203  fXmin[i] = x1;
204  fXmax[i] = x2;
205  fUnit[i] = u;
206  fActive[i] = true;
207  fHistoActive = true;
208  } else {
209  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index "
210  << i << G4endl;
211  }
212 }
213 
214 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
215 
216 void Histo::Activate(G4int i, G4bool val)
217 {
218  if(fVerbose > 1) {
219  G4cout << "Histo::Activate: Histogram: #" << i << " "
220  << val << G4endl;
221  }
222  if(i>=0 && i<fNHisto) {
223  fActive[i] = val;
224  if(val) { fHistoActive = true; }
225  }
226 }
227 
228 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
229 
231 {
232  if(!fHistoActive) { return; }
233  if(fVerbose > 1) {
234  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
235  << " weight= " << w
236  << G4endl;
237  }
238  if(i>=0 && i<fNHisto) {
239  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
240  } else {
241  G4cout << "Histo::Fill: WARNING! wrong histogram index "
242  << i << G4endl;
243  }
244 }
245 
246 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
247 
248 void Histo::ScaleH1(G4int i, G4double x)
249 {
250  if(!fHistoActive) { return; }
251  if(fVerbose > 0) {
252  G4cout << "Histo::Scale: Histogram: #" << i << " by factor "
253  << x << G4endl;
254  }
255  if(i>=0 && i<fNHisto) {
256  if(fActive[i]) {
257  if ( fHistType == "root" )
258  dynamic_cast<G4RootAnalysisManager*>(fManager)->GetH1(fHisto[i])->scale(x);
259  if ( fHistType == "xml" )
260  dynamic_cast<G4XmlAnalysisManager*>(fManager)->GetH1(fHisto[i])->scale(x);
261  }
262  } else {
263  G4cout << "Histo::Scale: WARNING! wrong histogram index "
264  << i << G4endl;
265  }
266 }
267 
268 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
269 
270 void Histo::AddTuple(const G4String& w1)
271 {
272  fTupleTitle = w1;
273 }
274 
275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
276 
277 void Histo::AddTupleI(const G4String& w1)
278 {
279  fNtupleActive = true;
280  fNtupleI.push_back(w1);
281  fTupleI.push_back(-1);
282 }
283 
284 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
285 
286 void Histo::AddTupleF(const G4String& w1)
287 {
288  fNtupleActive = true;
289  fNtupleF.push_back(w1);
290  fTupleF.push_back(-1);
291 }
292 
293 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
294 
295 void Histo::AddTupleD(const G4String& w1)
296 {
297  fNtupleActive = true;
298  fNtupleD.push_back(w1);
299  fTupleD.push_back(-1);
300 }
301 
302 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
303 
304 void Histo::FillTupleI(G4int i, G4int x)
305 {
306  if(!fNtupleActive) { return; }
307  G4int n = fNtupleI.size();
308  if(i >= 0 && i < n) {
309  if(fVerbose > 1) {
310  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
311  << " <" << fNtupleI[i] << "> = " << x << G4endl;
312  }
314  } else {
315  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index "
316  << i << G4endl;
317  }
318 }
319 
320 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
321 
323 {
324  if(!fNtupleActive) { return; }
325  G4int n = fNtupleF.size();
326  if(i >= 0 && i < n) {
327  if(fVerbose > 1) {
328  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
329  << " <" << fNtupleF[i] << "> = " << x << G4endl;
330  }
332  } else {
333  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index "
334  << i << G4endl;
335  }
336 }
337 
338 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
339 
341 {
342  if(!fNtupleActive) { return; }
343  G4int n = fNtupleD.size();
344  if(i >= 0 && i < n) {
345  if(fVerbose > 1) {
346  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
347  << " <" << fNtupleD[i] << "> = " << x << G4endl;
348  }
350  } else {
351  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
352  << i << G4endl;
353  }
354 }
355 
356 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
357 
358 void Histo::AddRow()
359 {
360  if(!fNtupleActive) { return; }
362 }
363 
364 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
365 
366 void Histo::SetFileName(const G4String& nam)
367 {
368  fHistName = nam;
369  fHistoActive = true;
370 }
371 
372 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
373 
374 void Histo::SetFileType(const G4String& nam)
375 {
376  if(nam == "root" || nam == "ROOT" ) { fHistType = "root"; }
377  else if(nam == "xml" || nam == "XML") { fHistType = "xml"; }
378  else {
379  G4cout<<"Histo::SetFileType: Sorry, format for output file: "<<nam
380  <<" not supported. Use \"root\" or \"xml\"" << G4endl;
381  }
382 }
383 
384 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
385 
void AddTupleD(const G4String &)
Definition: Histo.cc:279
void AddTuple(const G4String &)
Definition: Histo.cc:254
G4int CreateNtupleIColumn(const G4String &name)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
~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:261
Histo()
Definition: Histo.cc:51
G4String name
Definition: TRTMaterials.hh:40
float G4float
Definition: G4Types.hh:77
G4bool fHistoActive
Definition: Histo.hh:122
const G4double w[NPOINTSGL]
std::vector< G4int > fTupleF
Definition: Histo.hh:127
G4int CreateNtuple(const G4String &name, const G4String &title)
static G4XmlAnalysisManager * Instance()
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:207
std::vector< G4bool > fActive
Definition: Histo.hh:130
void Book()
Definition: Histo.cc:78
int G4int
Definition: G4Types.hh:78
G4bool OpenFile(const G4String &fileName="")
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:221
G4bool fDefaultAct
Definition: Histo.hh:121
std::vector< G4double > fXmin
Definition: Histo.hh:131
void FillTupleF(G4int, G4float)
Definition: Histo.cc:306
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:238
const G4int n
void Add1D(const G4String &, const G4String &, G4int nb, G4double x1, G4double x2, G4double u=1.)
Definition: Histo.cc:161
std::vector< G4int > fBins
Definition: Histo.hh:129
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
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:270
G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
void SetHisto1D(G4int, G4int, G4double, G4double, G4double)
Definition: Histo.cc:185
G4int fVerbose
Definition: Histo.hh:120
const G4double x[NPOINTSGL]
void FillTupleD(G4int, G4double)
Definition: Histo.cc:324
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:342
G4int CreateNtupleDColumn(const G4String &name)
std::vector< G4int > fHisto
Definition: Histo.hh:125
void SetFileType(const G4String &)
Definition: Histo.cc:358
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:288
void Save()
Definition: Histo.cc:137
G4bool fNtupleActive
Definition: Histo.hh:123
void SetFileName(const G4String &)
Definition: Histo.cc:350
std::vector< G4double > fXmax
Definition: Histo.hh:132