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