Geant4_10
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 68036 2013-03-13 14:13:45Z 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 
78 void Histo::Book()
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] = 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) {
111  fManager->CreateNtuple(fTupleName,fTupleTitle);
112  G4int i;
113  G4int n = fNtupleI.size();
114  for(i=0; i<n; ++i) {
115  if(fTupleI[i] == -1) { fTupleI[i] = fManager->CreateNtupleIColumn(fNtupleI[i]); }
116  }
117  n = fNtupleF.size();
118  for(i=0; i<n; ++i) {
119  if(fTupleF[i] == -1) { fTupleF[i] = fManager->CreateNtupleFColumn(fNtupleF[i]); }
120  }
121  n = fNtupleD.size();
122  for(i=0; i<n; ++i) {
123  if(fTupleD[i] == -1) { fTupleD[i] = fManager->CreateNtupleDColumn(fNtupleD[i]); }
124  }
125  }
126 }
127 
128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
129 
130 void Histo::Save()
131 {
132  if(!(fHistoActive || fNtupleActive)) { return; }
133 
134  // Creating a tree mapped to a new hbook file.
135  G4String nam = fHistName + "." + fHistType;
136 
137  // Write histogram file
138  if(!fManager->Write()) {
139  G4cout << "Histo::Save: FATAL ERROR writing ROOT file" << G4endl;
140  exit(1);
141  }
142  if(fVerbose > 0) {
143  G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
144  }
145  if(fManager->CloseFile() && fVerbose > 0) {
146  G4cout << " File is closed" << G4endl;
147  }
149  fManager = 0;
150 }
151 
152 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
153 
154 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb,
156 {
157  if(fVerbose > 0) {
158  G4cout << "Histo::Add1D: New histogram will be booked: #" << id << " <" << name
159  << " " << nb << " " << x1 << " " << x2 << " " << u
160  << G4endl;
161  }
162  ++fNHisto;
163  x1 /= u;
164  x2 /= u;
165  fActive.push_back(fDefaultAct);
166  fBins.push_back(nb);
167  fXmin.push_back(x1);
168  fXmax.push_back(x2);
169  fUnit.push_back(u);
170  fIds.push_back(id);
171  fTitles.push_back(name);
172  fHisto.push_back(-1);
173 }
174 
175 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
176 
177 void Histo::SetHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u)
178 {
179  if(i>=0 && i<fNHisto) {
180  if(fVerbose > 0) {
181  G4cout << "Histo::SetHisto1D: #" << i
182  << " " << nb << " " << x1 << " " << x2 << " " << u
183  << G4endl;
184  }
185  fBins[i] = nb;
186  fXmin[i] = x1;
187  fXmax[i] = x2;
188  fUnit[i] = u;
189  fActive[i] = true;
190  fHistoActive = true;
191  } else {
192  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index " << i << G4endl;
193  }
194 }
195 
196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
197 
198 void Histo::Activate(G4int i, G4bool val)
199 {
200  if(fVerbose > 1) {
201  G4cout << "Histo::Activate: Histogram: #" << i << " "
202  << val << G4endl;
203  }
204  if(i>=0 && i<fNHisto) {
205  fActive[i] = val;
206  if(val) { fHistoActive = true; }
207  }
208 }
209 
210 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
211 
212 void Histo::Fill(G4int i, G4double x, G4double w)
213 {
214  if(!fHistoActive) { return; }
215  if(fVerbose > 1) {
216  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
217  << " weight= " << w
218  << G4endl;
219  }
220  if(i>=0 && i<fNHisto) {
221  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
222  } else {
223  G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
224  }
225 }
226 
227 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
228 
229 void Histo::ScaleH1(G4int i, G4double x)
230 {
231  if(!fHistoActive) { return; }
232  if(fVerbose > 0) {
233  G4cout << "Histo::Scale: Histogram: #" << i << " by factor " << x << G4endl;
234  }
235  if(i>=0 && i<fNHisto) {
236  if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
237  } else {
238  G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
239  }
240 }
241 
242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
243 
244 void Histo::AddTuple(const G4String& w1)
245 {
246  fTupleTitle = w1;
247 }
248 
249 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
250 
251 void Histo::AddTupleI(const G4String& w1)
252 {
253  fNtupleActive = true;
254  fNtupleI.push_back(w1);
255  fTupleI.push_back(-1);
256 }
257 
258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
259 
260 void Histo::AddTupleF(const G4String& w1)
261 {
262  fNtupleActive = true;
263  fNtupleF.push_back(w1);
264  fTupleF.push_back(-1);
265 }
266 
267 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
268 
269 void Histo::AddTupleD(const G4String& w1)
270 {
271  fNtupleActive = true;
272  fNtupleD.push_back(w1);
273  fTupleD.push_back(-1);
274 }
275 
276 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
277 
278 void Histo::FillTupleI(G4int i, G4int x)
279 {
280  if(!fNtupleActive) { return; }
281  G4int n = fNtupleI.size();
282  if(i >= 0 && i < n) {
283  if(fVerbose > 1) {
284  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
285  << " <" << fNtupleI[i] << "> = " << x << G4endl;
286  }
287  fManager->FillNtupleIColumn(fTupleI[i], x);
288  } else {
289  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index " << i << G4endl;
290  }
291 }
292 
293 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
294 
296 {
297  if(!fNtupleActive) { return; }
298  G4int n = fNtupleF.size();
299  if(i >= 0 && i < n) {
300  if(fVerbose > 1) {
301  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
302  << " <" << fNtupleF[i] << "> = " << x << G4endl;
303  }
304  fManager->FillNtupleFColumn(fTupleF[i], x);
305  } else {
306  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index " << i << G4endl;
307  }
308 }
309 
310 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
311 
313 {
314  if(!fNtupleActive) { return; }
315  G4int n = fNtupleD.size();
316  if(i >= 0 && i < n) {
317  if(fVerbose > 1) {
318  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
319  << " <" << fNtupleD[i] << "> = " << x << G4endl;
320  }
321  fManager->FillNtupleDColumn(fTupleD[i], x);
322  } else {
323  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index " << i << G4endl;
324  }
325 }
326 
327 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
328 
329 void Histo::AddRow()
330 {
331  if(!fNtupleActive) { return; }
332  fManager->AddNtupleRow();
333 }
334 
335 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
336 
337 void Histo::SetFileName(const G4String& nam)
338 {
339  fHistName = nam;
340  fHistoActive = true;
341 }
342 
343 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
344 
345 void Histo::SetFileType(const G4String& nam)
346 {
347  if(nam == "root" || nam == "ROOT" ) { fHistType = "root"; }
348  else if(nam == "xml" || nam == "XML") { fHistType = "xml"; }
349  else if(nam == "ascii" || nam == "ASCII" ||
350  nam == "Csv" || nam == "csv" || nam == "CSV") { fHistType = "ascii"; }
351 }
352 
353 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
354 
void AddTupleD(const G4String &)
Definition: Histo.cc:269
void AddTuple(const G4String &)
Definition: Histo.cc:244
G4int CreateNtupleIColumn(const G4String &name)
Double_t x2[nxs]
Definition: Style.C:19
~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
float G4float
Definition: G4Types.hh:77
G4int CreateNtuple(const G4String &name, const G4String &title)
const XML_Char * name
Definition: expat.h:151
void Activate(G4int, G4bool)
Definition: Histo.cc:198
tuple x
Definition: test.py:50
void Book()
Definition: Histo.cc:78
int G4int
Definition: G4Types.hh:78
G4bool FillNtupleFColumn(G4int id, G4float value)
void Fill(G4int, G4double, G4double)
Definition: Histo.cc:212
void FillTupleF(G4int, G4float)
Definition: Histo.cc:295
Char_t n[5]
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:229
Double_t x1[nxs]
Definition: Style.C:18
void Add1D(const G4String &, const G4String &, G4int nb, G4double x1, G4double x2, G4double u=1.)
Definition: Histo.cc:154
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
void FillTupleD(G4int, G4double)
Definition: Histo.cc:312
G4int CreateNtupleFColumn(const G4String &name)
static G4RootAnalysisManager * Instance()
#define G4endl
Definition: G4ios.hh:61
void AddRow()
Definition: Histo.cc:329
G4int CreateNtupleDColumn(const G4String &name)
void SetFileType(const G4String &)
Definition: Histo.cc:345
double G4double
Definition: G4Types.hh:76
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
void SetFileName(const G4String &)
Definition: Histo.cc:337