File indexing completed on 2025-02-23 09:20:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include "HistoManager.hh"
0034
0035 #include "G4UnitsTable.hh"
0036
0037 #include <CLHEP/Units/SystemOfUnits.h>
0038 #include <TFile.h>
0039 #include <TH1D.h>
0040 #include <TTree.h>
0041
0042
0043
0044 HistoManager::~HistoManager()
0045 {
0046 delete fRootFile;
0047 }
0048
0049
0050
0051 void HistoManager::Book()
0052 {
0053
0054
0055
0056 G4String fileName = "AnaEx02.root";
0057 fRootFile = new TFile(fileName, "RECREATE");
0058 if (fRootFile == nullptr) {
0059 G4cout << " HistoManager::Book :"
0060 << " problem creating the ROOT TFile " << G4endl;
0061 return;
0062 }
0063
0064
0065 fHisto[0] = new TH1D("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * CLHEP::MeV);
0066
0067 fHisto[1] = new TH1D("EGap", "Edep in gap (MeV)", 100, 0., 100 * CLHEP::MeV);
0068
0069 fHisto[2] = new TH1D("LAbs", "trackL in absorber (mm)", 100, 0., 1 * CLHEP::m);
0070
0071 fHisto[3] = new TH1D("LGap", "trackL in gap (mm)", 100, 0., 50 * CLHEP::cm);
0072
0073 for (G4int i = 0; i < kMaxHisto; ++i) {
0074 if (fHisto[i] == nullptr) {
0075 G4cout << "\n can't create histo " << i << G4endl;
0076 }
0077 }
0078
0079
0080 fNtuple1 = new TTree("Ntuple1", "Edep");
0081 fNtuple1->Branch("Eabs", &fEabs, "Eabs/D");
0082 fNtuple1->Branch("Egap", &fEgap, "Egap/D");
0083
0084
0085 fNtuple2 = new TTree("Ntuple2", "TrackL");
0086 fNtuple2->Branch("Labs", &fLabs, "Labs/D");
0087 fNtuple2->Branch("Lgap", &fLgap, "Lgap/D");
0088
0089 G4cout << "\n----> Output file is open in " << fileName << G4endl;
0090 }
0091
0092
0093
0094 void HistoManager::Save()
0095 {
0096 if (fRootFile == nullptr) {
0097 return;
0098 }
0099
0100 fRootFile->Write();
0101 fRootFile->Close();
0102
0103 G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
0104 }
0105
0106
0107
0108 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
0109 {
0110 if (ih >= kMaxHisto) {
0111 G4cerr << "---> warning from HistoManager::FillHisto() : histo " << ih
0112 << " does not exist. (xbin=" << xbin << " weight=" << weight << ")" << G4endl;
0113 return;
0114 }
0115 if (fHisto[ih] != nullptr) {
0116 fHisto[ih]->Fill(xbin, weight);
0117 }
0118 }
0119
0120
0121
0122 void HistoManager::Normalize(G4int ih, G4double fac)
0123 {
0124 if (ih >= kMaxHisto) {
0125 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih
0126 << " does not exist. (fac=" << fac << ")" << G4endl;
0127 return;
0128 }
0129 if (fHisto[ih] != nullptr) {
0130 fHisto[ih]->Scale(fac);
0131 }
0132 }
0133
0134
0135
0136 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, G4double trackLAbs,
0137 G4double trackLGap)
0138 {
0139 fEabs = energyAbs;
0140 fEgap = energyGap;
0141 fLabs = trackLAbs;
0142 fLgap = trackLGap;
0143
0144 if (fNtuple1 != nullptr) {
0145 fNtuple1->Fill();
0146 }
0147 if (fNtuple2 != nullptr) {
0148 fNtuple2->Fill();
0149 }
0150 }
0151
0152
0153
0154 void HistoManager::PrintStatistic()
0155 {
0156 G4cout << "\n ----> print histograms statistic \n" << G4endl;
0157 for (auto h1 : fHisto) {
0158 const G4String name = h1->GetName();
0159
0160 G4String unitCategory;
0161 if (name[0] == 'E') {
0162 unitCategory = "Energy";
0163 }
0164 if (name[0] == 'L') {
0165 unitCategory = "Length";
0166 }
0167
0168 G4cout << name << ": mean = " << G4BestUnit(h1->GetMean(), unitCategory)
0169 << " rms = " << G4BestUnit(h1->GetRMS(), unitCategory) << G4endl;
0170 }
0171 }
0172
0173