File indexing completed on 2025-02-23 09:20:41
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
0034 #include "HistoManager.hh"
0035
0036 #include "G4SystemOfUnits.hh"
0037 #include "G4UnitsTable.hh"
0038
0039
0040
0041 HistoManager::HistoManager()
0042 {
0043
0044 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0045 analysisManager->SetDefaultFileType("root");
0046
0047 }
0048
0049
0050
0051 void HistoManager::Book()
0052 {
0053
0054 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0055
0056 if (!fFactoryOn) {
0057
0058 analysisManager->SetVerboseLevel(1);
0059
0060 #ifdef G4MULTITHREADED
0061 analysisManager->SetNtupleMerging(true);
0062 #endif
0063
0064
0065 analysisManager->SetHistoDirectoryName("histo");
0066 analysisManager->SetNtupleDirectoryName("ntuple");
0067 }
0068
0069
0070
0071 G4bool fileOpen = analysisManager->OpenFile("AnaEx01");
0072 if (!fileOpen) {
0073 G4cerr << "\n---> HistoManager::Book(): cannot open " << analysisManager->GetFileName()
0074 << G4endl;
0075 return;
0076 }
0077
0078 if (!fFactoryOn) {
0079
0080
0081
0082
0083
0084
0085 analysisManager->CreateH1("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * MeV);
0086
0087 analysisManager->CreateH1("EGap", "Edep in gap (MeV)", 100, 0., 100 * MeV);
0088
0089 analysisManager->CreateH1("LAbs", "trackL in absorber (mm)", 100, 0., 1 * m);
0090
0091 analysisManager->CreateH1("LGap", "trackL in gap (mm)", 100, 0., 50 * cm);
0092
0093
0094
0095
0096
0097
0098
0099 analysisManager->CreateNtuple("Ntuple1", "Edep");
0100 analysisManager->CreateNtupleDColumn("Eabs");
0101 analysisManager->CreateNtupleDColumn("Egap");
0102 analysisManager->FinishNtuple();
0103
0104
0105
0106 analysisManager->CreateNtuple("Ntuple2", "TrackL");
0107 analysisManager->CreateNtupleDColumn("Labs");
0108 analysisManager->CreateNtupleDColumn("Lgap");
0109 analysisManager->FinishNtuple();
0110
0111 fFactoryOn = true;
0112 }
0113
0114 G4cout << "\n----> Output file is open in " << analysisManager->GetFileName() << "."
0115 << analysisManager->GetFileType() << G4endl;
0116 }
0117
0118
0119
0120 void HistoManager::Save()
0121 {
0122 if (!fFactoryOn) {
0123 return;
0124 }
0125
0126 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0127 analysisManager->Write();
0128 analysisManager->CloseFile();
0129
0130 G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
0131 }
0132
0133
0134
0135 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
0136 {
0137 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0138 analysisManager->FillH1(ih, xbin, weight);
0139 }
0140
0141
0142
0143 void HistoManager::Normalize(G4int ih, G4double fac)
0144 {
0145 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0146 auto h1 = analysisManager->GetH1(ih);
0147 if (h1 != nullptr) {
0148 h1->scale(fac);
0149 }
0150 }
0151
0152
0153
0154 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, G4double trackLAbs,
0155 G4double trackLGap)
0156 {
0157 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0158
0159 analysisManager->FillNtupleDColumn(0, 0, energyAbs);
0160 analysisManager->FillNtupleDColumn(0, 1, energyGap);
0161 analysisManager->AddNtupleRow(0);
0162
0163 analysisManager->FillNtupleDColumn(1, 0, trackLAbs);
0164 analysisManager->FillNtupleDColumn(1, 1, trackLGap);
0165 analysisManager->AddNtupleRow(1);
0166 }
0167
0168
0169
0170 void HistoManager::PrintStatistic()
0171 {
0172 if (!fFactoryOn) {
0173 return;
0174 }
0175
0176 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0177
0178 G4cout << "\n ----> print histograms statistic \n" << G4endl;
0179 for (G4int i = 0; i < analysisManager->GetNofH1s(); ++i) {
0180 G4String name = analysisManager->GetH1Name(i);
0181 auto h1 = analysisManager->GetH1(i);
0182
0183 G4String unitCategory;
0184 if (name[0U] == 'E') {
0185 unitCategory = "Energy";
0186 }
0187 if (name[0U] == 'L') {
0188 unitCategory = "Length";
0189 }
0190
0191
0192
0193 G4cout << name << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
0194 << " rms = " << G4BestUnit(h1->rms(), unitCategory) << G4endl;
0195 }
0196 }
0197
0198