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(nullptr),
52  fHistName("test"),
53  fTupleName("tuple"),
54  fTupleTitle("test"),
55  fNHisto(0),
56  fVerbose(0),
57  fDefaultAct(true),
58  fHistoActive(false),
59  fNtupleActive(false)
60 {
61  fMessenger = new HistoMessenger(this);
62 }
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
65 
67 {
68  delete fMessenger;
69  delete fManager;
70 }
71 
72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
73 
74 void Histo::Book()
75 {
76  if(!(fHistoActive || fNtupleActive)) { return; }
77 
78  // Always creating analysis manager
80 
81  // Creating a tree mapped to a new hbook file.
82  G4String nam = fHistName + ".root";
83 
84  // Open file histogram file
85  if(!fManager->OpenFile(nam)) {
86  G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
87  fHistoActive = false;
88  fNtupleActive = false;
89  return;
90  }
91  G4cout << "### Histo::Save: Opended file <" << nam << "> for "
92  << fNHisto << " histograms " << G4endl;
93 
94  // Creating an 1-dimensional histograms in the root directory of the tree
95  for(G4int i=0; i<fNHisto; ++i) {
96  if(fActive[i]) {
97  G4String ss = "h" + fIds[i];
98  fHisto[i] =
99  fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
100  if(fVerbose > 0) {
101  G4cout << "Created histogram #" << i << " id= " << fHisto[i]
102  << " " << ss << " " << fTitles[i] << G4endl;
103  }
104  }
105  }
106  // Creating a tuple factory, whose tuples will be handled by the tree
107  if(fNtupleActive) {
109  G4int i;
110  G4int n = fNtupleI.size();
111  for(i=0; i<n; ++i) {
112  if(fTupleI[i] == -1) {
114  }
115  }
116  n = fNtupleF.size();
117  for(i=0; i<n; ++i) {
118  if(fTupleF[i] == -1) {
120  }
121  }
122  n = fNtupleD.size();
123  for(i=0; i<n; ++i) {
124  if(fTupleD[i] == -1) {
126  }
127  }
128  }
129 }
130 
131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
132 
133 void Histo::Save()
134 {
135  if(!(fHistoActive || fNtupleActive)) { return; }
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: #"
159  << id << " <" << name
160  << " " << nb << " " << x1 << " " << x2 << " " << u
161  << G4endl;
162  }
163  ++fNHisto;
164  x1 /= u;
165  x2 /= u;
166  fActive.push_back(fDefaultAct);
167  fBins.push_back(nb);
168  fXmin.push_back(x1);
169  fXmax.push_back(x2);
170  fUnit.push_back(u);
171  fIds.push_back(id);
172  fTitles.push_back(name);
173  fHisto.push_back(-1);
174 }
175 
176 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
177 
178 void
180 {
181  if(i>=0 && i<fNHisto) {
182  if(fVerbose > 0) {
183  G4cout << "Histo::SetHisto1D: #" << i
184  << " " << nb << " " << x1 << " " << x2 << " " << u
185  << G4endl;
186  }
187  fBins[i] = nb;
188  fXmin[i] = x1;
189  fXmax[i] = x2;
190  fUnit[i] = u;
191  fActive[i] = true;
192  fHistoActive = true;
193  } else {
194  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index "
195  << i << G4endl;
196  }
197 }
198 
199 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
200 
201 void Histo::Activate(G4int i, G4bool val)
202 {
203  if(fVerbose > 1) {
204  G4cout << "Histo::Activate: Histogram: #" << i << " "
205  << val << G4endl;
206  }
207  if(i>=0 && i<fNHisto) {
208  fActive[i] = val;
209  if(val) { fHistoActive = true; }
210  }
211 }
212 
213 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
214 
216 {
217  if(!fHistoActive) { return; }
218  if(fVerbose > 1) {
219  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
220  << " weight= " << w
221  << G4endl;
222  }
223  if(i>=0 && i<fNHisto) {
224  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
225  } else {
226  G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
227  }
228 }
229 
230 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
231 
232 void Histo::ScaleH1(G4int i, G4double x)
233 {
234  if(!fHistoActive) { return; }
235  if(fVerbose > 0) {
236  G4cout << "Histo::Scale: Histogram: #" << i
237  << " by factor " << x << G4endl;
238  }
239  if(i>=0 && i<fNHisto) {
240  if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
241  } else {
242  G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
243  }
244 }
245 
246 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
247 
248 void Histo::AddTuple(const G4String& w1)
249 {
250  fTupleTitle = w1;
251 }
252 
253 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
254 
255 void Histo::AddTupleI(const G4String& w1)
256 {
257  fNtupleActive = true;
258  fNtupleI.push_back(w1);
259  fTupleI.push_back(-1);
260 }
261 
262 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
263 
264 void Histo::AddTupleF(const G4String& w1)
265 {
266  fNtupleActive = true;
267  fNtupleF.push_back(w1);
268  fTupleF.push_back(-1);
269 }
270 
271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
272 
273 void Histo::AddTupleD(const G4String& w1)
274 {
275  fNtupleActive = true;
276  fNtupleD.push_back(w1);
277  fTupleD.push_back(-1);
278 }
279 
280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
281 
282 void Histo::FillTupleI(G4int i, G4int x)
283 {
284  if(!fNtupleActive) { return; }
285  G4int n = fNtupleI.size();
286  if(i >= 0 && i < n) {
287  if(fVerbose > 1) {
288  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
289  << " <" << fNtupleI[i] << "> = " << x << G4endl;
290  }
292  } else {
293  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index "
294  << i << G4endl;
295  }
296 }
297 
298 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
299 
301 {
302  if(!fNtupleActive) { return; }
303  G4int n = fNtupleF.size();
304  if(i >= 0 && i < n) {
305  if(fVerbose > 1) {
306  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
307  << " <" << fNtupleF[i] << "> = " << x << G4endl;
308  }
310  } else {
311  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index "
312  << i << G4endl;
313  }
314 }
315 
316 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
317 
319 {
320  if(!fNtupleActive) { return; }
321  G4int n = fNtupleD.size();
322  if(i >= 0 && i < n) {
323  if(fVerbose > 1) {
324  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
325  << " <" << fNtupleD[i] << "> = " << x << G4endl;
326  }
328  } else {
329  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
330  << i << G4endl;
331  }
332 }
333 
334 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
335 
336 void Histo::AddRow()
337 {
338  if(!fNtupleActive) { return; }
340 }
341 
342 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
343 
344 void Histo::SetFileName(const G4String& nam)
345 {
346  fHistName = nam;
347  fHistoActive = true;
348 }
349 
350 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
351