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