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