Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 /// \file hadronic/Hadr01/src/Histo.cc 27 /// \brief Implementation of the Histo class 28 // 29 // 30 //-------------------------------------------- 31 // 32 // ClassName: Histo - Generic histogram/ntup 33 // 34 // 35 // Author: V.Ivanchenko 30.10.03 36 // 37 //-------------------------------------------- 38 // 39 40 //....oooOO0OOooo........oooOO0OOooo........oo 41 //....oooOO0OOooo........oooOO0OOooo........oo 42 43 #include "Histo.hh" 44 45 #include "HistoMessenger.hh" 46 47 #include "G4RootAnalysisManager.hh" 48 49 //....oooOO0OOooo........oooOO0OOooo........oo 50 //....oooOO0OOooo........oooOO0OOooo........oo 51 52 Histo::Histo() 53 : fManager(nullptr), 54 fHistName("test"), 55 fTupleName("tuple"), 56 fTupleTitle("test"), 57 fNHisto(0), 58 fVerbose(0), 59 fDefaultAct(true), 60 fHistoActive(false), 61 fNtupleActive(false) 62 { 63 fMessenger = new HistoMessenger(this); 64 } 65 66 //....oooOO0OOooo........oooOO0OOooo........oo 67 68 Histo::~Histo() 69 { 70 delete fMessenger; 71 } 72 73 //....oooOO0OOooo........oooOO0OOooo........oo 74 75 void Histo::Book() 76 { 77 if (!(fHistoActive || fNtupleActive)) { 78 return; 79 } 80 81 // Always creating analysis manager 82 fManager = G4RootAnalysisManager::Instance() 83 84 // Creating a tree mapped to a new hbook fil 85 G4String nam = fHistName + ".root"; 86 87 // Open file histogram file 88 if (!fManager->OpenFile(nam)) { 89 G4cout << "Histo::Book: ERROR open file <" 90 fHistoActive = false; 91 fNtupleActive = false; 92 return; 93 } 94 G4cout << "### Histo::Save: Opended file <" 95 << G4endl; 96 97 // Creating an 1-dimensional histograms in t 98 for (G4int i = 0; i < fNHisto; ++i) { 99 if (fActive[i]) { 100 G4String ss = "h" + fIds[i]; 101 fHisto[i] = fManager->CreateH1(ss, fTitl 102 if (fVerbose > 0) { 103 G4cout << "Created histogram #" << i < 104 << fTitles[i] << G4endl; 105 } 106 } 107 } 108 // Creating a tuple factory, whose tuples wi 109 if (fNtupleActive) { 110 fManager->CreateNtuple(fTupleName, fTupleT 111 G4int i; 112 G4int n = fNtupleI.size(); 113 for (i = 0; i < n; ++i) { 114 if (fTupleI[i] == -1) { 115 fTupleI[i] = fManager->CreateNtupleICo 116 } 117 } 118 n = fNtupleF.size(); 119 for (i = 0; i < n; ++i) { 120 if (fTupleF[i] == -1) { 121 fTupleF[i] = fManager->CreateNtupleFCo 122 } 123 } 124 n = fNtupleD.size(); 125 for (i = 0; i < n; ++i) { 126 if (fTupleD[i] == -1) { 127 fTupleD[i] = fManager->CreateNtupleDCo 128 } 129 } 130 } 131 } 132 133 //....oooOO0OOooo........oooOO0OOooo........oo 134 135 void Histo::Save() 136 { 137 if (!(fHistoActive || fNtupleActive)) { 138 return; 139 } 140 141 // Write histogram file 142 if (!fManager->Write()) { 143 G4cout << "Histo::Save: FATAL ERROR writin 144 exit(1); 145 } 146 if (fVerbose > 0) { 147 G4cout << "### Histo::Save: Histograms and 148 } 149 if (fManager->CloseFile() && fVerbose > 0) { 150 G4cout << "Histo::Save: File is closed" << 151 } 152 fManager->Clear(); 153 } 154 155 //....oooOO0OOooo........oooOO0OOooo........oo 156 157 void Histo::Add1D(const G4String& id, const G4 158 G4double u) 159 { 160 if (fVerbose > 0) { 161 G4cout << "Histo::Add1D: New histogram wil 162 << " " << x1 << " " << x2 << " " 163 } 164 ++fNHisto; 165 x1 /= u; 166 x2 /= u; 167 fActive.push_back(fDefaultAct); 168 fBins.push_back(nb); 169 fXmin.push_back(x1); 170 fXmax.push_back(x2); 171 fUnit.push_back(u); 172 fIds.push_back(id); 173 fTitles.push_back(name); 174 fHisto.push_back(-1); 175 } 176 177 //....oooOO0OOooo........oooOO0OOooo........oo 178 179 void Histo::SetHisto1D(G4int i, G4int nb, G4do 180 { 181 if (i >= 0 && i < fNHisto) { 182 if (fVerbose > 0) { 183 G4cout << "Histo::SetHisto1D: #" << i << 184 << G4endl; 185 } 186 fBins[i] = nb; 187 fXmin[i] = x1; 188 fXmax[i] = x2; 189 fUnit[i] = u; 190 fActive[i] = true; 191 fHistoActive = true; 192 } 193 else { 194 G4cout << "Histo::SetHisto1D: WARNING! wro 195 } 196 } 197 198 //....oooOO0OOooo........oooOO0OOooo........oo 199 200 void Histo::Activate(G4int i, G4bool val) 201 { 202 if (fVerbose > 1) { 203 G4cout << "Histo::Activate: Histogram: #" 204 } 205 if (i >= 0 && i < fNHisto) { 206 fActive[i] = val; 207 if (val) { 208 fHistoActive = true; 209 } 210 } 211 } 212 213 //....oooOO0OOooo........oooOO0OOooo........oo 214 215 void Histo::Fill(G4int i, G4double x, G4double 216 { 217 if (!fHistoActive) { 218 return; 219 } 220 if (fVerbose > 1) { 221 G4cout << "Histo::Fill: Histogram: #" << i 222 } 223 if (i >= 0 && i < fNHisto) { 224 if (fActive[i]) { 225 fManager->FillH1(fHisto[i], x / fUnit[i] 226 } 227 } 228 else { 229 G4cout << "Histo::Fill: WARNING! wrong his 230 } 231 } 232 233 //....oooOO0OOooo........oooOO0OOooo........oo 234 235 void Histo::ScaleH1(G4int i, G4double x) 236 { 237 if (!fHistoActive) { 238 return; 239 } 240 if (fVerbose > 0) { 241 G4cout << "Histo::Scale: Histogram: #" << 242 } 243 if (i >= 0 && i < fNHisto) { 244 if (fActive[i]) { 245 fManager->GetH1(fHisto[i])->scale(x); 246 } 247 } 248 else { 249 G4cout << "Histo::Scale: WARNING! wrong hi 250 } 251 } 252 253 //....oooOO0OOooo........oooOO0OOooo........oo 254 255 void Histo::AddTuple(const G4String& w1) 256 { 257 fTupleTitle = w1; 258 } 259 260 //....oooOO0OOooo........oooOO0OOooo........oo 261 262 void Histo::AddTupleI(const G4String& w1) 263 { 264 fNtupleActive = true; 265 fNtupleI.push_back(w1); 266 fTupleI.push_back(-1); 267 } 268 269 //....oooOO0OOooo........oooOO0OOooo........oo 270 271 void Histo::AddTupleF(const G4String& w1) 272 { 273 fNtupleActive = true; 274 fNtupleF.push_back(w1); 275 fTupleF.push_back(-1); 276 } 277 278 //....oooOO0OOooo........oooOO0OOooo........oo 279 280 void Histo::AddTupleD(const G4String& w1) 281 { 282 fNtupleActive = true; 283 fNtupleD.push_back(w1); 284 fTupleD.push_back(-1); 285 } 286 287 //....oooOO0OOooo........oooOO0OOooo........oo 288 289 void Histo::FillTupleI(G4int i, G4int x) 290 { 291 if (!fNtupleActive) { 292 return; 293 } 294 G4int n = fNtupleI.size(); 295 if (i >= 0 && i < n) { 296 if (fVerbose > 1) { 297 G4cout << "Histo::FillTupleI: i= " << i 298 << "> = " << x << G4endl; 299 } 300 fManager->FillNtupleIColumn(fTupleI[i], x) 301 } 302 else { 303 G4cout << "Histo::FillTupleI: WARNING! wro 304 } 305 } 306 307 //....oooOO0OOooo........oooOO0OOooo........oo 308 309 void Histo::FillTupleF(G4int i, G4float x) 310 { 311 if (!fNtupleActive) { 312 return; 313 } 314 G4int n = fNtupleF.size(); 315 if (i >= 0 && i < n) { 316 if (fVerbose > 1) { 317 G4cout << "Histo::FillTupleF: i= " << i 318 << "> = " << x << G4endl; 319 } 320 fManager->FillNtupleFColumn(fTupleF[i], x) 321 } 322 else { 323 G4cout << "Histo::FillTupleF: WARNING! wro 324 } 325 } 326 327 //....oooOO0OOooo........oooOO0OOooo........oo 328 329 void Histo::FillTupleD(G4int i, G4double x) 330 { 331 if (!fNtupleActive) { 332 return; 333 } 334 G4int n = fNtupleD.size(); 335 if (i >= 0 && i < n) { 336 if (fVerbose > 1) { 337 G4cout << "Histo::FillTupleD: i= " << i 338 << "> = " << x << G4endl; 339 } 340 fManager->FillNtupleDColumn(fTupleD[i], x) 341 } 342 else { 343 G4cout << "Histo::FillTupleD: WARNING! wro 344 } 345 } 346 347 //....oooOO0OOooo........oooOO0OOooo........oo 348 349 void Histo::AddRow() 350 { 351 if (!fNtupleActive) { 352 return; 353 } 354 fManager->AddNtupleRow(); 355 } 356 357 //....oooOO0OOooo........oooOO0OOooo........oo 358 359 void Histo::SetFileName(const G4String& nam) 360 { 361 fHistName = nam; 362 fHistoActive = true; 363 } 364 365 //....oooOO0OOooo........oooOO0OOooo........oo 366