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