Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 98275 2016-07-04 17:59:05Z 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 {
55  fMessenger = new HistoMessenger(this);
56 
57  fHistName = "test";
58  fHistType = "root";
59  fTupleName = "tuple";
60  fTupleTitle = "test";
61  fNHisto = 0;
62  fVerbose = 0;
63  fDefaultAct = true;
64  fHistoActive= false;
65  fNtupleActive= false;
66 }
67 
68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69 
71 {
72  delete fMessenger;
73  delete fManager;
74 }
75 
76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
77 
79 {
80  if(!(fHistoActive || fNtupleActive)) { return; }
81 
82  // Always creating analysis manager
83  fManager = G4RootAnalysisManager::Instance();
84 
85  // Creating a tree mapped to a new hbook file.
86  G4String nam = fHistName + "." + fHistType;
87 
88  // Open file histogram file
89  if(!fManager->OpenFile(nam)) {
90  G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
91  fHistoActive = false;
92  fNtupleActive = false;
93  return;
94  }
95  G4cout << "### Histo::Save: Opended file <" << nam << "> for "
96  << fNHisto << " histograms " << G4endl;
97 
98  // Creating an 1-dimensional histograms in the root directory of the tree
99  for(G4int i=0; i<fNHisto; ++i) {
100  if(fActive[i]) {
101  G4String ss = "h" + fIds[i];
102  fHisto[i] =
103  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) {
112  fManager->CreateNtuple(fTupleName,fTupleTitle);
113  G4int i;
114  G4int n = fNtupleI.size();
115  for(i=0; i<n; ++i) {
116  if(fTupleI[i] == -1) {
117  fTupleI[i] = fManager->CreateNtupleIColumn(fNtupleI[i]);
118  }
119  }
120  n = fNtupleF.size();
121  for(i=0; i<n; ++i) {
122  if(fTupleF[i] == -1) {
123  fTupleF[i] = fManager->CreateNtupleFColumn(fNtupleF[i]);
124  }
125  }
126  n = fNtupleD.size();
127  for(i=0; i<n; ++i) {
128  if(fTupleD[i] == -1) {
129  fTupleD[i] = fManager->CreateNtupleDColumn(fNtupleD[i]);
130  }
131  }
132  }
133 }
134 
135 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
136 
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  G4Exception ("Histo::Save()", "hist01", FatalException,
147  "Cannot write ROOT file.");
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: #" << id
166  << " <" << 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 
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 
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 
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 
239 {
240  if(!fHistoActive) { return; }
241  if(fVerbose > 0) {
242  G4cout << "Histo::Scale: Histogram: #"
243  << i << " 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 
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  }
297  fManager->FillNtupleIColumn(fTupleI[i], x);
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  }
315  fManager->FillNtupleFColumn(fTupleF[i], x);
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  }
333  fManager->FillNtupleDColumn(fTupleD[i], x);
334  } else {
335  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
336  << i << G4endl;
337  }
338 }
339 
340 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
341 
343 {
344  if(!fNtupleActive) { return; }
345  fManager->AddNtupleRow();
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  // format other than ROOT is not tested
361  if(nam == "root" || nam == "ROOT" ) { fHistType = "root"; }
362  else if(nam == "xml" || nam == "XML") { fHistType = "xml"; }
363  else if(nam == "ascii" || nam == "ASCII" ||
364  nam == "Csv" || nam == "csv" || nam == "CSV")
365  { fHistType = "ascii"; }
366 }
367 
368 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
369 
void AddTupleD(const G4String &)
Definition: Histo.cc:279
void AddTuple(const G4String &)
Definition: Histo.cc:254
const XML_Char * name
Definition: expat.h:151
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:261
Histo()
Definition: Histo.cc:51
float G4float
Definition: G4Types.hh:77
G4int CreateNtuple(const G4String &name, const G4String &title)
void Activate(G4int, G4bool)
Definition: Histo.cc:207
tuple x
Definition: test.py:50
void Book()
Definition: Histo.cc:78
int G4int
Definition: G4Types.hh:78
G4bool OpenFile(const G4String &fileName="")
G4bool FillNtupleFColumn(G4int id, G4float value)
void Fill(G4int, G4double, G4double)
Definition: Histo.cc:221
void FillTupleF(G4int, G4float)
Definition: Histo.cc:306
G4bool FillNtupleIColumn(G4int id, G4int value)
G4GLOB_DLL std::ostream G4cout
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
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
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
void FillTupleD(G4int, G4double)
Definition: Histo.cc:324
G4int CreateNtupleFColumn(const G4String &name)
static G4RootAnalysisManager * Instance()
#define G4endl
Definition: G4ios.hh:61
void AddRow()
Definition: Histo.cc:342
G4int CreateNtupleDColumn(const G4String &name)
void SetFileType(const G4String &)
Definition: Histo.cc:358
double G4double
Definition: G4Types.hh:76
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
void FillTupleI(G4int, G4int)
Definition: Histo.cc:288
void Save()
Definition: Histo.cc:137
void SetFileName(const G4String &)
Definition: Histo.cc:350