Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
hadronic/Hadr01/src/Histo.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 // $Id: Histo.cc 107541 2017-11-22 08:24:57Z gcosmo $
30 //
31 //---------------------------------------------------------------------------
32 //
33 // ClassName: Histo - Generic histogram/ntuple manager class
34 //
35 //
36 // Author: V.Ivanchenko 30.10.03
37 //
38 //----------------------------------------------------------------------------
39 //
40 
41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 
44 #include "Histo.hh"
45 #include "HistoMessenger.hh"
46 #include "G4RootAnalysisManager.hh"
47 
48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52 : fManager(nullptr),
53  fHistName("test"),
54  fTupleName("tuple"),
55  fTupleTitle("test"),
56  fNHisto(0),
57  fVerbose(0),
58  fDefaultAct(true),
59  fHistoActive(false),
60  fNtupleActive(false)
61 {
62  fMessenger = new HistoMessenger(this);
63 }
64 
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
66 
68 {
69  delete fMessenger;
70  delete fManager;
71 }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 
75 void Histo::Book()
76 {
77  if(!(fHistoActive || fNtupleActive)) { return; }
78 
79  // Always creating analysis manager
81 
82  // Creating a tree mapped to a new hbook file.
83  G4String nam = fHistName + ".root";
84 
85  // Open file histogram file
86  if(!fManager->OpenFile(nam)) {
87  G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
88  fHistoActive = false;
89  fNtupleActive = false;
90  return;
91  }
92  G4cout << "### Histo::Save: Opended file <" << nam << "> for "
93  << fNHisto << " histograms " << G4endl;
94 
95  // Creating an 1-dimensional histograms in the root directory of the tree
96  for(G4int i=0; i<fNHisto; ++i) {
97  if(fActive[i]) {
98  G4String ss = "h" + fIds[i];
99  fHisto[i] =
100  fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
101  if(fVerbose > 0) {
102  G4cout << "Created histogram #" << i << " id= " << fHisto[i]
103  << " " << ss << " " << fTitles[i] << G4endl;
104  }
105  }
106  }
107  // Creating a tuple factory, whose tuples will be handled by the tree
108  if(fNtupleActive) {
110  G4int i;
111  G4int n = fNtupleI.size();
112  for(i=0; i<n; ++i) {
113  if(fTupleI[i] == -1) {
115  }
116  }
117  n = fNtupleF.size();
118  for(i=0; i<n; ++i) {
119  if(fTupleF[i] == -1) {
121  }
122  }
123  n = fNtupleD.size();
124  for(i=0; i<n; ++i) {
125  if(fTupleD[i] == -1) {
127  }
128  }
129  }
130 }
131 
132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
133 
134 void Histo::Save()
135 {
136  if(!(fHistoActive || fNtupleActive)) { return; }
137 
138  // Write histogram file
139  if(!fManager->Write()) {
140  G4cout << "Histo::Save: FATAL ERROR writing ROOT file" << G4endl;
141  exit(1);
142  }
143  if(fVerbose > 0) {
144  G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
145  }
146  if(fManager->CloseFile() && fVerbose > 0) {
147  G4cout << " File is closed" << G4endl;
148  }
150  fManager = 0;
151 }
152 
153 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
154 
155 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb,
157 {
158  if(fVerbose > 0) {
159  G4cout << "Histo::Add1D: New histogram will be booked: #"
160  << id << " <" << name
161  << " " << nb << " " << x1 << " " << x2 << " " << u
162  << G4endl;
163  }
164  ++fNHisto;
165  x1 /= u;
166  x2 /= u;
167  fActive.push_back(fDefaultAct);
168  fBins.push_back(nb);
169  fXmin.push_back(x1);
170  fXmax.push_back(x2);
171  fUnit.push_back(u);
172  fIds.push_back(id);
173  fTitles.push_back(name);
174  fHisto.push_back(-1);
175 }
176 
177 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
178 
179 void
181 {
182  if(i>=0 && i<fNHisto) {
183  if(fVerbose > 0) {
184  G4cout << "Histo::SetHisto1D: #" << i
185  << " " << nb << " " << x1 << " " << x2 << " " << u
186  << G4endl;
187  }
188  fBins[i] = nb;
189  fXmin[i] = x1;
190  fXmax[i] = x2;
191  fUnit[i] = u;
192  fActive[i] = true;
193  fHistoActive = true;
194  } else {
195  G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index "
196  << i << G4endl;
197  }
198 }
199 
200 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
201 
202 void Histo::Activate(G4int i, G4bool val)
203 {
204  if(fVerbose > 1) {
205  G4cout << "Histo::Activate: Histogram: #" << i << " "
206  << val << G4endl;
207  }
208  if(i>=0 && i<fNHisto) {
209  fActive[i] = val;
210  if(val) { fHistoActive = true; }
211  }
212 }
213 
214 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
215 
217 {
218  if(!fHistoActive) { return; }
219  if(fVerbose > 1) {
220  G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x
221  << " weight= " << w
222  << G4endl;
223  }
224  if(i>=0 && i<fNHisto) {
225  if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
226  } else {
227  G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
228  }
229 }
230 
231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
232 
233 void Histo::ScaleH1(G4int i, G4double x)
234 {
235  if(!fHistoActive) { return; }
236  if(fVerbose > 0) {
237  G4cout << "Histo::Scale: Histogram: #" << i
238  << " by factor " << x << G4endl;
239  }
240  if(i>=0 && i<fNHisto) {
241  if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
242  } else {
243  G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
244  }
245 }
246 
247 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
248 
249 void Histo::AddTuple(const G4String& w1)
250 {
251  fTupleTitle = w1;
252 }
253 
254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
255 
256 void Histo::AddTupleI(const G4String& w1)
257 {
258  fNtupleActive = true;
259  fNtupleI.push_back(w1);
260  fTupleI.push_back(-1);
261 }
262 
263 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
264 
265 void Histo::AddTupleF(const G4String& w1)
266 {
267  fNtupleActive = true;
268  fNtupleF.push_back(w1);
269  fTupleF.push_back(-1);
270 }
271 
272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
273 
274 void Histo::AddTupleD(const G4String& w1)
275 {
276  fNtupleActive = true;
277  fNtupleD.push_back(w1);
278  fTupleD.push_back(-1);
279 }
280 
281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
282 
283 void Histo::FillTupleI(G4int i, G4int x)
284 {
285  if(!fNtupleActive) { return; }
286  G4int n = fNtupleI.size();
287  if(i >= 0 && i < n) {
288  if(fVerbose > 1) {
289  G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i]
290  << " <" << fNtupleI[i] << "> = " << x << G4endl;
291  }
293  } else {
294  G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index "
295  << i << G4endl;
296  }
297 }
298 
299 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
300 
302 {
303  if(!fNtupleActive) { return; }
304  G4int n = fNtupleF.size();
305  if(i >= 0 && i < n) {
306  if(fVerbose > 1) {
307  G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i]
308  << " <" << fNtupleF[i] << "> = " << x << G4endl;
309  }
311  } else {
312  G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index "
313  << i << G4endl;
314  }
315 }
316 
317 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
318 
320 {
321  if(!fNtupleActive) { return; }
322  G4int n = fNtupleD.size();
323  if(i >= 0 && i < n) {
324  if(fVerbose > 1) {
325  G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i]
326  << " <" << fNtupleD[i] << "> = " << x << G4endl;
327  }
329  } else {
330  G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index "
331  << i << G4endl;
332  }
333 }
334 
335 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
336 
337 void Histo::AddRow()
338 {
339  if(!fNtupleActive) { return; }
341 }
342 
343 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
344 
345 void Histo::SetFileName(const G4String& nam)
346 {
347  fHistName = nam;
348  fHistoActive = true;
349 }
350 
351 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
352 
Float_t x
Definition: compare.C:6
std::vector< G4int > fHisto
const XML_Char * name
Definition: expat.h:151
void SetHisto1D(G4int, G4int, G4double, G4double, G4double)
std::vector< G4String > fTitles
Float_t x1[n_points_granero]
Definition: compare.C:5
#define G4endl
Definition: G4ios.hh:61
float G4float
Definition: G4Types.hh:77
G4RootAnalysisManager * fManager
void Fill(G4int, G4double, G4double)
std::vector< G4double > fXmax
G4bool OpenFile(const G4String &fileName="")
std::vector< G4double > fUnit
G4bool FillNtupleIColumn(G4int id, G4int value)
G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
void FillTupleF(G4int, G4float)
std::vector< G4String > fIds
G4int CreateNtupleIColumn(const G4String &name)
void AddTupleI(const G4String &)
G4bool FillNtupleDColumn(G4int id, G4double value)
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
std::vector< G4String > fNtupleF
void AddTupleF(const G4String &)
void SetFileName(const G4String &)
G4int CreateNtupleDColumn(const G4String &name)
std::vector< G4int > fTupleF
std::vector< G4String > fNtupleI
int G4int
Definition: G4Types.hh:78
void ScaleH1(G4int, G4double)
void FillTupleD(G4int, G4double)
std::vector< G4int > fTupleD
G4bool FillNtupleFColumn(G4int id, G4float value)
static G4RootAnalysisManager * Instance()
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
std::vector< G4String > fNtupleD
G4GLOB_DLL std::ostream G4cout
G4int CreateNtuple(const G4String &name, const G4String &title)
std::vector< G4double > fXmin
Char_t n[5]
void FillTupleI(G4int, G4int)
void Activate(G4int, G4bool)
std::vector< G4int > fBins
Float_t x2[n_points_geant4]
Definition: compare.C:26
void Add1D(const G4String &, const G4String &, G4int nb, G4double x1, G4double x2, G4double u=1.)
G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
void AddTupleD(const G4String &)
G4int CreateNtupleFColumn(const G4String &name)
std::vector< G4int > fTupleI
std::vector< G4bool > fActive
void AddTuple(const G4String &)