Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
extended/electromagnetic/TestEm17/src/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 //
26 /// \file electromagnetic/TestEm17/src/HistoManager.cc
27 /// \brief Implementation of the HistoManager class
28 //
29 //
30 // $Id: HistoManager.cc 69011 2013-04-15 09:35:47Z gcosmo $
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 #include "G4PhysicalConstants.hh"
39 #include "G4SystemOfUnits.hh"
40 
41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
42 
44  : fHistoMessenger(0)
45 {
46  fileName[0] = "testem17";
47  factoryOn = false;
48  fNbHist = 0;
49 
50  // histograms
51  for (G4int k=0; k<MaxHisto; k++) {
52  fHistId[k] = 0;
53  fHistPt[k] = 0;
54  fExist[k] = false;
55  fUnit[k] = 1.0;
56  fWidth[k] = 1.0;
57  fAscii[k] = false;
58  }
59 
60  fHistoMessenger = new HistoMessenger(this);
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(0);
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  G4cout << "\n----> HistoManager::save " << G4endl;
117  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
118  analysisManager->Write();
119  analysisManager->CloseFile();
120  saveAscii(); // Write fAscii file, if any
121  G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl;
122 
123  // delete G4AnalysisManager::Instance();
124  factoryOn = false;
125  }
126 }
127 
128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
129 
131 {
132  if (ih > MaxHisto) {
133  G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
134  << "does not fExist; e= " << e << " w= " << weight << G4endl;
135  return;
136  }
137 
138  if (fHistPt[ih]) fHistPt[ih]->fill(e/fUnit[ih], weight);
139 }
140 
141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
142 
144  G4int nbins, G4double vmin, G4double vmax, const G4String& unit)
145 {
146  if (ih > MaxHisto) {
147  G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih
148  << "does not fExist" << G4endl;
149  return;
150  }
151 
152  const G4String id[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
153  "10", "11", "12", "13", "14", "15"};
154  const G4String title[] =
155  { "dummy", //0
156  "log10(Etransfert/Emu) muIonization", //1
157  "log10(Etransfert/Emu) muPair", //2
158  "log10(Etransfert/Emu) muBrems", //3
159  "log10(Etransfert/Emu) muNuclear", //4
160  "log10(Etransfert/Emu) hIonization", //5
161  "log10(Etransfert/Emu) hPair", //6
162  "log10(Etransfert/Emu) hBrems", //7
163  "dummy", //8
164  "dummy", //9
165  "dummy", //10
166  "log10(Etransfert/Emu) muIonization", //11
167  "log10(Etransfert/Emu) muPair", //12
168  "log10(Etransfert/Emu) muBrems", //13
169  "log10(Etransfert/Emu) muNuclear" //14
170  };
171 
172  G4String titl = title[ih];
173  fUnit[ih] = 1.;
174 
175  if (unit != "none") {
176  titl = title[ih] + " (" + unit + ")";
177  fUnit[ih] = G4UnitDefinition::GetValueOf(unit);
178  }
179 
180  fExist[ih] = true;
181  fLabel[ih] = id[ih];
182  fTitle[ih] = titl;
183  fNbins[ih] = nbins;
184  fVmin[ih] = vmin;
185  fVmax[ih] = vmax;
186  fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins;
187 
188  fNbHist++;
189 
190  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
191  << nbins << " bins from "
192  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
193 
194 }
195 
196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
197 
199 {
200  if (ih >= MaxHisto) {
201  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
202  << " fac= " << fac << G4endl;
203  return;
204  }
205 
206  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
207 }
208 
209 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
210 
212 {
213  if (ih < MaxHisto) { fAscii[ih] = true; fAscii[0] = true; }
214  else
215  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
216  << "does not exist" << G4endl;
217 }
218 
219 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
220 
221 #include <fstream>
222 
223 void HistoManager::saveAscii()
224 {
225  if (!fAscii[0]) return;
226 
227  G4String name = fileName[0] + ".ascii";
228  std::ofstream File(name, std::ios::out);
229  if (!File) {
230  G4cout
231  << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
232  return;
233  }
234 
235  File.setf( std::ios::scientific, std::ios::floatfield );
236 
237  //write selected histograms
238  for (G4int ih=0; ih<MaxHisto; ih++) {
239  if (fHistPt[ih] && fAscii[ih]) {
240 
241  File << "\n 1D histogram " << ih << ": " << fTitle[ih]
242  << "\n \n \t X \t\t Y" << G4endl;
243 
244  for (G4int iBin=0; iBin<fNbins[ih]; iBin++) {
245  File << " " << iBin << "\t"
246  << fHistPt[ih]->axis().bin_center(iBin) << "\t"
247  << fHistPt[ih]->bin_height(iBin)
248  << G4endl;
249  }
250  }
251  }
252 }
253 
254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
255 
void FillHisto(G4int id, G4double bin, G4double weight=1.0)
const XML_Char * name
int G4int
Definition: G4Types.hh:78
void Normalize(G4int id, G4double fac)
static G4double GetValueOf(const G4String &)
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:46
void SetHisto(G4int, G4int, G4double, G4double, const G4String &unit="none")
subroutine title(NA, NB, NCA, NCB)
Definition: dpm25nuc7.f:1744
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76