ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistoManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HistoManager.cc
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 //
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 
34 #include "HistoManager.hh"
35 #include "G4UnitsTable.hh"
36 #include "G4SystemOfUnits.hh"
37 
38 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
39 
41  : fFactoryOn(false)
42 {}
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
47 {}
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
51 void HistoManager::Book()
52 {
53  // Create or get analysis manager
54  // The choice of analysis technology is done via selection of a namespace
55  // in HistoManager.hh
57  analysisManager->SetVerboseLevel(1);
58  analysisManager->SetNtupleMerging(true);
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 
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......