Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-06 16:42:59

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