Geant4  10.02
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 //
30 // $Id: HistoManager.cc 92443 2015-09-01 13:56:16Z 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  : fFactoryOn(false)
43 {}
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
48 {}
49 
50 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
51 
52 void HistoManager::Book()
53 {
54  // Create or get analysis manager
55  // The choice of analysis technology is done via selection of a namespace
56  // in HistoManager.hh
57  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
58  analysisManager->SetVerboseLevel(1);
59 
60  // Create directories
61  analysisManager->SetHistoDirectoryName("histo");
62  analysisManager->SetNtupleDirectoryName("ntuple");
63 
64  // Open an output file
65  //
66  G4bool fileOpen = analysisManager->OpenFile("AnaEx01");
67  if (! fileOpen) {
68  G4cerr << "\n---> HistoManager::Book(): cannot open "
69  << analysisManager->GetFileName() << G4endl;
70  return;
71  }
72 
73  // Create histograms.
74  // Histogram ids are generated automatically starting from 0.
75  // The start value can be changed by:
76  // analysisManager->SetFirstHistoId(1);
77 
78  // id = 0
79  analysisManager->CreateH1("EAbs","Edep in absorber (MeV)", 100, 0., 800*MeV);
80  // id = 1
81  analysisManager->CreateH1("EGap","Edep in gap (MeV)", 100, 0., 100*MeV);
82  // id = 2
83  analysisManager->CreateH1("LAbs","trackL in absorber (mm)", 100, 0., 1*m);
84  // id = 3
85  analysisManager->CreateH1("LGap","trackL in gap (mm)", 100, 0., 50*cm);
86 
87  // Create ntuples.
88  // Ntuples ids are generated automatically starting from 0.
89  // The start value can be changed by:
90  // analysisManager->SetFirstMtupleId(1);
91 
92  // Create 1st ntuple (id = 0)
93  analysisManager->CreateNtuple("Ntuple1", "Edep");
94  analysisManager->CreateNtupleDColumn("Eabs"); // column Id = 0
95  analysisManager->CreateNtupleDColumn("Egap"); // column Id = 1
96  analysisManager->FinishNtuple();
97 
98  // Create 2nd ntuple (id = 1)
99  //
100  analysisManager->CreateNtuple("Ntuple2", "TrackL");
101  analysisManager->CreateNtupleDColumn("Labs"); // column Id = 0
102  analysisManager->CreateNtupleDColumn("Lgap"); // column Id = 1
103  analysisManager->FinishNtuple();
104 
105  fFactoryOn = true;
106 
107  G4cout << "\n----> Output file is open in "
108  << analysisManager->GetFileName() << "."
109  << analysisManager->GetFileType() << G4endl;
110 }
111 
112 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
113 
115 {
116  if (! fFactoryOn) return;
117 
118  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
119  analysisManager->Write();
120  analysisManager->CloseFile();
121 
122  G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
123 
124  delete G4AnalysisManager::Instance();
125  fFactoryOn = false;
126 }
127 
128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
129 
131 {
132  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
133  analysisManager->FillH1(ih, xbin, weight);
134 }
135 
136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
137 
139 {
140  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
141  G4H1* h1 = analysisManager->GetH1(ih);
142  if (h1) h1->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  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
151  // Fill 1st ntuple ( id = 0)
152  analysisManager->FillNtupleDColumn(0, 0, energyAbs);
153  analysisManager->FillNtupleDColumn(0, 1, energyGap);
154  analysisManager->AddNtupleRow(0);
155  // Fill 2nd ntuple ( id = 1)
156  analysisManager->FillNtupleDColumn(1, 0, trackLAbs);
157  analysisManager->FillNtupleDColumn(1, 1, trackLGap);
158  analysisManager->AddNtupleRow(1);
159 }
160 
161 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
162 
164 {
165  if (! fFactoryOn) return;
166 
167  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
168 
169  G4cout << "\n ----> print histograms statistic \n" << G4endl;
170  for ( G4int i=0; i<analysisManager->GetNofH1s(); ++i ) {
171  G4String name = analysisManager->GetH1Name(i);
172  G4H1* h1 = analysisManager->GetH1(i);
173 
174  G4String unitCategory;
175  if (name[0U] == 'E' ) unitCategory = "Energy";
176  if (name[0U] == 'L' ) unitCategory = "Length";
177  // we use an explicit unsigned int type for operator [] argument
178  // to avoid problems with windows compiler
179 
180  G4cout << name
181  << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
182  << " rms = " << G4BestUnit(h1->rms(), unitCategory )
183  << G4endl;
184  }
185 }
186 
187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
188 
189 
static const double cm
Definition: G4SIunits.hh:118
G4bool fFactoryOn
Definition: HistoManager.hh:65
static const double MeV
Definition: G4SIunits.hh:211
void FillHisto(G4int id, G4double e, G4double weight=1.0)
G4String name
Definition: TRTMaterials.hh:40
void PrintStatistic()
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
tools::hbook::h1 G4H1
Definition: g4hbook_defs.hh:45
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:66
static const G4double fac
#define G4endl
Definition: G4ios.hh:61
static const double m
Definition: G4SIunits.hh:128
void FillNtuple(G4double EnergyAbs, G4double EnergyGap, G4double TrackLAbs, G4double TrackLGap)
double G4double
Definition: G4Types.hh:76
G4GLOB_DLL std::ostream G4cerr