Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
extended/analysis/AnaEx01/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/AnaEx01/src/HistoManager.cc
27 /// \brief Implementation of the HistoManager class
28 //
29 //
30 // $Id: HistoManager.cc 74272 2013-10-02 14:48:50Z gcosmo $
31 //
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
34 
35 #include "HistoManager.hh"
36 #include "G4UnitsTable.hh"
37 #include "G4SystemOfUnits.hh"
38 
39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
40 
42 {
43  fFileName[0] = "AnaEx01";
44  fFactoryOn = false;
45 
46  // histograms
47  for (G4int k=0; k<MaxHisto; k++) {
48  fHistId[k] = 0;
49  fHistPt[k] = 0;
50  }
51  // ntuple
52  for (G4int k=0; k<MaxNtCol; k++) {
53  fNtColId[k] = 0;
54  }
55 }
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58 
60 { }
61 
62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
63 
64 void HistoManager::book()
65 {
66  // Create or get analysis manager
67  // The choice of analysis technology is done via selection of a namespace
68  // in HistoManager.hh
69  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
70  analysisManager->SetVerboseLevel(2);
71  G4String extension = analysisManager->GetFileType();
72  fFileName[1] = fFileName[0] + "." + extension;
73 
74  // Create directories
75  analysisManager->SetHistoDirectoryName("histo");
76  analysisManager->SetNtupleDirectoryName("ntuple");
77 
78  // Open an output file
79  //
80  G4bool fileOpen = analysisManager->OpenFile(fFileName[0]);
81  if (!fileOpen) {
82  G4cout << "\n---> HistoManager::book(): cannot open " << fFileName[1]
83  << G4endl;
84  return;
85  }
86 
87  // create selected histograms
88  //
89  analysisManager->SetFirstHistoId(1);
90  analysisManager->SetFirstNtupleId(1);
91 
92  fHistId[1] = analysisManager->CreateH1("1","Edep in absorber (MeV)",
93  100, 0., 800*MeV);
94  fHistPt[1] = analysisManager->GetH1(fHistId[1]);
95 
96  fHistId[2] = analysisManager->CreateH1("2","Edep in gap (MeV)",
97  100, 0., 100*MeV);
98  fHistPt[2] = analysisManager->GetH1(fHistId[2]);
99 
100  fHistId[3] = analysisManager->CreateH1("3","trackL in absorber (mm)",
101  100, 0., 1*m);
102  fHistPt[3] = analysisManager->GetH1(fHistId[3]);
103 
104  fHistId[4] = analysisManager->CreateH1("4","trackL in gap (mm)",
105  100, 0., 50*cm);
106  fHistPt[4] = analysisManager->GetH1(fHistId[4]);
107 
108  // Create 1st ntuple (id = 1)
109  //
110  analysisManager->CreateNtuple("101", "Edep");
111  fNtColId[0] = analysisManager->CreateNtupleDColumn("Eabs");
112  fNtColId[1] = analysisManager->CreateNtupleDColumn("Egap");
113  analysisManager->FinishNtuple();
114 
115  // Create 2nd ntuple (id = 2)
116  //
117  analysisManager->CreateNtuple("102", "TrackL");
118  fNtColId[2] = analysisManager->CreateNtupleDColumn("Labs");
119  fNtColId[3] = analysisManager->CreateNtupleDColumn("Lgap");
120  analysisManager->FinishNtuple();
121 
122  fFactoryOn = true;
123  G4cout << "\n----> Histogram Tree is opened in " << fFileName[1] << G4endl;
124 }
125 
126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
127 
128 void HistoManager::save()
129 {
130  if (fFactoryOn) {
131  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
132  analysisManager->Write();
133  analysisManager->CloseFile();
134  G4cout << "\n----> Histogram Tree is saved in " << fFileName[1] << G4endl;
135 
136  delete G4AnalysisManager::Instance();
137  fFactoryOn = false;
138  }
139 }
140 
141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
142 
143 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
144 {
145  if (ih > MaxHisto) {
146  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
147  << "does note xist; xbin= " << xbin << " w= " << weight << G4endl;
148  return;
149  }
150 
151  if (fHistPt[ih]) fHistPt[ih]->fill(xbin, weight);
152 }
153 
154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
155 
157 {
158  if (ih >= MaxHisto) {
159  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
160  << " fac= " << fac << G4endl;
161  return;
162  }
163 
164  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
165 }
166 
167 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
168 
169 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap,
170  G4double trackLAbs, G4double trackLGap)
171 {
172  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
173  // Fill 1st ntuple ( id = 1)
174  analysisManager->FillNtupleDColumn(1,fNtColId[0], energyAbs);
175  analysisManager->FillNtupleDColumn(1,fNtColId[1], energyGap);
176  analysisManager->AddNtupleRow(1);
177  // Fill 2nd ntuple ( id = 2)
178  analysisManager->FillNtupleDColumn(2,fNtColId[2], trackLAbs);
179  analysisManager->FillNtupleDColumn(2,fNtColId[3], trackLGap);
180  analysisManager->AddNtupleRow(2);
181 }
182 
183 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
184 
186 {
187  if(fFactoryOn) {
188  G4cout << "\n ----> print histograms statistic \n" << G4endl;
189 
190  G4cout
191  << " EAbs : mean = " << G4BestUnit(fHistPt[1]->mean(), "Energy")
192  << " rms = " << G4BestUnit(fHistPt[1]->rms(), "Energy")
193  << G4endl;
194  G4cout
195  << " EGap : mean = " << G4BestUnit(fHistPt[2]->mean(), "Energy")
196  << " rms = " << G4BestUnit(fHistPt[2]->rms(), "Energy")
197  << G4endl;
198  G4cout
199  << " LAbs : mean = " << G4BestUnit(fHistPt[3]->mean(), "Length")
200  << " rms = " << G4BestUnit(fHistPt[3]->rms(), "Length")
201  << G4endl;
202  G4cout
203  << " LGap : mean = " << G4BestUnit(fHistPt[4]->mean(), "Length")
204  << " rms = " << G4BestUnit(fHistPt[4]->rms(), "Length")
205  << G4endl;
206  }
207 }
208 
209 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
210 
211 
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
bool G4bool
Definition: G4Types.hh:79
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:46
const G4int MaxNtCol
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76