File indexing completed on 2026-09-16 08:29:01
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 #include "HistoManager.hh"
0030
0031 #include "G4SystemOfUnits.hh"
0032 #include "G4UnitsTable.hh"
0033
0034
0035
0036 HistoManager::HistoManager()
0037 {
0038
0039 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0040 analysisManager->SetDefaultFileType("root");
0041
0042 }
0043
0044
0045
0046 void HistoManager::Book()
0047 {
0048
0049 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0050
0051 if (!fFactoryOn) {
0052
0053 analysisManager->SetVerboseLevel(1);
0054
0055 #ifdef G4MULTITHREADED
0056 analysisManager->SetNtupleMerging(true);
0057 #endif
0058
0059
0060 analysisManager->SetHistoDirectoryName("histo");
0061 analysisManager->SetNtupleDirectoryName("ntuple");
0062 }
0063
0064
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
0075
0076
0077
0078
0079
0080 analysisManager->CreateH1("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * MeV);
0081
0082 analysisManager->CreateH1("EGap", "Edep in gap (MeV)", 100, 0., 100 * MeV);
0083
0084 analysisManager->CreateH1("LAbs", "trackL in absorber (mm)", 100, 0., 1 * m);
0085
0086 analysisManager->CreateH1("LGap", "trackL in gap (mm)", 100, 0., 50 * cm);
0087
0088
0089
0090
0091
0092
0093
0094 analysisManager->CreateNtuple("Ntuple1", "Edep");
0095 analysisManager->CreateNtupleDColumn("Eabs");
0096 analysisManager->CreateNtupleDColumn("Egap");
0097 analysisManager->FinishNtuple();
0098
0099
0100
0101 analysisManager->CreateNtuple("Ntuple2", "TrackL");
0102 analysisManager->CreateNtupleDColumn("Labs");
0103 analysisManager->CreateNtupleDColumn("Lgap");
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
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
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
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
0148
0149 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, G4double trackLAbs,
0150 G4double trackLGap)
0151 {
0152 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0153
0154 analysisManager->FillNtupleDColumn(0, 0, energyAbs);
0155 analysisManager->FillNtupleDColumn(0, 1, energyGap);
0156 analysisManager->AddNtupleRow(0);
0157
0158 analysisManager->FillNtupleDColumn(1, 0, trackLAbs);
0159 analysisManager->FillNtupleDColumn(1, 1, trackLGap);
0160 analysisManager->AddNtupleRow(1);
0161 }
0162
0163
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
0186
0187
0188 G4cout << name << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
0189 << " rms = " << G4BestUnit(h1->rms(), unitCategory) << G4endl;
0190 }
0191 }
0192
0193