Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
extended/analysis/AnaEx02/src/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 //
26 /// \file analysis/AnaEx02/src/HistoManager.cc
27 /// \brief Implementation of the HistoManager class
28 //
29 // $Id: HistoManager.cc 74272 2013-10-02 14:48:50Z 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<MaxHisto; 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  fHisto[1] = new TH1D("1", "Edep in absorber", 100, 0., 800*CLHEP::MeV);
83  if (!fHisto[1]) G4cout << "\n can't create histo 1" << G4endl;
84  fHisto[2] = new TH1D("2", "Edep in gap", 100, 0., 100*CLHEP::MeV);
85  if (!fHisto[2]) G4cout << "\n can't create histo 2" << G4endl;
86  fHisto[3] = new TH1D("3", "trackL in absorber", 100, 0., 1*CLHEP::m);
87  if (!fHisto[3]) G4cout << "\n can't create histo 3" << G4endl;
88  fHisto[4] = new TH1D("4", "trackL in gap", 100, 0., 50*CLHEP::cm);
89  if (!fHisto[4]) G4cout << "\n can't create histo 4" << G4endl;
90 
91  // create 1st ntuple in subdirectory "tuples"
92  //
93  fNtuple1 = new TTree("101", "Edep");
94  fNtuple1->Branch("Eabs", &fEabs, "Eabs/D");
95  fNtuple1->Branch("Egap", &fEgap, "Egap/D");
96 
97  // create 2nd ntuple in subdirectory "tuples"
98  //
99  fNtuple2 = new TTree("102", "TrackL");
100  fNtuple2->Branch("Labs", &fLabs, "Labs/D");
101  fNtuple2->Branch("Lgap", &fLgap, "Lgap/D");
102 
103 
104  G4cout << "\n----> Histogram file is opened in " << fileName << G4endl;
105 }
106 
107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
108 
109 void HistoManager::save()
110 {
111  if (fRootFile) {
112  fRootFile->Write(); // Writing the histograms to the file
113  fRootFile->Close(); // and closing the tree (and the file)
114  G4cout << "\n----> Histogram Tree is saved \n" << G4endl;
115  }
116 }
117 
118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
119 
120 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
121 {
122  if (ih >= MaxHisto) {
123  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
124  << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
125  << G4endl;
126  return;
127  }
128  if (fHisto[ih]) { fHisto[ih]->Fill(xbin, weight); }
129 }
130 
131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
132 
134 {
135  if (ih >= MaxHisto) {
136  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
137  << " does not exist. (fac=" << fac << ")" << G4endl;
138  return;
139  }
140  if (fHisto[ih]) fHisto[ih]->Scale(fac);
141 }
142 
143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
144 
145 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
146  G4double trackLAbs , G4double trackLGap )
147 {
148  fEabs = energyAbs;
149  fEgap = energyGap;
150  fLabs = trackLAbs;
151  fLgap = trackLGap;
152 
153  if (fNtuple1) fNtuple1->Fill();
154  if (fNtuple2) fNtuple2->Fill();
155 }
156 
157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
158 
160 {
161  if(fHisto[1]) {
162  G4cout << "\n ----> print histograms statistic \n" << G4endl;
163 
164  G4cout
165  << " EAbs : mean = " << G4BestUnit(fHisto[1]->GetMean(), "Energy")
166  << " rms = " << G4BestUnit(fHisto[1]->GetRMS(), "Energy") << G4endl;
167  G4cout
168  << " EGap : mean = " << G4BestUnit(fHisto[2]->GetMean(), "Energy")
169  << " rms = " << G4BestUnit(fHisto[2]->GetRMS(), "Energy") << G4endl;
170  G4cout
171  << " LAbs : mean = " << G4BestUnit(fHisto[3]->GetMean(), "Length")
172  << " rms = " << G4BestUnit(fHisto[3]->GetRMS(), "Length") << G4endl;
173  G4cout
174  << " LGap : mean = " << G4BestUnit(fHisto[4]->GetMean(), "Length")
175  << " rms = " << G4BestUnit(fHisto[4]->GetRMS(), "Length") << G4endl;
176 
177  }
178 }
179 
180 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
181 
182 
void FillNtuple(G4int id, G4int column, G4double value)
void FillHisto(G4int id, G4double bin, G4double weight=1.0)
#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
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76