Geant4  9.6.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 65827 2012-11-29 10:21:47Z 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 :rootFile(0),ntupl(0), Eabs(0), Egap(0) ,Labs(0), Lgap(0)
47 {
48 
49  // histograms
50  for (G4int k=0; k<MaxHisto; k++) histo[k] = 0;
51 
52  // ntuple
53  ntupl = 0;
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 {
60  if ( rootFile ) delete rootFile;
61 }
62 
63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
64 
65 void HistoManager::book()
66 {
67  // Creating a tree container to handle histograms and ntuples.
68  // This tree is associated to an output file.
69  //
70  G4String fileName = "AnaEx02.root";
71  rootFile = new TFile(fileName,"RECREATE");
72  if(!rootFile) {
73  G4cout << " HistoManager::book :"
74  << " problem creating the ROOT TFile "
75  << G4endl;
76  return;
77  }
78 
79  histo[1] = new TH1D("1", "Edep in absorber", 100, 0., 800*CLHEP::MeV);
80  if (!histo[1]) G4cout << "\n can't create histo 1" << G4endl;
81  histo[2] = new TH1D("2", "Edep in gap", 100, 0., 100*CLHEP::MeV);
82  if (!histo[2]) G4cout << "\n can't create histo 2" << G4endl;
83  histo[3] = new TH1D("3", "trackL in absorber", 100, 0., 1*CLHEP::m);
84  if (!histo[3]) G4cout << "\n can't create histo 3" << G4endl;
85  histo[4] = new TH1D("4", "trackL in gap", 100, 0., 50*CLHEP::cm);
86  if (!histo[4]) G4cout << "\n can't create histo 4" << G4endl;
87 
88  // create 1 ntuple in subdirectory "tuples"
89  //
90  ntupl = new TTree("101", "Edep and TrackL");
91  ntupl->Branch("Eabs", &Eabs, "Eabs/D");
92  ntupl->Branch("Egap", &Egap, "Egap/D");
93  ntupl->Branch("Labs", &Labs, "Labs/D");
94  ntupl->Branch("Lgap", &Lgap, "Lgap/D");
95 
96 
97  G4cout << "\n----> Histogram file is opened in " << fileName << G4endl;
98 }
99 
100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
101 
102 void HistoManager::save()
103 {
104  if (rootFile) {
105  rootFile->Write(); // Writing the histograms to the file
106  rootFile->Close(); // and closing the tree (and the file)
107  G4cout << "\n----> Histogram Tree is saved \n" << G4endl;
108  }
109 }
110 
111 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
112 
114 {
115  if (ih >= MaxHisto) {
116  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
117  << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
118  << G4endl;
119  return;
120  }
121  if (histo[ih]) { histo[ih]->Fill(xbin, weight); }
122 }
123 
124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
125 
127 {
128  if (ih >= MaxHisto) {
129  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
130  << " does not exist. (fac=" << fac << ")" << G4endl;
131  return;
132  }
133  if (histo[ih]) histo[ih]->Scale(fac);
134 }
135 
136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
137 
138 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
139  G4double trackLAbs , G4double trackLGap )
140 {
141  Eabs = energyAbs;
142  Egap = energyGap;
143  Labs = trackLAbs;
144  Lgap = trackLGap;
145 
146  if (ntupl) ntupl->Fill();
147 }
148 
149 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
150 
152 {
153  if(histo[1]) {
154  G4cout << "\n ----> print histograms statistic \n" << G4endl;
155 
156  G4cout
157  << " EAbs : mean = " << G4BestUnit(histo[1]->GetMean(), "Energy")
158  << " rms = " << G4BestUnit(histo[1]->GetRMS(), "Energy") << G4endl;
159  G4cout
160  << " EGap : mean = " << G4BestUnit(histo[2]->GetMean(), "Energy")
161  << " rms = " << G4BestUnit(histo[2]->GetRMS(), "Energy") << G4endl;
162  G4cout
163  << " LAbs : mean = " << G4BestUnit(histo[3]->GetMean(), "Length")
164  << " rms = " << G4BestUnit(histo[3]->GetRMS(), "Length") << G4endl;
165  G4cout
166  << " LGap : mean = " << G4BestUnit(histo[4]->GetMean(), "Length")
167  << " rms = " << G4BestUnit(histo[4]->GetRMS(), "Length") << G4endl;
168 
169  }
170 }
171 
172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
173 
174