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] = "particleGun";
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 
60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
61 
63 {
64  delete fHistoMessenger;
65 }
66 
67 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
68 
69 void HistoManager::book()
70 {
71  // if no histos, do nothing
72  if (fNbHist == 0) return;
73 
74  // Create or get analysis manager
75  // The choice of analysis technology is done via selection of a namespace
76  // in HistoManager.hh
77  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
78  analysisManager->SetVerboseLevel(1);
79  G4String extension = analysisManager->GetType();
80  fileName[1] = fileName[0] + "." + extension;
81 
82  // Open an output file
83  //
84  G4bool fileOpen = analysisManager->OpenFile(fileName[0]);
85  if (!fileOpen) {
86  G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1]
87  << G4endl;
88  return;
89  }
90 
91  // create selected histograms
92  //
93  analysisManager->SetFirstHistoId(1);
94 
95  for (G4int k=0; k<MaxHisto; k++) {
96  if (fExist[k]) {
97  fHistId[k] = analysisManager->CreateH1( fLabel[k], fTitle[k],
98  fNbins[k], fVmin[k], fVmax[k]);
99  fHistPt[k] = analysisManager->GetH1(fHistId[k]);
100  factoryOn = true;
101  }
102  }
103 
104  if (factoryOn)
105  G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl;
106 }
107 
108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
109 
110 void HistoManager::save()
111 {
112  if (factoryOn) {
113  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
114  analysisManager->Write();
115  analysisManager->CloseFile();
116  saveAscii(); // Write fAscii file, if any
117  G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl;
118 
119  delete G4AnalysisManager::Instance();
120  factoryOn = false;
121  }
122 }
123 
124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
125 
127 {
128  if (ih > MaxHisto) {
129  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
130  << "does not fExist; e= " << e << " w= " << weight << G4endl;
131  return;
132  }
133 
134  if (fHistPt[ih]) fHistPt[ih]->fill(e/fUnit[ih], weight);
135 }
136 
137 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
138 
140  G4int nbins, G4double vmin, G4double vmax, const G4String& unit)
141 {
142  if (ih > MaxHisto) {
143  G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
144  << "does not fExist" << G4endl;
145  return;
146  }
147 
148  const G4String id[] = { "0", "1", "2", "3", "4", "5", "6", "7" , "8" };
149  const G4String title[] =
150  { "dummy", //0
151  "energy spectrum dN/dE = f(E)", //1 for GunAction#2
152  "moment dir: angular distr dN/dOmega = f(alpha) ", //2 for GunAction#2
153  "moment dir: angular distr dN/dOmega = f(psi) ", //3 for GunAction#2
154  "vertex posi: radial distr dN/dv = f(r)", //4 for GunAction#3
155  "vertex posi: angular distr dN/dv = f(theta)", //5 for GunAction#3
156  "vertex posi: angular distr dN/dv = f(phi)", //6 for GunAction#3
157  "moment dir: angular distr dN/dOmega = f(alpha) ", //7 for GunAction#3
158  "moment dir: angular distr dN/dOmega = f(psi) " //8 for GunAction#3
159  };
160 
161  G4String titl = title[ih];
162  fUnit[ih] = 1.;
163 
164  if (unit != "none") {
165  titl = title[ih] + " (" + unit + ")";
166  fUnit[ih] = G4UnitDefinition::GetValueOf(unit);
167  }
168 
169  fExist[ih] = true;
170  fLabel[ih] = id[ih];
171  fTitle[ih] = titl;
172  fNbins[ih] = nbins;
173  fVmin[ih] = vmin;
174  fVmax[ih] = vmax;
175  fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins;
176 
177  fNbHist++;
178 
179  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
180  << nbins << " bins from "
181  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
182 
183 }
184 
185 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
186 
188 {
189  if (ih >= MaxHisto) {
190  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
191  << " fac= " << fac << G4endl;
192  return;
193  }
194 
195  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
196 }
197 
198 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
199 
201 {
202  if (ih < MaxHisto) { fAscii[ih] = true; fAscii[0] = true; }
203  else
204  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
205  << "does not exist" << G4endl;
206 }
207 
208 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
209 
210 #include <fstream>
211 
212 void HistoManager::saveAscii()
213 {
214  if (!fAscii[0]) return;
215 
216  G4String name = fileName[0] + ".ascii";
217  std::ofstream File(name, std::ios::out);
218  if (!File) {
219  G4cout
220  << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
221  return;
222  }
223 
224  File.setf( std::ios::scientific, std::ios::floatfield );
225 
226  //write selected histograms
227  for (G4int ih=0; ih<MaxHisto; ih++) {
228  if (fHistPt[ih] && fAscii[ih]) {
229 
230  File << "\n 1D histogram " << ih << ": " << fTitle[ih]
231  << "\n \n \t X \t\t Y" << G4endl;
232 
233  for (G4int iBin=0; iBin<fNbins[ih]; iBin++) {
234  File << " " << iBin << "\t"
235  << fHistPt[ih]->axis().bin_center(iBin) << "\t"
236  << fHistPt[ih]->bin_height(iBin)
237  << G4endl;
238  }
239  }
240  }
241 }
242 
243 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
244