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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

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