source: trunk/examples/extended/electromagnetic/TestEm9/src/Histo.cc @ 1342

Last change on this file since 1342 was 1342, checked in by garnier, 14 years ago

update ti head

File size: 7.8 KB
Line 
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//---------------------------------------------------------------------------
27//
28// ClassName:   Histo - Generic histogram/ntuple manager class
29//
30//
31// Author:      V.Ivanchenko 30.10.03
32//
33//----------------------------------------------------------------------------
34//
35
36//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
37//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
38
39#include "Histo.hh"
40
41#ifdef G4ANALYSIS_USE
42
43#include "AIDA/AIDA.h"
44#include "HistoMessenger.hh"
45
46#endif
47
48//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
49//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
50
51Histo::Histo()
52{
53  verbose    = 0;
54  histName   = "testem9";
55  histType   = "root";
56  nHisto     = 0;
57  defaultAct = 1;
58  tupleName  = "tuple9";
59  tupleId    = "100";
60  tupleList  = "";
61  ntup = 0;
62  messenger  = 0;
63
64#ifdef G4ANALYSIS_USE
65  tree = 0;
66  af   = 0; 
67  messenger = new HistoMessenger(this);
68#endif
69}
70
71//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
72
73Histo::~Histo()
74{
75#ifdef G4ANALYSIS_USE
76  delete messenger;
77  delete af;
78#endif
79}
80
81//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
82
83void Histo::book()
84{
85#ifdef G4ANALYSIS_USE
86  G4cout << "### Histo books " << nHisto << " histograms " << G4endl;
87  // Creating the analysis factory
88  if(!af) af = AIDA_createAnalysisFactory();
89  // Creating the tree factory
90  AIDA::ITreeFactory* tf = af->createTreeFactory();
91
92  // Creating a tree mapped to a new hbook file.
93
94  G4String nam = histName + "." + histType;
95  G4String options  = "";
96  //G4String options  = "--noErrors export=root uncompress";
97 
98  tree = tf->create(nam,histType,false,true,options);
99  delete tf;
100  if(tree) {
101    G4cout << "Tree store  : " << tree->storeName() << G4endl;
102  } else {
103    G4cout << "ERROR: Tree store " << histName  << " is not created!" << G4endl;
104    return;
105  }
106  // Creating a histogram factory, whose histograms will be handled by the tree
107  AIDA::IHistogramFactory* hf = af->createHistogramFactory( *tree );
108
109  // Creating an 1-dimensional histograms in the root directory of the tree
110  for(G4int i=0; i<nHisto; i++) {
111    if(active[i]) {
112      G4String ss = ids[i];
113      if(histType == "root") ss = "h" + ids[i];
114      histo[i] = hf->createHistogram1D(ss, titles[i], bins[i], xmin[i], xmax[i]);
115    }
116  }
117  delete hf;
118  // Creating a tuple factory, whose tuples will be handled by the tree
119  if(tupleList != "") {
120     AIDA::ITupleFactory* tpf = af->createTupleFactory( *tree );
121     ntup = tpf->create(tupleId, tupleName, tupleList);
122     delete tpf;
123  }
124#endif
125} 
126
127//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
128
129void Histo::save()
130{
131#ifdef G4ANALYSIS_USE
132  // Write histogram file
133  if(tree) {
134    tree->commit();
135    G4cout << "Closing the tree..." << G4endl;
136    tree->close();
137    G4cout << "Histograms and Ntuples are saved" << G4endl;
138    delete tree;
139    tree = 0;
140  }
141#endif
142} 
143
144//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
145
146void Histo::add1D(const G4String& id, const G4String& name, G4int nb, 
147                  G4double x1, G4double x2, G4double u)
148{
149  if(verbose > 0) {
150    G4cout << "New histogram will be booked: #" << id << "  <" << name
151           << "  " << nb << "  " << x1 << "  " << x2 << "  " << u
152           << G4endl;
153  }
154  nHisto++;
155  x1 /= u;
156  x2 /= u;
157  active.push_back(defaultAct);
158  bins.push_back(nb);
159  xmin.push_back(x1);
160  xmax.push_back(x2);
161  unit.push_back(u);
162  ids.push_back(id);
163  titles.push_back(name);
164  histo.push_back(0);
165}
166
167//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
168
169void Histo::setHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u)
170{
171  if(i>=0 && i<nHisto) {
172    if(verbose > 0) {
173      G4cout << "Update histogram: #" << i 
174             << "  " << nb << "  " << x1 << "  " << x2 << "  " << u
175             << G4endl;
176    }
177    bins[i] = nb;
178    xmin[i] = x1;
179    xmax[i] = x2;
180    unit[i] = u;
181  } else {
182    G4cout << "Histo::setHisto1D: WARNING! wrong histogram index " << i << G4endl;
183  }
184}
185
186//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
187
188void Histo::fill(G4int i, G4double x, G4double w)
189{
190  if(verbose > 1) {
191    G4cout << "fill histogram: #" << i << " at x= " << x
192           << "  weight= " << w
193           << G4endl;   
194  }
195#ifdef G4ANALYSIS_USE 
196  if(!tree) return;
197  if(i>=0 && i<nHisto) {
198    if(active[i]) histo[i]->fill((x/unit[i]), w);
199  } else {
200    G4cout << "Histo::fill: WARNING! wrong histogram index " << i << G4endl;
201  }
202#endif
203}
204
205//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
206
207void Histo::scale(G4int i, G4double x)
208{
209  if(verbose > 0) {
210    G4cout << "Scale histogram: #" << i << " by factor " << x << G4endl;   
211  }
212#ifdef G4ANALYSIS_USE 
213  if(!tree) return;
214  if(i>=0 && i<nHisto) {
215    histo[i]->scale(x);
216  } else {
217    G4cout << "Histo::scale: WARNING! wrong histogram index " << i << G4endl;
218  }
219#endif
220}
221
222//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
223
224void Histo::addTuple(const G4String& w1, const G4String& w2, const G4String& w3)
225{
226  tupleId = w1;
227  tupleName = w2;
228  tupleList = w3;
229}
230
231//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
232
233void Histo::fillTuple(const G4String& parname, G4double x)
234{
235  if(verbose > 1) {
236    G4cout << "fill tuple by parameter <" << parname << "> = " << x << G4endl; 
237  }
238#ifdef G4ANALYSIS_USE 
239  if(ntup) ntup->fill(ntup->findColumn(parname), (float)x);
240#endif
241}
242
243//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
244
245void Histo::addRow()
246{
247#ifdef G4ANALYSIS_USE
248  if(ntup) ntup->addRow();
249#endif
250} 
251
252//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
253
254void Histo::setFileName(const G4String& nam) 
255{
256  histName = nam;
257}
258
259//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
260
261void Histo::setFileType(const G4String& nam) 
262{
263  if(nam == "root" || nam == "hbook" || nam == "aida") histType = nam;
264  else if(nam == "xml" || nam == "XML") histType = "aida";
265}
266
267//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
268
Note: See TracBrowser for help on using the repository browser.