File indexing completed on 2025-04-04 08:05:05
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
0035
0036
0037
0038
0039
0040
0041
0042
0043 #include "Histo.hh"
0044
0045 #include "HistoMessenger.hh"
0046
0047 #include "G4RootAnalysisManager.hh"
0048
0049
0050
0051
0052 Histo::Histo()
0053 : fManager(nullptr),
0054 fHistName("test"),
0055 fTupleName("tuple"),
0056 fTupleTitle("test"),
0057 fNHisto(0),
0058 fVerbose(0),
0059 fDefaultAct(true),
0060 fHistoActive(false),
0061 fNtupleActive(false)
0062 {
0063 fMessenger = new HistoMessenger(this);
0064 }
0065
0066
0067
0068 Histo::~Histo()
0069 {
0070 delete fMessenger;
0071 }
0072
0073
0074
0075 void Histo::Book()
0076 {
0077 if (!(fHistoActive || fNtupleActive)) {
0078 return;
0079 }
0080
0081
0082 fManager = G4RootAnalysisManager::Instance();
0083
0084
0085 G4String nam = fHistName + ".root";
0086
0087
0088 if (!fManager->OpenFile(nam)) {
0089 G4cout << "Histo::Book: ERROR open file <" << nam << ">" << G4endl;
0090 fHistoActive = false;
0091 fNtupleActive = false;
0092 return;
0093 }
0094 G4cout << "### Histo::Save: Opended file <" << nam << "> for " << fNHisto << " histograms "
0095 << G4endl;
0096
0097
0098 for (G4int i = 0; i < fNHisto; ++i) {
0099 if (fActive[i]) {
0100 G4String ss = "h" + fIds[i];
0101 fHisto[i] = fManager->CreateH1(ss, fTitles[i], fBins[i], fXmin[i], fXmax[i]);
0102 if (fVerbose > 0) {
0103 G4cout << "Created histogram #" << i << " id= " << fHisto[i] << " " << ss << " "
0104 << fTitles[i] << G4endl;
0105 }
0106 }
0107 }
0108
0109 if (fNtupleActive) {
0110 fManager->CreateNtuple(fTupleName, fTupleTitle);
0111 G4int i;
0112 G4int n = fNtupleI.size();
0113 for (i = 0; i < n; ++i) {
0114 if (fTupleI[i] == -1) {
0115 fTupleI[i] = fManager->CreateNtupleIColumn(fNtupleI[i]);
0116 }
0117 }
0118 n = fNtupleF.size();
0119 for (i = 0; i < n; ++i) {
0120 if (fTupleF[i] == -1) {
0121 fTupleF[i] = fManager->CreateNtupleFColumn(fNtupleF[i]);
0122 }
0123 }
0124 n = fNtupleD.size();
0125 for (i = 0; i < n; ++i) {
0126 if (fTupleD[i] == -1) {
0127 fTupleD[i] = fManager->CreateNtupleDColumn(fNtupleD[i]);
0128 }
0129 }
0130 }
0131 }
0132
0133
0134
0135 void Histo::Save()
0136 {
0137 if (!(fHistoActive || fNtupleActive)) {
0138 return;
0139 }
0140
0141
0142 if (!fManager->Write()) {
0143 G4cout << "Histo::Save: FATAL ERROR writing ROOT file" << G4endl;
0144 exit(1);
0145 }
0146 if (fVerbose > 0) {
0147 G4cout << "### Histo::Save: Histograms and Ntuples are saved" << G4endl;
0148 }
0149 if (fManager->CloseFile() && fVerbose > 0) {
0150 G4cout << "Histo::Save: File is closed" << G4endl;
0151 }
0152 fManager->Clear();
0153 }
0154
0155
0156
0157 void Histo::Add1D(const G4String& id, const G4String& name, G4int nb, G4double x1, G4double x2,
0158 G4double u)
0159 {
0160 if (fVerbose > 0) {
0161 G4cout << "Histo::Add1D: New histogram will be booked: #" << id << " <" << name << " " << nb
0162 << " " << x1 << " " << x2 << " " << u << G4endl;
0163 }
0164 ++fNHisto;
0165 x1 /= u;
0166 x2 /= u;
0167 fActive.push_back(fDefaultAct);
0168 fBins.push_back(nb);
0169 fXmin.push_back(x1);
0170 fXmax.push_back(x2);
0171 fUnit.push_back(u);
0172 fIds.push_back(id);
0173 fTitles.push_back(name);
0174 fHisto.push_back(-1);
0175 }
0176
0177
0178
0179 void Histo::SetHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u)
0180 {
0181 if (i >= 0 && i < fNHisto) {
0182 if (fVerbose > 0) {
0183 G4cout << "Histo::SetHisto1D: #" << i << " " << nb << " " << x1 << " " << x2 << " " << u
0184 << G4endl;
0185 }
0186 fBins[i] = nb;
0187 fXmin[i] = x1;
0188 fXmax[i] = x2;
0189 fUnit[i] = u;
0190 fActive[i] = true;
0191 fHistoActive = true;
0192 }
0193 else {
0194 G4cout << "Histo::SetHisto1D: WARNING! wrong histogram index " << i << G4endl;
0195 }
0196 }
0197
0198
0199
0200 void Histo::Activate(G4int i, G4bool val)
0201 {
0202 if (fVerbose > 1) {
0203 G4cout << "Histo::Activate: Histogram: #" << i << " " << val << G4endl;
0204 }
0205 if (i >= 0 && i < fNHisto) {
0206 fActive[i] = val;
0207 if (val) {
0208 fHistoActive = true;
0209 }
0210 }
0211 }
0212
0213
0214
0215 void Histo::Fill(G4int i, G4double x, G4double w)
0216 {
0217 if (!fHistoActive) {
0218 return;
0219 }
0220 if (fVerbose > 1) {
0221 G4cout << "Histo::Fill: Histogram: #" << i << " at x= " << x << " weight= " << w << G4endl;
0222 }
0223 if (i >= 0 && i < fNHisto) {
0224 if (fActive[i]) {
0225 fManager->FillH1(fHisto[i], x / fUnit[i], w);
0226 }
0227 }
0228 else {
0229 G4cout << "Histo::Fill: WARNING! wrong histogram index " << i << G4endl;
0230 }
0231 }
0232
0233
0234
0235 void Histo::ScaleH1(G4int i, G4double x)
0236 {
0237 if (!fHistoActive) {
0238 return;
0239 }
0240 if (fVerbose > 0) {
0241 G4cout << "Histo::Scale: Histogram: #" << i << " by factor " << x << G4endl;
0242 }
0243 if (i >= 0 && i < fNHisto) {
0244 if (fActive[i]) {
0245 fManager->GetH1(fHisto[i])->scale(x);
0246 }
0247 }
0248 else {
0249 G4cout << "Histo::Scale: WARNING! wrong histogram index " << i << G4endl;
0250 }
0251 }
0252
0253
0254
0255 void Histo::AddTuple(const G4String& w1)
0256 {
0257 fTupleTitle = w1;
0258 }
0259
0260
0261
0262 void Histo::AddTupleI(const G4String& w1)
0263 {
0264 fNtupleActive = true;
0265 fNtupleI.push_back(w1);
0266 fTupleI.push_back(-1);
0267 }
0268
0269
0270
0271 void Histo::AddTupleF(const G4String& w1)
0272 {
0273 fNtupleActive = true;
0274 fNtupleF.push_back(w1);
0275 fTupleF.push_back(-1);
0276 }
0277
0278
0279
0280 void Histo::AddTupleD(const G4String& w1)
0281 {
0282 fNtupleActive = true;
0283 fNtupleD.push_back(w1);
0284 fTupleD.push_back(-1);
0285 }
0286
0287
0288
0289 void Histo::FillTupleI(G4int i, G4int x)
0290 {
0291 if (!fNtupleActive) {
0292 return;
0293 }
0294 G4int n = fNtupleI.size();
0295 if (i >= 0 && i < n) {
0296 if (fVerbose > 1) {
0297 G4cout << "Histo::FillTupleI: i= " << i << " id= " << fTupleI[i] << " <" << fNtupleI[i]
0298 << "> = " << x << G4endl;
0299 }
0300 fManager->FillNtupleIColumn(fTupleI[i], x);
0301 }
0302 else {
0303 G4cout << "Histo::FillTupleI: WARNING! wrong ntuple index " << i << G4endl;
0304 }
0305 }
0306
0307
0308
0309 void Histo::FillTupleF(G4int i, G4float x)
0310 {
0311 if (!fNtupleActive) {
0312 return;
0313 }
0314 G4int n = fNtupleF.size();
0315 if (i >= 0 && i < n) {
0316 if (fVerbose > 1) {
0317 G4cout << "Histo::FillTupleF: i= " << i << " id= " << fTupleF[i] << " <" << fNtupleF[i]
0318 << "> = " << x << G4endl;
0319 }
0320 fManager->FillNtupleFColumn(fTupleF[i], x);
0321 }
0322 else {
0323 G4cout << "Histo::FillTupleF: WARNING! wrong ntuple index " << i << G4endl;
0324 }
0325 }
0326
0327
0328
0329 void Histo::FillTupleD(G4int i, G4double x)
0330 {
0331 if (!fNtupleActive) {
0332 return;
0333 }
0334 G4int n = fNtupleD.size();
0335 if (i >= 0 && i < n) {
0336 if (fVerbose > 1) {
0337 G4cout << "Histo::FillTupleD: i= " << i << " id= " << fTupleD[i] << " <" << fNtupleD[i]
0338 << "> = " << x << G4endl;
0339 }
0340 fManager->FillNtupleDColumn(fTupleD[i], x);
0341 }
0342 else {
0343 G4cout << "Histo::FillTupleD: WARNING! wrong ntuple index " << i << G4endl;
0344 }
0345 }
0346
0347
0348
0349 void Histo::AddRow()
0350 {
0351 if (!fNtupleActive) {
0352 return;
0353 }
0354 fManager->AddNtupleRow();
0355 }
0356
0357
0358
0359 void Histo::SetFileName(const G4String& nam)
0360 {
0361 fHistName = nam;
0362 fHistoActive = true;
0363 }
0364
0365