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