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