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