Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
extended/electromagnetic/TestEm17/src/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 // $Id: HistoManager.cc 100677 2016-10-31 10:46:50Z 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<kMaxHisto; 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<kMaxHisto; 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 > kMaxHisto) {
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 > kMaxHisto) {
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 + ")";
178  }
179 
180  fExist[ih] = true;
181  fLabel[ih] = "h"+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 >= kMaxHisto) {
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 < kMaxHisto) { 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 
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<kMaxHisto; 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 
const XML_Char * name
Definition: expat.h:151
#define G4endl
Definition: G4ios.hh:61
void Normalize(G4int id, G4double fac)
void FillHisto(G4int id, G4double e, G4double weight=1.0)
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
static G4double GetValueOf(const G4String &)
G4CsvAnalysisManager G4AnalysisManager
Definition: g4csv_defs.hh:77
void SetHisto(G4int, G4int, G4double, G4double, const G4String &unit="none")
double weight
Definition: plottest35.C:25
int G4int
Definition: G4Types.hh:78
static const G4double fac
G4GLOB_DLL std::ostream G4cout