Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistoManager.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 //
28 //
29 // $Id: HistoManager.cc 98060 2016-07-01 16:24:08Z gcosmo $
30 // GEANT4 tag $Name: geant4-09-04 $
31 //
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
34 
35 #include <TH1D.h>
36 #include <TFile.h>
37 #include <TTree.h>
39 
40 #include "HistoManager.hh"
41 #include "G4UnitsTable.hh"
42 
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44 
46 :fRootFile(0),
47  fNtuple1(0), fNtuple2(0),
48  fEabs(0.), fEgap(0.) ,fLabs(0.), fLgap(0.)
49 {
50 
51  // histograms
52  for (G4int k=0; k<kMaxHisto; k++) fHisto[k] = 0;
53 
54  // ntuple
55  fNtuple1 = 0;
56  fNtuple2 = 0;
57 }
58 
59 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
60 
62 {
63  if (fRootFile) delete fRootFile;
64 }
65 
66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 
68 void HistoManager::Book()
69 {
70  // Creating a tree container to handle histograms and ntuples.
71  // This tree is associated to an output file.
72  //
73  G4String fileName = "AnaEx02.root";
74  fRootFile = new TFile(fileName,"RECREATE");
75  if (! fRootFile) {
76  G4cout << " HistoManager::Book :"
77  << " problem creating the ROOT TFile "
78  << G4endl;
79  return;
80  }
81 
82  // id = 0
83  fHisto[0] = new TH1D("EAbs", "Edep in absorber (MeV)", 100, 0., 800*CLHEP::MeV);
84  // id = 1
85  fHisto[1] = new TH1D("EGap", "Edep in gap (MeV)", 100, 0., 100*CLHEP::MeV);
86  // id = 2
87  fHisto[2] = new TH1D("LAbs", "trackL in absorber (mm)", 100, 0., 1*CLHEP::m);
88  // id = 3
89  fHisto[3] = new TH1D("LGap", "trackL in gap (mm)", 100, 0., 50*CLHEP::cm);
90 
91  for ( G4int i=0; i<kMaxHisto; ++i ) {
92  if (! fHisto[i]) G4cout << "\n can't create histo " << i << G4endl;
93  }
94 
95  // create 1st ntuple
96  fNtuple1 = new TTree("Ntuple1", "Edep");
97  fNtuple1->Branch("Eabs", &fEabs, "Eabs/D");
98  fNtuple1->Branch("Egap", &fEgap, "Egap/D");
99 
100  // create 2nd ntuple
101  fNtuple2 = new TTree("Ntuple2", "TrackL");
102  fNtuple2->Branch("Labs", &fLabs, "Labs/D");
103  fNtuple2->Branch("Lgap", &fLgap, "Lgap/D");
104 
105  G4cout << "\n----> Output file is open in " << fileName << G4endl;
106 }
107 
108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
109 
110 void HistoManager::Save()
111 {
112  if (! fRootFile) return;
113 
114  fRootFile->Write(); // Writing the histograms to the file
115  fRootFile->Close(); // and closing the tree (and the file)
116 
117  G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
121 
122 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
123 {
124  if (ih >= kMaxHisto) {
125  G4cerr << "---> warning from HistoManager::FillHisto() : histo " << ih
126  << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
127  << G4endl;
128  return;
129  }
130  if (fHisto[ih]) { fHisto[ih]->Fill(xbin, weight); }
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
134 
136 {
137  if (ih >= kMaxHisto) {
138  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
139  << " does not exist. (fac=" << fac << ")" << G4endl;
140  return;
141  }
142  if (fHisto[ih]) fHisto[ih]->Scale(fac);
143 }
144 
145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
146 
147 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
148  G4double trackLAbs , G4double trackLGap )
149 {
150  fEabs = energyAbs;
151  fEgap = energyGap;
152  fLabs = trackLAbs;
153  fLgap = trackLGap;
154 
155  if (fNtuple1) fNtuple1->Fill();
156  if (fNtuple2) fNtuple2->Fill();
157 }
158 
159 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
160 
162 {
163  G4cout << "\n ----> print histograms statistic \n" << G4endl;
164  for ( G4int i=0; i<kMaxHisto; ++i ) {
165  TH1D* h1 = fHisto[i];
166  const G4String name = h1->GetName();
167 
168  G4String unitCategory;
169  if (name[0] == 'E' ) unitCategory = "Energy";
170  if (name[0] == 'L' ) unitCategory = "Length";
171 
172  G4cout << name
173  << ": mean = " << G4BestUnit(h1->GetMean(), unitCategory)
174  << " rms = " << G4BestUnit(h1->GetRMS(), unitCategory )
175  << G4endl;
176  }
177 }
178 
179 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
180 
181 
static constexpr double m
const XML_Char * name
Definition: expat.h:151
static constexpr double cm
Definition: SystemOfUnits.h:99
void FillHisto(G4int id, G4double e, G4double weight=1.0)
void PrintStatistic()
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
void Normalize(G4int id, G4double fac)
G4GLOB_DLL std::ostream G4cout
static constexpr double MeV
static const G4double fac
#define G4endl
Definition: G4ios.hh:61
void FillNtuple(G4double EnergyAbs, G4double EnergyGap, G4double TrackLAbs, G4double TrackLGap)
double G4double
Definition: G4Types.hh:76
const G4int kMaxHisto
Definition: HistoManager.hh:46
G4GLOB_DLL std::ostream G4cerr