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 | #ifdef G4ANALYSIS_USE |
---|
27 | #include <AIDA/AIDA.h> |
---|
28 | #endif |
---|
29 | // |
---|
30 | #ifdef G4ANALYSIS_USE_ROOT |
---|
31 | #include "TROOT.h" |
---|
32 | #include "TApplication.h" |
---|
33 | #include "TGClient.h" |
---|
34 | #include "TCanvas.h" |
---|
35 | #include "TSystem.h" |
---|
36 | #include "TTree.h" |
---|
37 | #include "TBranch.h" |
---|
38 | #include "TFile.h" |
---|
39 | #include "TH1D.h" |
---|
40 | #include "TNtuple.h" |
---|
41 | #endif |
---|
42 | |
---|
43 | #include "exrdmHisto.hh" |
---|
44 | #include "exrdmHistoMessenger.hh" |
---|
45 | #include "G4ParticleTable.hh" |
---|
46 | |
---|
47 | #include "G4Tokenizer.hh" |
---|
48 | |
---|
49 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
50 | exrdmHisto::exrdmHisto() |
---|
51 | { |
---|
52 | verbose = 1; |
---|
53 | histName = "exrdm"; |
---|
54 | histType = "aida"; |
---|
55 | //histType = "root"; |
---|
56 | nHisto = 0; |
---|
57 | nTuple = 0; |
---|
58 | defaultAct = 1; |
---|
59 | // |
---|
60 | #ifdef G4ANALYSIS_USE |
---|
61 | aida = 0; |
---|
62 | tree = 0; |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef G4ANALYSIS_USE_ROOT |
---|
66 | ROOThisto.clear(); |
---|
67 | ROOTntup.clear(); |
---|
68 | Rarray.clear(); |
---|
69 | Rcol.clear(); |
---|
70 | #endif |
---|
71 | |
---|
72 | active.clear(); |
---|
73 | bins.clear(); |
---|
74 | xmin.clear(); |
---|
75 | xmax.clear(); |
---|
76 | unit.clear(); |
---|
77 | ids.clear(); |
---|
78 | titles.clear(); |
---|
79 | tupleName.clear(); |
---|
80 | tupleId.clear(); |
---|
81 | tupleList.clear(); |
---|
82 | tupleListROOT.clear(); |
---|
83 | |
---|
84 | messenger = new exrdmHistoMessenger(this); |
---|
85 | } |
---|
86 | |
---|
87 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
88 | |
---|
89 | exrdmHisto::~exrdmHisto() |
---|
90 | { |
---|
91 | #ifdef G4ANALYSIS_USE |
---|
92 | histo.clear(); |
---|
93 | ntup.clear(); |
---|
94 | #endif |
---|
95 | #ifdef G4ANALYSIS_USE_ROOT |
---|
96 | //FIXME : G.Barrand : the below is crashy. |
---|
97 | // In principle the TH are deleted |
---|
98 | // when doing the TFile::Close ! |
---|
99 | // In fact the hfileROOT should |
---|
100 | // be deleted in save(). And I am pretty |
---|
101 | // sure that the TApplication is not needed. |
---|
102 | // |
---|
103 | // removed by F.Lei |
---|
104 | // for(G4int i=0; i<nHisto; i++) { |
---|
105 | // if(ROOThisto[i]) delete ROOThisto[i]; |
---|
106 | // } |
---|
107 | #endif |
---|
108 | delete messenger; |
---|
109 | } |
---|
110 | |
---|
111 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
112 | |
---|
113 | void exrdmHisto::book() |
---|
114 | { |
---|
115 | #ifdef G4ANALYSIS_USE |
---|
116 | G4cout << "### exrdmHisto books " << nHisto << " histograms " << G4endl; |
---|
117 | // Creating the analysis factory |
---|
118 | aida = AIDA_createAnalysisFactory(); |
---|
119 | if(!aida) { |
---|
120 | G4cout << "ERROR: can't get AIDA." << G4endl; |
---|
121 | return; |
---|
122 | } |
---|
123 | // Creating the tree factory |
---|
124 | {AIDA::ITreeFactory* tf = aida->createTreeFactory(); |
---|
125 | // Creating a tree mapped to a new aida file. |
---|
126 | G4String fileName = histName + "." + histType; |
---|
127 | if (histType == "root") fileName = histName + "_aida." + histType; |
---|
128 | tree = tf->create(fileName,histType,false,true,"compress=yes"); |
---|
129 | delete tf; |
---|
130 | if(!tree) { |
---|
131 | G4cout << "ERROR: Tree store " << histName << " is not created!" << G4endl; |
---|
132 | return; |
---|
133 | } |
---|
134 | G4cout << "Tree store : " << tree->storeName() << G4endl;} |
---|
135 | // Creating a histogram factory, whose histograms will be handled by the tree |
---|
136 | {AIDA::IHistogramFactory* hf = aida->createHistogramFactory(*tree); |
---|
137 | // Creating an 1-dimensional histograms in the root directory of the tree |
---|
138 | for(G4int i=0; i<nHisto; i++) { |
---|
139 | if(active[i]) { |
---|
140 | if(verbose>1) |
---|
141 | G4cout<<"book: histogram "<< i << " id= " << ids[i] <<G4endl; |
---|
142 | G4String tit = ids[i]; |
---|
143 | if(histType == "root") tit = "h" + ids[i]; |
---|
144 | histo[i] = hf->createHistogram1D(tit, titles[i], bins[i], xmin[i], xmax[i]); |
---|
145 | } |
---|
146 | } |
---|
147 | delete hf; |
---|
148 | G4cout << "AIDA histograms are booked" << G4endl;} |
---|
149 | |
---|
150 | // Creating a tuple factory, whose tuples will be handled by the tree |
---|
151 | {AIDA::ITupleFactory* tpf = aida->createTupleFactory( *tree ); |
---|
152 | G4cout << "AIDA will book " << nTuple << " ntuples" << G4endl; |
---|
153 | for(G4int i=0; i<nTuple; i++) { |
---|
154 | if(tupleList[i] != "") { |
---|
155 | G4cout << "Creating Ntuple: " << tupleName[i] <<":" <<tupleList[i] <<G4endl; |
---|
156 | ntup[i] = tpf->create(tupleId[i], tupleName[i], tupleList[i],""); |
---|
157 | } |
---|
158 | } |
---|
159 | delete tpf; |
---|
160 | G4cout << "AIDA ntuples are booked" << G4endl;} |
---|
161 | #endif |
---|
162 | |
---|
163 | #ifdef G4ANALYSIS_USE_ROOT |
---|
164 | // new TApplication("App", ((int *)0), ((char **)0)); |
---|
165 | G4String fileNameROOT = histName + G4String(".root"); |
---|
166 | hfileROOT = new TFile(fileNameROOT.c_str() ,"RECREATE","ROOT file for exRDM"); |
---|
167 | G4cout << "Root file: " << fileNameROOT << G4endl; |
---|
168 | // Creating an 1-dimensional histograms in the root directory of the tree |
---|
169 | for(G4int i=0; i<nHisto; i++) { |
---|
170 | if(active[i]) { |
---|
171 | G4String id = G4String("h")+ids[i]; |
---|
172 | ROOThisto[i] = new TH1D(id, titles[i], bins[i], xmin[i], xmax[i]); |
---|
173 | G4cout << "ROOT Histo " << ids[i] << " " << titles[i] << " booked " << G4endl; |
---|
174 | } |
---|
175 | } |
---|
176 | // Now the ntuples |
---|
177 | for(G4int i=0; i<nTuple; i++) { |
---|
178 | if(tupleListROOT[i] != "") { |
---|
179 | G4String id = G4String("t")+tupleId[i]; |
---|
180 | G4cout << "Creating Ntuple "<<tupleId[i] << " in ROOT file: " |
---|
181 | << tupleName[i] << G4endl; |
---|
182 | ROOTntup[i] = new TNtuple(id, tupleName[i], tupleListROOT[i]); |
---|
183 | G4cout << "ROOT Ntuple " << id << " " << tupleName[i] <<" "<< tupleListROOT[i]<< " booked " << G4endl; |
---|
184 | } |
---|
185 | } |
---|
186 | #endif |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
191 | |
---|
192 | void exrdmHisto::save() |
---|
193 | { |
---|
194 | #ifdef G4ANALYSIS_USE |
---|
195 | // Write histogram file |
---|
196 | tree->commit(); |
---|
197 | G4cout << "Closing the AIDA tree..." << G4endl; |
---|
198 | tree->close(); |
---|
199 | G4cout << "Histograms and Ntuples are saved" << G4endl; |
---|
200 | delete tree; |
---|
201 | tree = 0; |
---|
202 | delete aida; |
---|
203 | aida = 0; |
---|
204 | {for(G4int i=0; i<nHisto; i++) histo[i] = 0;} |
---|
205 | {for(G4int i=0; i<nTuple; i++) ntup[i] = 0;} |
---|
206 | #endif |
---|
207 | #ifdef G4ANALYSIS_USE_ROOT |
---|
208 | G4cout << "ROOT: files writing..." << G4endl; |
---|
209 | hfileROOT->Write(); |
---|
210 | G4cout << "ROOT: files closing..." << G4endl; |
---|
211 | hfileROOT->Close(); |
---|
212 | // |
---|
213 | // F.Lei added following Guy's suggestion! |
---|
214 | delete hfileROOT; |
---|
215 | |
---|
216 | #endif |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
221 | |
---|
222 | void exrdmHisto::add1D(const G4String& id, const G4String& name, G4int nb, |
---|
223 | G4double x1, G4double x2, G4double u) |
---|
224 | { |
---|
225 | if(verbose > 0) { |
---|
226 | G4cout << "New histogram will be booked: #" << id << " <" << name |
---|
227 | << " " << nb << " " << x1 << " " << x2 << " " << u |
---|
228 | << G4endl; |
---|
229 | } |
---|
230 | nHisto++; |
---|
231 | x1 /= u; |
---|
232 | x2 /= u; |
---|
233 | active.push_back(defaultAct); |
---|
234 | bins.push_back(nb); |
---|
235 | xmin.push_back(x1); |
---|
236 | xmax.push_back(x2); |
---|
237 | unit.push_back(u); |
---|
238 | ids.push_back(id); |
---|
239 | titles.push_back(name); |
---|
240 | #ifdef G4ANALYSIS_USE |
---|
241 | histo.push_back(0); |
---|
242 | #endif |
---|
243 | #ifdef G4ANALYSIS_USE_ROOT |
---|
244 | ROOThisto.push_back(0); |
---|
245 | #endif |
---|
246 | } |
---|
247 | |
---|
248 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
249 | |
---|
250 | void exrdmHisto::setHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u) |
---|
251 | { |
---|
252 | if(i>=0 && i<nHisto) { |
---|
253 | if(verbose > 0) { |
---|
254 | G4cout << "Update histogram: #" << i |
---|
255 | << " " << nb << " " << x1 << " " << x2 << " " << u |
---|
256 | << G4endl; |
---|
257 | } |
---|
258 | bins[i] = nb; |
---|
259 | xmin[i] = x1; |
---|
260 | xmax[i] = x2; |
---|
261 | unit[i] = u; |
---|
262 | } else { |
---|
263 | G4cout << "exrdmHisto::setexrdmHisto1D: WARNING! wrong histogram index " << i << G4endl; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
268 | |
---|
269 | void exrdmHisto::fillHisto(G4int i, G4double x, G4double w) |
---|
270 | { |
---|
271 | if(verbose > 1) { |
---|
272 | G4cout << "fill histogram: #" << i << " at x= " << x |
---|
273 | << " weight= " << w |
---|
274 | << G4endl; |
---|
275 | } |
---|
276 | #ifdef G4ANALYSIS_USE |
---|
277 | if(i>=0 && i<nHisto) { |
---|
278 | histo[i]->fill(x/unit[i], w); |
---|
279 | } else { |
---|
280 | G4cout << "exrdmHisto::fill: WARNING! wrong AIDA histogram index " << i << G4endl; |
---|
281 | } |
---|
282 | #endif |
---|
283 | #ifdef G4ANALYSIS_USE_ROOT |
---|
284 | if(i>=0 && i<nHisto) { |
---|
285 | ROOThisto[i]->Fill(x/unit[i],w); |
---|
286 | } else { |
---|
287 | G4cout << "exrdmHisto::fill: WARNING! wrong ROOT histogram index " << i << G4endl; |
---|
288 | } |
---|
289 | #endif |
---|
290 | } |
---|
291 | |
---|
292 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
293 | |
---|
294 | void exrdmHisto::scaleHisto(G4int i, G4double x) |
---|
295 | { |
---|
296 | if(verbose > 0) { |
---|
297 | G4cout << "Scale histogram: #" << i << " by factor " << x << G4endl; |
---|
298 | } |
---|
299 | #ifdef G4ANALYSIS_USE |
---|
300 | if(i>=0 && i<nHisto) { |
---|
301 | histo[i]->scale(x); |
---|
302 | G4cout << "exrdmHisto::scale: WARNING! wrong AIDA histogram index " << i << G4endl; |
---|
303 | } |
---|
304 | #endif |
---|
305 | #ifdef G4ANALYSIS_USE_ROOT |
---|
306 | if(i>=0 && i<nHisto) { |
---|
307 | ROOThisto[i]->Scale(x); |
---|
308 | } else { |
---|
309 | G4cout << "exrdmHisto::scale: WARNING! wrong ROOT histogram index " << i << G4endl; |
---|
310 | } |
---|
311 | #endif |
---|
312 | } |
---|
313 | |
---|
314 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
315 | |
---|
316 | #ifdef G4ANALYSIS_USE |
---|
317 | void exrdmHisto::addTuple(const G4String& w1, const G4String& w2, const G4String& w3 ) |
---|
318 | #else |
---|
319 | void exrdmHisto::addTuple(const G4String& w1, const G4String& w2, const G4String& ) |
---|
320 | #endif |
---|
321 | |
---|
322 | { |
---|
323 | //G4cout << w1 << " " << w2 << " " << w3 << G4endl; |
---|
324 | nTuple++; |
---|
325 | tupleId.push_back(w1); |
---|
326 | tupleName.push_back(w2) ; |
---|
327 | #ifdef G4ANALYSIS_USE |
---|
328 | tupleList.push_back(w3); |
---|
329 | ntup.push_back(0); |
---|
330 | #endif |
---|
331 | |
---|
332 | #ifdef G4ANALYSIS_USE_ROOT |
---|
333 | std::vector<float> ar; |
---|
334 | ar.clear(); |
---|
335 | for (size_t i = 0; i < 20; i++) ar.push_back(0.); |
---|
336 | Rarray.push_back(ar); |
---|
337 | // convert AIDA header to ROOT header for ntuple |
---|
338 | G4Tokenizer next(w3); |
---|
339 | G4String token = next(); |
---|
340 | G4String ROOTList1 = "" ; |
---|
341 | G4int col = 0; |
---|
342 | while ( token != "") { |
---|
343 | token = next(); |
---|
344 | if (token == ",") token = next(); |
---|
345 | if (token.contains(",")) token.remove(token.size()-1); |
---|
346 | ROOTList1 = ROOTList1 + token + G4String(":"); |
---|
347 | col++; |
---|
348 | } |
---|
349 | G4String ROOTList = ROOTList1.substr(0,ROOTList1.length()-2); |
---|
350 | // G4cout << ROOTList << G4endl; |
---|
351 | tupleListROOT.push_back(ROOTList); |
---|
352 | ROOTntup.push_back(0); |
---|
353 | Rcol.push_back(col-1); |
---|
354 | #endif |
---|
355 | } |
---|
356 | |
---|
357 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
358 | |
---|
359 | void exrdmHisto::fillTuple(G4int i, const G4String& parname, G4double x) |
---|
360 | { |
---|
361 | if(verbose > 1) |
---|
362 | G4cout << "fill tuple # " << i |
---|
363 | <<" with parameter <" << parname << "> = " << x << G4endl; |
---|
364 | #ifdef G4ANALYSIS_USE |
---|
365 | if(ntup[i]) ntup[i]->fill(ntup[i]->findColumn(parname), x); |
---|
366 | #endif |
---|
367 | } |
---|
368 | |
---|
369 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
370 | |
---|
371 | void exrdmHisto::fillTuple(G4int i, G4int col, G4double x) |
---|
372 | { |
---|
373 | if(verbose > 1) { |
---|
374 | G4cout << "fill tuple # " << i |
---|
375 | <<" in column < " << col << "> = " << x << G4endl; |
---|
376 | } |
---|
377 | #ifdef G4ANALYSIS_USE |
---|
378 | if(ntup[i]) ntup[i]->fill(col,double(x)); |
---|
379 | #endif |
---|
380 | |
---|
381 | #ifdef G4ANALYSIS_USE_ROOT |
---|
382 | if(ROOTntup[i]) (Rarray[i])[col] = float(x); |
---|
383 | #endif |
---|
384 | |
---|
385 | } |
---|
386 | |
---|
387 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
388 | |
---|
389 | void exrdmHisto::fillTuple(G4int i, const G4String& parname, G4String& x) |
---|
390 | { |
---|
391 | if(verbose > 1) { |
---|
392 | G4cout << "fill tuple # " << i |
---|
393 | <<" with parameter <" << parname << "> = " << x << G4endl; |
---|
394 | } |
---|
395 | #ifdef G4ANALYSIS_USE |
---|
396 | if(ntup[i]) ntup[i]->fill(ntup[i]->findColumn(parname), x); |
---|
397 | #endif |
---|
398 | |
---|
399 | } |
---|
400 | |
---|
401 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
402 | |
---|
403 | void exrdmHisto::addRow(G4int i) |
---|
404 | { |
---|
405 | if(verbose > 1) G4cout << "Added a raw #" << i << " to tuple" << G4endl; |
---|
406 | #ifdef G4ANALYSIS_USE |
---|
407 | if(ntup[i]) ntup[i]->addRow(); |
---|
408 | #endif |
---|
409 | |
---|
410 | #ifdef G4ANALYSIS_USE_ROOT |
---|
411 | float ar[4]; |
---|
412 | for (G4int j=0; j < Rcol[i]; j++) { |
---|
413 | // G4cout << i << " " << Rarray[i][j] << G4endl; |
---|
414 | ar[j] = Rarray[i][j]; |
---|
415 | } |
---|
416 | if(ROOTntup[i]) ROOTntup[i]->Fill(ar); |
---|
417 | #endif |
---|
418 | |
---|
419 | } |
---|
420 | |
---|
421 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
422 | |
---|
423 | void exrdmHisto::setFileName(const G4String& nam) |
---|
424 | { |
---|
425 | histName = nam; |
---|
426 | } |
---|
427 | |
---|
428 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
429 | |
---|
430 | void exrdmHisto::setFileType(const G4String& nam) |
---|
431 | { |
---|
432 | histType = nam; |
---|
433 | } |
---|
434 | |
---|
435 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
436 | |
---|
437 | const G4String& exrdmHisto::FileType() const |
---|
438 | { |
---|
439 | return histType; |
---|
440 | } |
---|
441 | |
---|
442 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
443 | |
---|