Geant4  9.6.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$
31 //
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
34 
35 #include "HistoManager.hh"
36 #include "HistoMessenger.hh"
37 #include "G4UnitsTable.hh"
38 
39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
40 
42 {
43  fileName[0] = "testem12";
44  factoryOn = false;
45  fNbHist = 0;
46 
47  // histograms
48  for (G4int k=0; k<MaxHisto; k++) {
49  fHistId[k] = 0;
50  fHistPt[k] = 0;
51  fExist[k] = false;
52  fUnit[k] = 1.0;
53  fWidth[k] = 1.0;
54  fAscii[k] = false;
55  }
56 
57  fHistoMessenger = new HistoMessenger(this);
58 
59  fRangeFlag = false;
60  fCsdaRange = DBL_MAX;
61 }
62 
63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
64 
66 {
67  delete fHistoMessenger;
68 }
69 
70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
71 
72 void HistoManager::book()
73 {
74  // if no histos, do nothing
75  if (fNbHist == 0) return;
76 
77  // Create or get analysis manager
78  // The choice of analysis technology is done via selection of a namespace
79  // in HistoManager.hh
80  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
81  analysisManager->SetVerboseLevel(1);
82  G4String extension = analysisManager->GetFileType();
83  fileName[1] = fileName[0] + "." + extension;
84 
85  // Open an output file
86  //
87  G4bool fileOpen = analysisManager->OpenFile(fileName[0]);
88  if (!fileOpen) {
89  G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1]
90  << G4endl;
91  return;
92  }
93 
94  // create selected histograms
95  //
96  analysisManager->SetFirstHistoId(1);
97 
98  for (G4int k=0; k<MaxHisto; k++) {
99  if (fExist[k]) {
100  fHistId[k] = analysisManager->CreateH1( fLabel[k], fTitle[k],
101  fNbins[k], fVmin[k], fVmax[k]);
102  fHistPt[k] = analysisManager->GetH1(fHistId[k]);
103  factoryOn = true;
104  }
105  }
106 
107  if (factoryOn)
108  G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl;
109 }
110 
111 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
112 
113 void HistoManager::save()
114 {
115  if (factoryOn) {
116  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
117  analysisManager->Write();
118  analysisManager->CloseFile();
119  saveAscii(); // Write fAscii file, if any
120  G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl;
121 
122  delete G4AnalysisManager::Instance();
123  factoryOn = false;
124  }
125 }
126 
127 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
128 
130 {
131  if (ih > MaxHisto) {
132  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
133  << "does not fExist; e= " << e << " w= " << weight << G4endl;
134  return;
135  }
136 
137  if (fHistPt[ih]) fHistPt[ih]->fill(e/fUnit[ih], weight);
138 }
139 
140 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
141 
143  G4int nbins, G4double vmin, G4double vmax, const G4String& unit)
144 {
145  if (ih > MaxHisto) {
146  G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
147  << "does not fExist" << G4endl;
148  return;
149  }
150 
151  const G4String id[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8" };
152  const G4String title[] =
153  { "dummy", //0
154  "dE/dr (MeV/mm) along radius", //1
155  "total Energy deposited in absorber", //2
156  "true track length of the primary particle", //3
157  "true step size of the primary particle", //4
158  "projected range of the primary particle", //5
159  "true track length of charged secondaries", //6
160  "true step size of charged secondaries", //7
161  "d(E/E0)/d(r/r0) along r/r0" //8
162  };
163 
164  G4String titl = title[ih];
165  fUnit[ih] = 1.;
166 
167  if (unit != "none") {
168  titl = title[ih] + " (" + unit + ")";
169  fUnit[ih] = G4UnitDefinition::GetValueOf(unit);
170  }
171 
172  fExist[ih] = true;
173  fLabel[ih] = id[ih];
174  fTitle[ih] = titl;
175  fNbins[ih] = nbins;
176  fVmin[ih] = vmin;
177  fVmax[ih] = vmax;
178  fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins;
179 
180  fNbHist++;
181 
182  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
183  << nbins << " bins from "
184  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
185 
186 }
187 
188 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
189 
191 {
192  if (ih >= MaxHisto) {
193  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
194  << " fac= " << fac << G4endl;
195  return;
196  }
197 
198  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
199 }
200 
201 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
202 
204 {
205  if (ih < MaxHisto) { fAscii[ih] = true; fAscii[0] = true; }
206  else
207  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
208  << "does not exist" << G4endl;
209 }
210 
211 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
212 
213 #include <fstream>
214 
215 void HistoManager::saveAscii()
216 {
217  if (!fAscii[0]) return;
218 
219  G4String name = fileName[0] + ".ascii";
220  std::ofstream File(name, std::ios::out);
221  if (!File) {
222  G4cout
223  << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
224  return;
225  }
226 
227  File.setf( std::ios::scientific, std::ios::floatfield );
228 
229  //write selected histograms
230  for (G4int ih=0; ih<MaxHisto; ih++) {
231  if (fHistPt[ih] && fAscii[ih]) {
232 
233  File << "\n 1D histogram " << ih << ": " << fTitle[ih]
234  << "\n \n \t X \t\t Y" << G4endl;
235 
236  for (G4int iBin=0; iBin<fNbins[ih]; iBin++) {
237  File << " " << iBin << "\t"
238  << fHistPt[ih]->axis().bin_center(iBin) << "\t"
239  << fHistPt[ih]->bin_height(iBin)
240  << G4endl;
241  }
242  }
243  }
244 }
245 
246 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
247 
249 {
250  // compute constraint on stepMax from histos 1 and 8
251  //
252  G4double stepMax = DBL_MAX;
253  G4double frac = 1.;
254 
255  G4int ih = 1;
256  if (fExist[ih]) {
257  stepMax = frac*fWidth[ih];
258  }
259 
260  ih = 8;
261  if (fExist[ih]) {
262  if (!fRangeFlag) fCsdaRange = range;
263  if (fCsdaRange > 0.) stepMax = std::min(stepMax,frac*fWidth[ih]*fCsdaRange);
264  }
265 
266  G4cout << "\n---> stepMax from HistoManager = "
267  << G4BestUnit(stepMax,"Length") << G4endl;
268 
269  return stepMax;
270 }
271 
272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
273