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 
47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
49 
51  : fManager(0),
52  fMessenger(0)
53 {
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
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 
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  G4Exception ("Histo::Save()", "hist01", FatalException,
146  "Cannot write ROOT file.");
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,
162 {
163  if(fVerbose > 0) {
164  G4cout << "Histo::Add1D: New histogram will be booked: #" << id
165  << " <" << 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 
185 {
186  if(i>=0 && i<fNHisto) {
187  if(fVerbose > 0) {
188  G4cout << "Histo::SetHisto1D: #" << i
189  << " " << nb << " " << x1 << " " << x2 << " " << u
190  << G4endl;
191  }
192  fBins[i] = nb;
193  fXmin[i] = x1;
194  fXmax[i] = x2;
195  fUnit[i] = u;
196  fActive[i] = true;
197  fHistoActive = true;
198  } else {
199  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index "
200  << i << G4endl;
201  }
202 }
203 
204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
205 
207 {
208  if(fVerbose > 1) {
209  G4cout << "Histo::Activate: Histogram: #" << i << " "
210  << val << G4endl;
211  }
212  if(i>=0 && i<fNHisto) {
213  fActive[i] = val;
214  if(val) { fHistoActive = true; }
215  }
216 }
217 
218 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
219 
221 {
222  if(!fHistoActive) { return; }
223  if(fVerbose > 1) {
224  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
225  << " weight= " << w
226  << G4endl;
227  }
228  if(i>=0 && i<fNHisto) {
229  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
230  } else {
231  G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
232  }
233 }
234 
235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
236 
238 {
239  if(!fHistoActive) { return; }
240  if(fVerbose > 0) {
241  G4cout << "Histo::Scale: Histogram: #"
242  << i << " by factor " << x << G4endl;
243  }
244  if(i>=0 && i<fNHisto) {
245  if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
246  } else {
247  G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
248  }
249 }
250 
251 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
252 
253 void Histo::AddTuple(const G4String& w1)
254 {
255  fTupleTitle = w1;
256 }
257 
258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
259 
260 void Histo::AddTupleI(const G4String& w1)
261 {
262  fNtupleActive = true;
263  fNtupleI.push_back(w1);
264  fTupleI.push_back(-1);
265 }
266 
267 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
268 
269 void Histo::AddTupleF(const G4String& w1)
270 {
271  fNtupleActive = true;
272  fNtupleF.push_back(w1);
273  fTupleF.push_back(-1);
274 }
275 
276 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
277 
278 void Histo::AddTupleD(const G4String& w1)
279 {
280  fNtupleActive = true;
281  fNtupleD.push_back(w1);
282  fTupleD.push_back(-1);
283 }
284 
285 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
286 
288 {
289  if(!fNtupleActive) { return; }
290  G4int n = fNtupleI.size();
291  if(i >= 0 && i < n) {
292  if(fVerbose > 1) {
293  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
294  << " <" << fNtupleI[i] << "> = " << x << G4endl;
295  }
297  } else {
298  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index "
299  << i << G4endl;
300  }
301 }
302 
303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
304 
306 {
307  if(!fNtupleActive) { return; }
308  G4int n = fNtupleF.size();
309  if(i >= 0 && i < n) {
310  if(fVerbose > 1) {
311  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
312  << " <" << fNtupleF[i] << "> = " << x << G4endl;
313  }
315  } else {
316  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index "
317  << i << G4endl;
318  }
319 }
320 
321 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
322 
324 {
325  if(!fNtupleActive) { return; }
326  G4int n = fNtupleD.size();
327  if(i >= 0 && i < n) {
328  if(fVerbose > 1) {
329  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
330  << " <" << fNtupleD[i] << "> = " << x << G4endl;
331  }
333  } else {
334  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
335  << i << G4endl;
336  }
337 }
338 
339 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
340 
342 {
343  if(!fNtupleActive) { return; }
345 }
346 
347 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
348 
349 void Histo::SetFileName(const G4String& nam)
350 {
351  fHistName = nam;
352  fHistoActive = true;
353 }
354 
355 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
356 
357 void Histo::SetFileType(const G4String& nam)
358 {
359  // format other than ROOT is not tested
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