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] = "fanocavity2";
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->GetFileType();
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","9" ,"10", "11"};
149 
150  const G4String title[] =
151  { "dummy", //0
152  "emission point of e-", //1
153  "energy spectrum of e-", //2
154  "theta distribution of e-", //3
155  "emission point of e- hitting cavity", //4
156  "energy spectrum of e- when entering in cavity", //5
157  "theta distribution of e- before enter in cavity", //6
158  "theta distribution of e- at first step in cavity", //7
159  "track segment of e- in cavity", //8
160  "step size of e- in wall", //9
161  "step size of e- in cavity", //10
162  "energy deposit in cavity per track" //11
163  };
164 
165  G4String titl = title[ih];
166  fUnit[ih] = 1.;
167 
168  if (unit != "none") {
169  titl = title[ih] + " (" + unit + ")";
170  fUnit[ih] = G4UnitDefinition::GetValueOf(unit);
171  }
172 
173  fExist[ih] = true;
174  fLabel[ih] = id[ih];
175  fTitle[ih] = titl;
176  fNbins[ih] = nbins;
177  fVmin[ih] = vmin;
178  fVmax[ih] = vmax;
179  fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins;
180 
181  fNbHist++;
182 
183  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
184  << nbins << " bins from "
185  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
186 
187 }
188 
189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
190 
192 {
193  if (ih >= MaxHisto) {
194  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
195  << " fac= " << fac << G4endl;
196  return;
197  }
198 
199  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
200 }
201 
202 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
203 
205 {
206  if (ih < MaxHisto) { fAscii[ih] = true; fAscii[0] = true; }
207  else
208  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
209  << "does not exist" << G4endl;
210 }
211 
212 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
213 
214 #include <fstream>
215 
216 void HistoManager::saveAscii()
217 {
218  if (!fAscii[0]) return;
219 
220  G4String name = fileName[0] + ".ascii";
221  std::ofstream File(name, std::ios::out);
222  if (!File) {
223  G4cout
224  << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
225  return;
226  }
227 
228  File.setf( std::ios::scientific, std::ios::floatfield );
229 
230  //write selected histograms
231  for (G4int ih=0; ih<MaxHisto; ih++) {
232  if (fHistPt[ih] && fAscii[ih]) {
233 
234  File << "\n 1D histogram " << ih << ": " << fTitle[ih]
235  << "\n \n \t X \t\t Y" << G4endl;
236 
237  for (G4int iBin=0; iBin<fNbins[ih]; iBin++) {
238  File << " " << iBin << "\t"
239  << fHistPt[ih]->axis().bin_center(iBin) << "\t"
240  << fHistPt[ih]->bin_height(iBin)
241  << G4endl;
242  }
243  }
244  }
245 }
246 
247 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
248