Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /geant4/examples/extended/electromagnetic/TestEm9/src/Histo.cc was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file Histo.cc
0027 /// \brief Implementation of the Histo class
0028 
0029 //---------------------------------------------------------------------------
0030 //
0031 // ClassName:   Histo - Generic histogram/ntuple manager class
0032 //
0033 //
0034 // Author:      V.Ivanchenko 30.10.03
0035 //
0036 //----------------------------------------------------------------------------
0037 //
0038 
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0040 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0041 
0042 #include "Histo.hh"
0043 
0044 #include "HistoMessenger.hh"
0045 
0046 #include "G4RootAnalysisManager.hh"
0047 
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 Histo::Histo() : fManager(0), fMessenger(0)
0052 {
0053   fMessenger = new HistoMessenger(this);
0054 
0055   fHistName = "test";
0056   fHistType = "root";
0057   fTupleName = "tuple";
0058   fTupleTitle = "test";
0059   fNHisto = 0;
0060   fVerbose = 0;
0061   fDefaultAct = true;
0062   fHistoActive = false;
0063   fNtupleActive = false;
0064 }
0065 
0066 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0067 
0068 Histo::~Histo()
0069 {
0070   delete fMessenger;
0071 }
0072 
0073 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0074 
0075 void Histo::Book()
0076 {
0077   if (!(fHistoActive || fNtupleActive)) {
0078     return;
0079   }
0080 
0081   // Always creating analysis manager
0082   fManager = G4RootAnalysisManager::Instance();
0083 
0084   // Creating a tree mapped to a new hbook file.
0085   G4String nam = fHistName + "." + fHistType;
0086 
0087   // Open file histogram file
0088   if (!fManager->OpenFile(nam)) {
0089     G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
0090     fHistoActive = false;
0091     fNtupleActive = false;
0092     return;
0093   }
0094   G4cout << "### Histo::Save: Opended file <" << nam << ">  for " << fNHisto << " histograms "
0095          << G4endl;
0096 
0097   // Creating an 1-dimensional histograms in the root directory of the tree
0098   for (G4int i = 0; i < fNHisto; ++i) {
0099     if (fActive[i]) {
0100       G4String ss = "h" + fIds[i];
0101       fHisto[i] = fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
0102       if (fVerbose > 0) {
0103         G4cout << "Created histogram #" << i << "  id= " << fHisto[i] << "  " << ss << "  "
0104                << fTitles[i] << G4endl;
0105       }
0106     }
0107   }
0108   // Creating a tuple factory, whose tuples will be handled by the tree
0109   if (fNtupleActive) {
0110     fManager->CreateNtuple(fTupleName, fTupleTitle);
0111     G4int i;
0112     G4int n = fNtupleI.size();
0113     for (i = 0; i < n; ++i) {
0114       if (fTupleI[i] == -1) {
0115         fTupleI[i] = fManager->CreateNtupleIColumn(fNtupleI[i]);
0116       }
0117     }
0118     n = fNtupleF.size();
0119     for (i = 0; i < n; ++i) {
0120       if (fTupleF[i] == -1) {
0121         fTupleF[i] = fManager->CreateNtupleFColumn(fNtupleF[i]);
0122       }
0123     }
0124     n = fNtupleD.size();
0125     for (i = 0; i < n; ++i) {
0126       if (fTupleD[i] == -1) {
0127         fTupleD[i] = fManager->CreateNtupleDColumn(fNtupleD[i]);
0128       }
0129     }
0130   }
0131 }
0132 
0133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0134 
0135 void Histo::Save()
0136 {
0137   if (!(fHistoActive || fNtupleActive)) {
0138     return;
0139   }
0140 
0141   // Creating a tree mapped to a new hbook file.
0142   G4String nam = fHistName + "." + fHistType;
0143 
0144   // Write histogram file
0145   if (!fManager->Write()) {
0146     G4Exception("Histo::Save()", "hist01", FatalException, "Cannot write ROOT file.");
0147   }
0148   if (fVerbose > 0) {
0149     G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
0150   }
0151   if (fManager->CloseFile() && fVerbose > 0) {
0152     G4cout << "                 File is closed" << G4endl;
0153   }
0154   fManager->Clear();
0155 }
0156 
0157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0158 
0159 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb, G4double x1, G4double x2,
0160                   G4double u)
0161 {
0162   if (fVerbose > 0) {
0163     G4cout << "Histo::Add1D: New histogram will be booked: #" << id << "  <" << name << "  " << nb
0164            << "  " << x1 << "  " << x2 << "  " << u << G4endl;
0165   }
0166   ++fNHisto;
0167   x1 /= u;
0168   x2 /= u;
0169   fActive.push_back(fDefaultAct);
0170   fBins.push_back(nb);
0171   fXmin.push_back(x1);
0172   fXmax.push_back(x2);
0173   fUnit.push_back(u);
0174   fIds.push_back(id);
0175   fTitles.push_back(name);
0176   fHisto.push_back(-1);
0177 }
0178 
0179 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0180 
0181 void Histo::SetHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u)
0182 {
0183   if (i >= 0 && i < fNHisto) {
0184     if (fVerbose > 0) {
0185       G4cout << "Histo::SetHisto1D: #" << i << "  " << nb << "  " << x1 << "  " << x2 << "  " << u
0186              << G4endl;
0187     }
0188     fBins[i] = nb;
0189     fXmin[i] = x1;
0190     fXmax[i] = x2;
0191     fUnit[i] = u;
0192     fActive[i] = true;
0193     fHistoActive = true;
0194   }
0195   else {
0196     G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index " << i << G4endl;
0197   }
0198 }
0199 
0200 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0201 
0202 void Histo::Activate(G4int i, G4bool val)
0203 {
0204   if (fVerbose > 1) {
0205     G4cout << "Histo::Activate: Histogram: #" << i << "   " << val << G4endl;
0206   }
0207   if (i >= 0 && i < fNHisto) {
0208     fActive[i] = val;
0209     if (val) {
0210       fHistoActive = true;
0211     }
0212   }
0213 }
0214 
0215 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0216 
0217 void Histo::Fill(G4int i, G4double x, G4double w)
0218 {
0219   if (!fHistoActive) {
0220     return;
0221   }
0222   if (fVerbose > 1) {
0223     G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x << "  weight= " << w << G4endl;
0224   }
0225   if (i >= 0 && i < fNHisto) {
0226     if (fActive[i]) {
0227       fManager->FillH1(fHisto[i], x / fUnit[i], w);
0228     }
0229   }
0230   else {
0231     G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
0232   }
0233 }
0234 
0235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0236 
0237 void Histo::ScaleH1(G4int i, G4double x)
0238 {
0239   if (!fHistoActive) {
0240     return;
0241   }
0242   if (fVerbose > 0) {
0243     G4cout << "Histo::Scale: Histogram: #" << i << " by factor " << x << G4endl;
0244   }
0245   if (i >= 0 && i < fNHisto) {
0246     if (fActive[i]) {
0247       fManager->GetH1(fHisto[i])->scale(x);
0248     }
0249   }
0250   else {
0251     G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
0252   }
0253 }
0254 
0255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0256 
0257 void Histo::AddTuple(const G4String& w1)
0258 {
0259   fTupleTitle = w1;
0260 }
0261 
0262 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0263 
0264 void Histo::AddTupleI(const G4String& w1)
0265 {
0266   fNtupleActive = true;
0267   fNtupleI.push_back(w1);
0268   fTupleI.push_back(-1);
0269 }
0270 
0271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0272 
0273 void Histo::AddTupleF(const G4String& w1)
0274 {
0275   fNtupleActive = true;
0276   fNtupleF.push_back(w1);
0277   fTupleF.push_back(-1);
0278 }
0279 
0280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0281 
0282 void Histo::AddTupleD(const G4String& w1)
0283 {
0284   fNtupleActive = true;
0285   fNtupleD.push_back(w1);
0286   fTupleD.push_back(-1);
0287 }
0288 
0289 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0290 
0291 void Histo::FillTupleI(G4int i, G4int x)
0292 {
0293   if (!fNtupleActive) {
0294     return;
0295   }
0296   G4int n = fNtupleI.size();
0297   if (i >= 0 && i < n) {
0298     if (fVerbose > 1) {
0299       G4cout << "Histo::FillTupleI: i= " << i << "  id= " << fTupleI[i] << "   <" << fNtupleI[i]
0300              << "> = " << x << G4endl;
0301     }
0302     fManager->FillNtupleIColumn(fTupleI[i], x);
0303   }
0304   else {
0305     G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index " << i << G4endl;
0306   }
0307 }
0308 
0309 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0310 
0311 void Histo::FillTupleF(G4int i, G4float x)
0312 {
0313   if (!fNtupleActive) {
0314     return;
0315   }
0316   G4int n = fNtupleF.size();
0317   if (i >= 0 && i < n) {
0318     if (fVerbose > 1) {
0319       G4cout << "Histo::FillTupleF: i= " << i << "  id= " << fTupleF[i] << "   <" << fNtupleF[i]
0320              << "> = " << x << G4endl;
0321     }
0322     fManager->FillNtupleFColumn(fTupleF[i], x);
0323   }
0324   else {
0325     G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index " << i << G4endl;
0326   }
0327 }
0328 
0329 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0330 
0331 void Histo::FillTupleD(G4int i, G4double x)
0332 {
0333   if (!fNtupleActive) {
0334     return;
0335   }
0336   G4int n = fNtupleD.size();
0337   if (i >= 0 && i < n) {
0338     if (fVerbose > 1) {
0339       G4cout << "Histo::FillTupleD: i= " << i << "  id= " << fTupleD[i] << "   <" << fNtupleD[i]
0340              << "> = " << x << G4endl;
0341     }
0342     fManager->FillNtupleDColumn(fTupleD[i], x);
0343   }
0344   else {
0345     G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index " << i << G4endl;
0346   }
0347 }
0348 
0349 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0350 
0351 void Histo::AddRow()
0352 {
0353   if (!fNtupleActive) {
0354     return;
0355   }
0356   fManager->AddNtupleRow();
0357 }
0358 
0359 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0360 
0361 void Histo::SetFileName(const G4String& nam)
0362 {
0363   fHistName = nam;
0364   fHistoActive = true;
0365 }
0366 
0367 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0368 
0369 void Histo::SetFileType(const G4String& nam)
0370 {
0371   // format other than ROOT is not tested
0372   if (nam == "root" || nam == "ROOT") {
0373     fHistType = "root";
0374   }
0375   else if (nam == "xml" || nam == "XML") {
0376     fHistType = "xml";
0377   }
0378   else if (nam == "ascii" || nam == "ASCII" || nam == "Csv" || nam == "csv" || nam == "CSV") {
0379     fHistType = "ascii";
0380   }
0381 }
0382 
0383 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......