Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-16 08:29:01

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 HistoManager.cc
0027 /// \brief Implementation of the HistoManager class
0028 
0029 #include "HistoManager.hh"
0030 
0031 #include "G4SystemOfUnits.hh"
0032 #include "G4UnitsTable.hh"
0033 
0034 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0035 
0036 HistoManager::HistoManager()
0037 {
0038   // Create or get analysis manager
0039   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0040   analysisManager->SetDefaultFileType("root");
0041   // the default file type can be overriden in run macro
0042 }
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 
0046 void HistoManager::Book()
0047 {
0048   // Create or get analysis manager
0049   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0050 
0051   if (!fFactoryOn) {
0052     //
0053     analysisManager->SetVerboseLevel(1);
0054     // Only merge in MT mode to avoid warning when running in Sequential mode
0055 #ifdef G4MULTITHREADED
0056     analysisManager->SetNtupleMerging(true);
0057 #endif
0058 
0059     // Create directories
0060     analysisManager->SetHistoDirectoryName("histo");
0061     analysisManager->SetNtupleDirectoryName("ntuple");
0062   }
0063 
0064   // Open an output file
0065   //
0066   G4bool fileOpen = analysisManager->OpenFile("AnaEx01");
0067   if (!fileOpen) {
0068     G4cerr << "\n---> HistoManager::Book(): cannot open " << analysisManager->GetFileName()
0069            << G4endl;
0070     return;
0071   }
0072 
0073   if (!fFactoryOn) {
0074     // Create histograms.
0075     // Histogram ids are generated automatically starting from 0.
0076     // The start value can be changed by:
0077     // analysisManager->SetFirstHistoId(1);
0078 
0079     // id = 0
0080     analysisManager->CreateH1("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * MeV);
0081     // id = 1
0082     analysisManager->CreateH1("EGap", "Edep in gap (MeV)", 100, 0., 100 * MeV);
0083     // id = 2
0084     analysisManager->CreateH1("LAbs", "trackL in absorber (mm)", 100, 0., 1 * m);
0085     // id = 3
0086     analysisManager->CreateH1("LGap", "trackL in gap (mm)", 100, 0., 50 * cm);
0087 
0088     // Create ntuples.
0089     // Ntuples ids are generated automatically starting from 0.
0090     // The start value can be changed by:
0091     // analysisManager->SetFirstMtupleId(1);
0092 
0093     // Create 1st ntuple (id = 0)
0094     analysisManager->CreateNtuple("Ntuple1", "Edep");
0095     analysisManager->CreateNtupleDColumn("Eabs");  // column Id = 0
0096     analysisManager->CreateNtupleDColumn("Egap");  // column Id = 1
0097     analysisManager->FinishNtuple();
0098 
0099     // Create 2nd ntuple (id = 1)
0100     //
0101     analysisManager->CreateNtuple("Ntuple2", "TrackL");
0102     analysisManager->CreateNtupleDColumn("Labs");  // column Id = 0
0103     analysisManager->CreateNtupleDColumn("Lgap");  // column Id = 1
0104     analysisManager->FinishNtuple();
0105 
0106     fFactoryOn = true;
0107   }
0108 
0109   G4cout << "\n----> Output file is open in " << analysisManager->GetFileName() << "."
0110          << analysisManager->GetFileType() << G4endl;
0111 }
0112 
0113 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0114 
0115 void HistoManager::Save()
0116 {
0117   if (!fFactoryOn) {
0118     return;
0119   }
0120 
0121   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0122   analysisManager->Write();
0123   analysisManager->CloseFile();
0124 
0125   G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
0126 }
0127 
0128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0129 
0130 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
0131 {
0132   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0133   analysisManager->FillH1(ih, xbin, weight);
0134 }
0135 
0136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0137 
0138 void HistoManager::Normalize(G4int ih, G4double fac)
0139 {
0140   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0141   auto h1 = analysisManager->GetH1(ih);
0142   if (h1 != nullptr) {
0143     h1->scale(fac);
0144   }
0145 }
0146 
0147 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0148 
0149 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, G4double trackLAbs,
0150                               G4double trackLGap)
0151 {
0152   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0153   // Fill 1st ntuple ( id = 0)
0154   analysisManager->FillNtupleDColumn(0, 0, energyAbs);
0155   analysisManager->FillNtupleDColumn(0, 1, energyGap);
0156   analysisManager->AddNtupleRow(0);
0157   // Fill 2nd ntuple ( id = 1)
0158   analysisManager->FillNtupleDColumn(1, 0, trackLAbs);
0159   analysisManager->FillNtupleDColumn(1, 1, trackLGap);
0160   analysisManager->AddNtupleRow(1);
0161 }
0162 
0163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0164 
0165 void HistoManager::PrintStatistic()
0166 {
0167   if (!fFactoryOn) {
0168     return;
0169   }
0170 
0171   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0172 
0173   G4cout << "\n ----> print histograms statistic \n" << G4endl;
0174   for (G4int i = 0; i < analysisManager->GetNofH1s(); ++i) {
0175     G4String name = analysisManager->GetH1Name(i);
0176     auto h1 = analysisManager->GetH1(i);
0177 
0178     G4String unitCategory;
0179     if (name[0U] == 'E') {
0180       unitCategory = "Energy";
0181     }
0182     if (name[0U] == 'L') {
0183       unitCategory = "Length";
0184     }
0185     // we use an explicit unsigned int type for operator [] argument
0186     // to avoid problems with windows compiler
0187 
0188     G4cout << name << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
0189            << " rms = " << G4BestUnit(h1->rms(), unitCategory) << G4endl;
0190   }
0191 }
0192 
0193 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......