Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:05

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