Histo.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 /// \file hadronic/Hadr00/src/Histo.cc
00027 /// \brief Implementation of the Histo class
00028 //
00029 // $Id$
00030 //
00031 //---------------------------------------------------------------------------
00032 //
00033 // ClassName:   Histo - Generic histogram/ntuple manager class
00034 //
00035 //
00036 // Author:      V.Ivanchenko 30.10.03
00037 //
00038 //----------------------------------------------------------------------------
00039 //
00040 
00041 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00042 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00043 
00044 #include "Histo.hh"
00045 #include "HistoMessenger.hh"
00046 #include "G4RootAnalysisManager.hh"
00047 
00048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00050 
00051 Histo::Histo()
00052 {
00053   fManager   = 0;
00054   fMessenger = new HistoMessenger(this);
00055  
00056   fHistName   = "test";
00057   fHistType   = "root";
00058   fTupleName  = "tuple";
00059   fTupleTitle = "test";
00060   fNHisto     = 0;
00061   fVerbose    = 0;
00062   fDefaultAct = true;
00063   fHistoActive= false;
00064   fNtupleActive= false;
00065 }
00066 
00067 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00068 
00069 Histo::~Histo()
00070 {
00071   delete fMessenger;
00072   delete fManager;
00073 }
00074 
00075 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00076 
00077 void Histo::Book()
00078 {
00079   if(!(fHistoActive || fNtupleActive)) { return; }
00080 
00081   // Always creating analysis manager
00082   fManager = G4RootAnalysisManager::Instance(); 
00083 
00084   // Creating a tree mapped to a new hbook file.
00085   G4String nam = fHistName + "." + fHistType;
00086 
00087   // Open file histogram file
00088   if(!fManager->OpenFile(nam)) {
00089     G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
00090     fHistoActive = false;
00091     fNtupleActive = false;
00092     return; 
00093   }
00094   G4cout << "### Histo::Save: Opended file <" << nam << ">  for " 
00095          << fNHisto << " histograms " << G4endl;
00096 
00097   // Creating an 1-dimensional histograms in the root directory of the tree
00098   for(G4int i=0; i<fNHisto; ++i) {
00099     if(fActive[i]) {
00100       G4String ss = "h" + fIds[i];
00101       fHisto[i] = fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
00102       if(fVerbose > 0) {
00103         G4cout << "Created histogram #" << i << "  id= " << fHisto[i]
00104                << "  "  << ss << "  " << fTitles[i] << G4endl;
00105       }
00106     }
00107   }
00108   // Creating a tuple factory, whose tuples will be handled by the tree
00109   if(fNtupleActive) {
00110     fManager->CreateNtuple(fTupleName,fTupleTitle); 
00111     G4int i;
00112     G4int n = fNtupleI.size();
00113     for(i=0; i<n; ++i) { 
00114       if(fTupleI[i] == -1) {  fTupleI[i] = fManager->CreateNtupleIColumn(fNtupleI[i]); }
00115     }
00116     n = fNtupleF.size();
00117     for(i=0; i<n; ++i) { 
00118       if(fTupleF[i] == -1) {  fTupleF[i] = fManager->CreateNtupleFColumn(fNtupleF[i]); }
00119     }
00120     n = fNtupleD.size();
00121     for(i=0; i<n; ++i) { 
00122       if(fTupleD[i] == -1) {  fTupleD[i] = fManager->CreateNtupleDColumn(fNtupleD[i]); }
00123     }
00124   }
00125 } 
00126 
00127 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00128 
00129 void Histo::Save()
00130 {
00131   if(!(fHistoActive || fNtupleActive)) { return; }
00132 
00133   // Creating a tree mapped to a new hbook file.
00134   G4String nam = fHistName + "." + fHistType;
00135 
00136   // Write histogram file
00137   if(!fManager->Write()) {
00138     G4cout   << "Histo::Save: FATAL ERROR writing ROOT file" << G4endl;
00139     exit(1);
00140   }
00141   if(fVerbose > 0) {
00142     G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
00143   }
00144   if(fManager->CloseFile() && fVerbose > 0) {
00145     G4cout << "                 File is closed" << G4endl;
00146   }
00147   delete G4RootAnalysisManager::Instance();
00148   fManager = 0;
00149 } 
00150 
00151 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00152 
00153 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb, 
00154                   G4double x1, G4double x2, G4double u)
00155 {
00156   if(fVerbose > 0) {
00157     G4cout << "Histo::Add1D: New histogram will be booked: #" << id << "  <" << name 
00158            << "  " << nb << "  " << x1 << "  " << x2 << "  " << u 
00159            << G4endl;
00160   }
00161   ++fNHisto;
00162   x1 /= u;
00163   x2 /= u;
00164   fActive.push_back(fDefaultAct);
00165   fBins.push_back(nb);
00166   fXmin.push_back(x1);
00167   fXmax.push_back(x2);
00168   fUnit.push_back(u);
00169   fIds.push_back(id);
00170   fTitles.push_back(name);
00171   fHisto.push_back(-1);
00172 }
00173 
00174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00175 
00176 void Histo::SetHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u)
00177 {
00178   if(i>=0 && i<fNHisto) {
00179     if(fVerbose > 0) {
00180       G4cout << "Histo::SetHisto1D: #" << i  
00181              << "  " << nb << "  " << x1 << "  " << x2 << "  " << u 
00182              << G4endl;
00183     }
00184     fBins[i] = nb;
00185     fXmin[i] = x1;
00186     fXmax[i] = x2;
00187     fUnit[i] = u;
00188     fActive[i] = true;
00189     fHistoActive = true;
00190   } else {
00191     G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index " << i << G4endl;
00192   }
00193 }
00194 
00195 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00196 
00197 void Histo::Activate(G4int i, G4bool val)
00198 {
00199   if(fVerbose > 1) {
00200     G4cout << "Histo::Activate: Histogram: #" << i << "   "  
00201            << val << G4endl;   
00202   }
00203   if(i>=0 && i<fNHisto) { 
00204     fActive[i] = val; 
00205     if(val) { fHistoActive = true; }
00206   }
00207 }
00208 
00209 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00210 
00211 void Histo::Fill(G4int i, G4double x, G4double w)
00212 {
00213   if(!fHistoActive) { return; }
00214   if(fVerbose > 1) {
00215     G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x 
00216            << "  weight= " << w
00217            << G4endl;   
00218   }
00219   if(i>=0 && i<fNHisto) {
00220     if(fActive[i]) { fManager->FillH1(fHisto[i], x/fUnit[i], w); }
00221   } else {
00222     G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
00223   }
00224 }
00225 
00226 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00227 
00228 void Histo::ScaleH1(G4int i, G4double x)
00229 {
00230   if(!fHistoActive) { return; }
00231   if(fVerbose > 0) {
00232     G4cout << "Histo::Scale: Histogram: #" << i << " by factor " << x << G4endl;   
00233   }
00234   if(i>=0 && i<fNHisto) {
00235     if(fActive[i]) { fManager->GetH1(fHisto[i])->scale(x); }
00236   } else {
00237     G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
00238   }
00239 }
00240 
00241 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00242 
00243 void Histo::AddTuple(const G4String& w1)
00244 {
00245   fTupleTitle = w1;
00246 }
00247 
00248 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00249 
00250 void Histo::AddTupleI(const G4String& w1)
00251 {
00252   fNtupleActive = true;
00253   fNtupleI.push_back(w1);
00254   fTupleI.push_back(-1);
00255 }
00256 
00257 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00258 
00259 void Histo::AddTupleF(const G4String& w1)
00260 {
00261   fNtupleActive = true;
00262   fNtupleF.push_back(w1);
00263   fTupleF.push_back(-1);
00264 }
00265 
00266 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00267 
00268 void Histo::AddTupleD(const G4String& w1)
00269 {
00270   fNtupleActive = true;
00271   fNtupleD.push_back(w1);
00272   fTupleD.push_back(-1);
00273 }
00274 
00275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00276 
00277 void Histo::FillTupleI(G4int i, G4int x)
00278 {
00279   if(!fNtupleActive) { return; }
00280   G4int n = fNtupleI.size();
00281   if(i >= 0 && i < n) {
00282     if(fVerbose > 1) {
00283       G4cout << "Histo::FillTupleI: i= " << i << "  id= " << fTupleI[i]
00284              << "   <" << fNtupleI[i] << "> = " << x << G4endl; 
00285     }
00286     fManager->FillNtupleIColumn(fTupleI[i], x); 
00287   } else {
00288     G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index " << i << G4endl;
00289   }
00290 }
00291 
00292 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00293 
00294 void Histo::FillTupleF(G4int i, G4float x)
00295 {
00296   if(!fNtupleActive) { return; }
00297   G4int n = fNtupleF.size();
00298   if(i >= 0 && i < n) {
00299     if(fVerbose > 1) {
00300       G4cout << "Histo::FillTupleF: i= " << i << "  id= " << fTupleF[i]
00301              << "   <" << fNtupleF[i] << "> = " << x << G4endl; 
00302     }
00303     fManager->FillNtupleFColumn(fTupleF[i], x); 
00304   } else {
00305     G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index " << i << G4endl;
00306   }
00307 }
00308 
00309 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00310 
00311 void Histo::FillTupleD(G4int i, G4double x)
00312 {
00313   if(!fNtupleActive) { return; }
00314   G4int n = fNtupleD.size();
00315   if(i >= 0 && i < n) {
00316     if(fVerbose > 1) {
00317       G4cout << "Histo::FillTupleD: i= " << i << "  id= " << fTupleD[i]
00318              << "   <" << fNtupleD[i] << "> = " << x << G4endl; 
00319     }
00320     fManager->FillNtupleDColumn(fTupleD[i], x); 
00321   } else {
00322     G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index " << i << G4endl;
00323   }
00324 }
00325 
00326 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00327 
00328 void Histo::AddRow()
00329 {
00330   if(!fNtupleActive) { return; }
00331   fManager->AddNtupleRow();
00332 } 
00333 
00334 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00335 
00336 void Histo::SetFileName(const G4String& nam) 
00337 {
00338   fHistName = nam;
00339   fHistoActive = true;
00340 }
00341 
00342 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00343 
00344 void Histo::SetFileType(const G4String& nam) 
00345 {
00346   if(nam == "root" || nam == "ROOT" )   { fHistType = "root"; }
00347   else if(nam == "xml" || nam == "XML") { fHistType = "xml"; }
00348   else if(nam == "ascii" || nam == "ASCII" || 
00349           nam == "Csv" || nam == "csv" || nam == "CSV") { fHistType = "ascii"; }
00350 }
00351 
00352 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00353 

Generated on 19 Feb 2017 for QwGeant4 by  doxygen 1.6.1