File indexing completed on 2025-02-23 09:20:55
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 "HistoMessenger.hh"
0037
0038 #include "G4PhysicalConstants.hh"
0039 #include "G4SystemOfUnits.hh"
0040 #include "G4UnitsTable.hh"
0041
0042
0043
0044 HistoManager::HistoManager() : fHistoMessenger(0)
0045 {
0046 fileName[0] = "testem17";
0047 factoryOn = false;
0048 fNbHist = 0;
0049
0050
0051 for (G4int k = 0; k < kMaxHisto; k++) {
0052 fHistId[k] = 0;
0053 fHistPt[k] = 0;
0054 fExist[k] = false;
0055 fUnit[k] = 1.0;
0056 fWidth[k] = 1.0;
0057 fAscii[k] = false;
0058 }
0059
0060 fHistoMessenger = new HistoMessenger(this);
0061 }
0062
0063
0064
0065 HistoManager::~HistoManager()
0066 {
0067 delete fHistoMessenger;
0068 }
0069
0070
0071
0072 void HistoManager::Book()
0073 {
0074
0075 if (fNbHist == 0) return;
0076
0077
0078
0079
0080 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0081 analysisManager->SetDefaultFileType("root");
0082 analysisManager->SetVerboseLevel(0);
0083 G4String extension = analysisManager->GetFileType();
0084 fileName[1] = fileName[0] + "." + extension;
0085
0086
0087
0088 G4bool fileOpen = analysisManager->OpenFile(fileName[0]);
0089 if (!fileOpen) {
0090 G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1] << G4endl;
0091 return;
0092 }
0093
0094
0095
0096 analysisManager->SetFirstHistoId(1);
0097
0098 for (G4int k = 0; k < kMaxHisto; k++) {
0099 if (fExist[k]) {
0100 fHistId[k] = analysisManager->CreateH1(fLabel[k], fTitle[k], fNbins[k], fVmin[k], fVmax[k]);
0101 fHistPt[k] = analysisManager->GetH1(fHistId[k]);
0102 factoryOn = true;
0103 }
0104 }
0105
0106 if (factoryOn) G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl;
0107 }
0108
0109
0110
0111 void HistoManager::Save()
0112 {
0113 if (factoryOn) {
0114 G4cout << "\n----> HistoManager::save " << G4endl;
0115 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0116 analysisManager->Write();
0117 analysisManager->CloseFile();
0118 SaveAscii();
0119 G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl;
0120
0121 analysisManager->Clear();
0122 factoryOn = false;
0123 }
0124 }
0125
0126
0127
0128 void HistoManager::FillHisto(G4int ih, G4double e, G4double weight)
0129 {
0130 if (ih > kMaxHisto) {
0131 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
0132 << "does not fExist; e= " << e << " w= " << weight << G4endl;
0133 return;
0134 }
0135
0136 if (fHistPt[ih]) fHistPt[ih]->fill(e / fUnit[ih], weight);
0137 }
0138
0139
0140
0141 void HistoManager::SetHisto(G4int ih, G4int nbins, G4double vmin, G4double vmax,
0142 const G4String& unit)
0143 {
0144 if (ih > kMaxHisto) {
0145 G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih << "does not fExist"
0146 << G4endl;
0147 return;
0148 }
0149
0150 const G4String id[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
0151 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
0152 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"};
0153 const G4String title[] = {
0154 "dummy",
0155 "log10(Eloss/Emu) muIonization",
0156 "log10(Eloss/Emu) muPair",
0157 "log10(Eloss/Emu) muBrems",
0158 "log10(Eloss/Emu) muNuclear",
0159 "log10(Eloss/Emu) hIonization",
0160 "log10(Eloss/Emu) hPair",
0161 "log10(Eloss/Emu) hBrems",
0162 "log10(Eloss/Emu) muToMuonPair",
0163 "dummy",
0164 "dummy",
0165 "log10(Eloss/Emu) muIonization",
0166 "log10(Eloss/Emu) muPair",
0167 "log10(Eloss/Emu) muBrems",
0168 "log10(Eloss/Emu) muNuclear",
0169 "dummy",
0170 "dummy",
0171 "dummy",
0172 "log10(Eloss/Emu) muToMuonPair",
0173 "dummy",
0174 "dummy",
0175 "CS(1/mm) vs ekin muIonisation",
0176 "CS(1/mm) vs ekin muPair",
0177 "CS(1/mm) vs ekin muBrems",
0178 "CS(1/mm) vs ekin muNuclear",
0179 "dummy",
0180 "dummy",
0181 "dummy",
0182 "CS(1/mm) vs ekin muToMuonPair",
0183 "dummy"
0184 };
0185
0186 G4String titl = title[ih];
0187 fUnit[ih] = 1.;
0188
0189 if (unit != "none") {
0190 titl = title[ih] + " (" + unit + ")";
0191 fUnit[ih] = G4UnitDefinition::GetValueOf(unit);
0192 }
0193
0194 fExist[ih] = true;
0195 fLabel[ih] = "h" + id[ih];
0196 fTitle[ih] = titl;
0197 fNbins[ih] = nbins;
0198 fVmin[ih] = vmin;
0199 fVmax[ih] = vmax;
0200 fWidth[ih] = fUnit[ih] * (vmax - vmin) / nbins;
0201
0202 fNbHist++;
0203
0204 G4cout << "----> SetHisto " << ih << ": " << titl << "; " << nbins << " bins from " << vmin
0205 << " " << unit << " to " << vmax << " " << unit << G4endl;
0206 }
0207
0208
0209
0210 void HistoManager::Normalize(G4int ih, G4double fac)
0211 {
0212 if (ih >= kMaxHisto) {
0213 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih << " fac= " << fac
0214 << G4endl;
0215 return;
0216 }
0217
0218 if (fHistPt[ih]) fHistPt[ih]->scale(fac);
0219 }
0220
0221
0222
0223 void HistoManager::PrintHisto(G4int ih)
0224 {
0225 if (ih < kMaxHisto) {
0226 fAscii[ih] = true;
0227 fAscii[0] = true;
0228 }
0229 else
0230 G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih << "does not exist"
0231 << G4endl;
0232 }
0233
0234
0235
0236 #include <fstream>
0237
0238 void HistoManager::SaveAscii()
0239 {
0240 if (!fAscii[0]) return;
0241
0242 G4String name = fileName[0] + ".ascii";
0243 std::ofstream File(name, std::ios::out);
0244 if (!File) {
0245 G4cout << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl;
0246 return;
0247 }
0248
0249 File.setf(std::ios::scientific, std::ios::floatfield);
0250
0251
0252 for (G4int ih = 0; ih < kMaxHisto; ih++) {
0253 if (fHistPt[ih] && fAscii[ih]) {
0254 File << "\n 1D histogram " << ih << ": " << fTitle[ih] << "\n \n \t X \t\t Y"
0255 << G4endl;
0256
0257 for (G4int iBin = 0; iBin < fNbins[ih]; iBin++) {
0258 File << " " << iBin << "\t" << fHistPt[ih]->axis().bin_center(iBin) << "\t"
0259 << fHistPt[ih]->bin_height(iBin) << G4endl;
0260 }
0261 }
0262 }
0263 }
0264
0265