ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistoManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HistoManager.cc
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 //
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 
34 #include "HistoManager.hh"
35 #include "HistoMessenger.hh"
36 #include "G4UnitsTable.hh"
37 #include "G4PhysicalConstants.hh"
38 #include "G4SystemOfUnits.hh"
39 
40 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
41 
43  : fHistoMessenger(0)
44 {
45  fileName[0] = "testem17";
46  factoryOn = false;
47  fNbHist = 0;
48 
49  // histograms
50  for (G4int k=0; k<kMaxHisto; k++) {
51  fHistId[k] = 0;
52  fHistPt[k] = 0;
53  fExist[k] = false;
54  fUnit[k] = 1.0;
55  fWidth[k] = 1.0;
56  fAscii[k] = false;
57  }
58 
59  fHistoMessenger = new HistoMessenger(this);
60 }
61 
62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
63 
65 {
66  delete fHistoMessenger;
67 }
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
71 void HistoManager::Book()
72 {
73  // if no histos, do nothing
74  if (fNbHist == 0) return;
75 
76  // Create or get analysis manager
77  // The choice of analysis technology is done via selection of a namespace
78  // in HistoManager.hh
80  analysisManager->SetVerboseLevel(0);
81  G4String extension = analysisManager->GetFileType();
82  fileName[1] = fileName[0] + "." + extension;
83 
84  // Open an output file
85  //
86  G4bool fileOpen = analysisManager->OpenFile(fileName[0]);
87  if (!fileOpen) {
88  G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1]
89  << G4endl;
90  return;
91  }
92 
93  // create selected histograms
94  //
95  analysisManager->SetFirstHistoId(1);
96 
97  for (G4int k=0; k<kMaxHisto; k++) {
98  if (fExist[k]) {
99  fHistId[k] = analysisManager->CreateH1( fLabel[k], fTitle[k],
100  fNbins[k], fVmin[k], fVmax[k]);
101  fHistPt[k] = analysisManager->GetH1(fHistId[k]);
102  factoryOn = true;
103  }
104  }
105 
106  if (factoryOn)
107  G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl;
108 }
109 
110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
111 
112 void HistoManager::Save()
113 {
114  if (factoryOn) {
115  G4cout << "\n----> HistoManager::save " << G4endl;
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 > kMaxHisto) {
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 > kMaxHisto) {
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", "9",
152  "10", "11", "12", "13", "14"};
153  const G4String title[] = { "dummy", // 0
154  "log10(Eloss/Emu) muIonization", //1
155  "log10(Eloss/Emu) muPair", //2
156  "log10(Eloss/Emu) muBrems", //3
157  "log10(Eloss/Emu) muNuclear", //4
158  "log10(Eloss/Emu) hIonization", //5
159  "log10(Eloss/Emu) hPair", //6
160  "log10(Eloss/Emu) hBrems", //7
161  "dummy", //8
162  "dummy", //9
163  "dummy", //10
164  "log10(Eloss/Emu) muIonization", //11
165  "log10(Eloss/Emu) muPair", //12
166  "log10(Eloss/Emu) muBrems", //13
167  "log10(Eloss/Emu) muNuclear" //14
168  };
169 
170  G4String titl = title[ih];
171  fUnit[ih] = 1.;
172 
173  if (unit != "none") {
174  titl = title[ih] + " (" + unit + ")";
176  }
177 
178  fExist[ih] = true;
179  fLabel[ih] = "h"+id[ih];
180  fTitle[ih] = titl;
181  fNbins[ih] = nbins;
182  fVmin[ih] = vmin;
183  fVmax[ih] = vmax;
184  fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins;
185 
186  fNbHist++;
187 
188  G4cout << "----> SetHisto " << ih << ": " << titl << "; "
189  << nbins << " bins from "
190  << vmin << " " << unit << " to " << vmax << " " << unit << G4endl;
191 
192 }
193 
194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
195 
197 {
198  if (ih >= kMaxHisto) {
199  G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
200  << " fac= " << fac << G4endl;
201  return;
202  }
203 
204  if (fHistPt[ih]) fHistPt[ih]->scale(fac);
205 }
206 
207 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
208 
210 {
211  if (ih < kMaxHisto) { fAscii[ih] = true; fAscii[0] = true; }
212  else
213  G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih
214  << "does not exist" << G4endl;
215 }
216 
217 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
218 
219 #include <fstream>
220 
222 {
223  if (!fAscii[0]) return;
224 
225  G4String name = fileName[0] + ".ascii";
226  std::ofstream File(name, std::ios::out);
227  if (!File) {
228  G4cout
229  << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
230  return;
231  }
232 
233  File.setf( std::ios::scientific, std::ios::floatfield );
234 
235  //write selected histograms
236  for (G4int ih=0; ih<kMaxHisto; ih++) {
237  if (fHistPt[ih] && fAscii[ih]) {
238 
239  File << "\n 1D histogram " << ih << ": " << fTitle[ih]
240  << "\n \n \t X \t\t Y" << G4endl;
241 
242  for (G4int iBin=0; iBin<fNbins[ih]; iBin++) {
243  File << " " << iBin << "\t"
244  << fHistPt[ih]->axis().bin_center(iBin) << "\t"
245  << fHistPt[ih]->bin_height(iBin)
246  << G4endl;
247  }
248  }
249  }
250 }
251 
252 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
253