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/Hadr02/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 #include "G4SystemOfUnits.hh" 49 #include "G4XmlAnalysisManager.hh" 50 #include "G4ios.hh" 51 52 //....oooOO0OOooo........oooOO0OOooo........oo 53 //....oooOO0OOooo........oooOO0OOooo........oo 54 55 Histo::Histo() 56 { 57 fManager = 0; 58 fMessenger = new HistoMessenger(this); 59 60 fHistName = "test"; 61 fHistType = "root"; 62 fTupleName = "tuple"; 63 fTupleTitle = "test"; 64 fNHisto = 0; 65 fVerbose = 0; 66 fDefaultAct = true; 67 fHistoActive = false; 68 fNtupleActive = false; 69 } 70 71 //....oooOO0OOooo........oooOO0OOooo........oo 72 73 Histo::~Histo() 74 { 75 delete fMessenger; 76 } 77 78 //....oooOO0OOooo........oooOO0OOooo........oo 79 80 void Histo::Book() 81 { 82 if (!(fHistoActive || fNtupleActive)) { 83 return; 84 } 85 86 // Always creating analysis manager 87 if (fHistType == "root") { 88 fManager = G4RootAnalysisManager::Instance 89 } 90 else { 91 fManager = G4XmlAnalysisManager::Instance( 92 } 93 94 // Creating a tree mapped to a new hbook fil 95 G4String nam = fHistName; // + "." + fHistT 96 97 // Open file histogram file 98 if (!fManager->OpenFile(nam)) { 99 G4cout << "Histo::Book: ERROR open file <" 100 fHistoActive = false; 101 fNtupleActive = false; 102 return; 103 } 104 G4cout << "### Histo::Save: Opended file <" 105 << G4endl; 106 107 // Creating an 1-dimensional histograms in t 108 for (G4int i = 0; i < fNHisto; ++i) { 109 if (fActive[i]) { 110 G4String ss = "h" + fIds[i]; 111 fHisto[i] = fManager->CreateH1(ss, fTitl 112 if (fVerbose > 0) { 113 G4cout << "Created histogram #" << i < 114 << fTitles[i] << G4endl; 115 } 116 } 117 } 118 // Creating a tuple factory, whose tuples wi 119 if (fNtupleActive) { 120 fManager->CreateNtuple(fTupleName, fTupleT 121 G4int i; 122 G4int n = fNtupleI.size(); 123 for (i = 0; i < n; ++i) { 124 if (fTupleI[i] == -1) { 125 fTupleI[i] = fManager->CreateNtupleICo 126 } 127 } 128 n = fNtupleF.size(); 129 for (i = 0; i < n; ++i) { 130 if (fTupleF[i] == -1) { 131 fTupleF[i] = fManager->CreateNtupleFCo 132 } 133 } 134 n = fNtupleD.size(); 135 for (i = 0; i < n; ++i) { 136 if (fTupleD[i] == -1) { 137 fTupleD[i] = fManager->CreateNtupleDCo 138 } 139 } 140 } 141 } 142 143 //....oooOO0OOooo........oooOO0OOooo........oo 144 145 void Histo::Save() 146 { 147 if (!(fHistoActive || fNtupleActive)) { 148 return; 149 } 150 151 // Creating a tree mapped to a new hbook fil 152 G4String nam = fHistName + "." + fHistType; 153 154 // Write histogram file 155 if (!fManager->Write()) { 156 G4ExceptionDescription message; 157 message << "Histo::Save: FATAL ERROR writi 158 G4Exception("Histo::Save()", "Hadr02Code00 159 } 160 if (fVerbose > 0) { 161 G4cout << "### Histo::Save: Histograms and 162 } 163 if (fManager->CloseFile() && fVerbose > 0) { 164 G4cout << " File is closed 165 } 166 fManager->Clear(); 167 } 168 169 //....oooOO0OOooo........oooOO0OOooo........oo 170 171 void Histo::Add1D(const G4String& id, const G4 172 G4double u) 173 { 174 if (fVerbose > 0) { 175 G4cout << "Histo::Add1D: New histogram wil 176 << " " << x1 << " " << x2 << " " 177 } 178 ++fNHisto; 179 x1 /= u; 180 x2 /= u; 181 fActive.push_back(fDefaultAct); 182 fBins.push_back(nb); 183 fXmin.push_back(x1); 184 fXmax.push_back(x2); 185 fUnit.push_back(u); 186 fIds.push_back(id); 187 fTitles.push_back(name); 188 fHisto.push_back(-1); 189 } 190 191 //....oooOO0OOooo........oooOO0OOooo........oo 192 193 void Histo::SetHisto1D(G4int i, G4int nb, G4do 194 { 195 if (i >= 0 && i < fNHisto) { 196 if (fVerbose > 0) { 197 G4cout << "Histo::SetHisto1D: #" << i << 198 << G4endl; 199 } 200 fBins[i] = nb; 201 fXmin[i] = x1; 202 fXmax[i] = x2; 203 fUnit[i] = u; 204 fActive[i] = true; 205 fHistoActive = true; 206 } 207 else { 208 G4cout << "Histo::SetHisto1D: WARNING! wro 209 } 210 } 211 212 //....oooOO0OOooo........oooOO0OOooo........oo 213 214 void Histo::Activate(G4int i, G4bool val) 215 { 216 if (fVerbose > 1) { 217 G4cout << "Histo::Activate: Histogram: #" 218 } 219 if (i >= 0 && i < fNHisto) { 220 fActive[i] = val; 221 if (val) { 222 fHistoActive = true; 223 } 224 } 225 } 226 227 //....oooOO0OOooo........oooOO0OOooo........oo 228 229 void Histo::Fill(G4int i, G4double x, G4double 230 { 231 if (!fHistoActive) { 232 return; 233 } 234 if (fVerbose > 1) { 235 G4cout << "Histo::Fill: Histogram: #" << i 236 } 237 if (i >= 0 && i < fNHisto) { 238 if (fActive[i]) { 239 fManager->FillH1(fHisto[i], x / fUnit[i] 240 } 241 } 242 else { 243 G4cout << "Histo::Fill: WARNING! wrong his 244 } 245 } 246 247 //....oooOO0OOooo........oooOO0OOooo........oo 248 249 void Histo::ScaleH1(G4int i, G4double x) 250 { 251 if (!fHistoActive) { 252 return; 253 } 254 if (fVerbose > 0) { 255 G4cout << "Histo::Scale: Histogram: #" << 256 } 257 if (i >= 0 && i < fNHisto) { 258 if (fActive[i]) { 259 if (fHistType == "root") 260 dynamic_cast<G4RootAnalysisManager*>(f 261 if (fHistType == "xml") 262 dynamic_cast<G4XmlAnalysisManager*>(fM 263 } 264 } 265 else { 266 G4cout << "Histo::Scale: WARNING! wrong hi 267 } 268 } 269 270 //....oooOO0OOooo........oooOO0OOooo........oo 271 272 void Histo::AddTuple(const G4String& w1) 273 { 274 fTupleTitle = w1; 275 } 276 277 //....oooOO0OOooo........oooOO0OOooo........oo 278 279 void Histo::AddTupleI(const G4String& w1) 280 { 281 fNtupleActive = true; 282 fNtupleI.push_back(w1); 283 fTupleI.push_back(-1); 284 } 285 286 //....oooOO0OOooo........oooOO0OOooo........oo 287 288 void Histo::AddTupleF(const G4String& w1) 289 { 290 fNtupleActive = true; 291 fNtupleF.push_back(w1); 292 fTupleF.push_back(-1); 293 } 294 295 //....oooOO0OOooo........oooOO0OOooo........oo 296 297 void Histo::AddTupleD(const G4String& w1) 298 { 299 fNtupleActive = true; 300 fNtupleD.push_back(w1); 301 fTupleD.push_back(-1); 302 } 303 304 //....oooOO0OOooo........oooOO0OOooo........oo 305 306 void Histo::FillTupleI(G4int i, G4int x) 307 { 308 if (!fNtupleActive) { 309 return; 310 } 311 G4int n = fNtupleI.size(); 312 if (i >= 0 && i < n) { 313 if (fVerbose > 1) { 314 G4cout << "Histo::FillTupleI: i= " << i 315 << "> = " << x << G4endl; 316 } 317 fManager->FillNtupleIColumn(fTupleI[i], x) 318 } 319 else { 320 G4cout << "Histo::FillTupleI: WARNING! wro 321 } 322 } 323 324 //....oooOO0OOooo........oooOO0OOooo........oo 325 326 void Histo::FillTupleF(G4int i, G4float x) 327 { 328 if (!fNtupleActive) { 329 return; 330 } 331 G4int n = fNtupleF.size(); 332 if (i >= 0 && i < n) { 333 if (fVerbose > 1) { 334 G4cout << "Histo::FillTupleF: i= " << i 335 << "> = " << x << G4endl; 336 } 337 fManager->FillNtupleFColumn(fTupleF[i], x) 338 } 339 else { 340 G4cout << "Histo::FillTupleF: WARNING! wro 341 } 342 } 343 344 //....oooOO0OOooo........oooOO0OOooo........oo 345 346 void Histo::FillTupleD(G4int i, G4double x) 347 { 348 if (!fNtupleActive) { 349 return; 350 } 351 G4int n = fNtupleD.size(); 352 if (i >= 0 && i < n) { 353 if (fVerbose > 1) { 354 G4cout << "Histo::FillTupleD: i= " << i 355 << "> = " << x << G4endl; 356 } 357 fManager->FillNtupleDColumn(fTupleD[i], x) 358 } 359 else { 360 G4cout << "Histo::FillTupleD: WARNING! wro 361 } 362 } 363 364 //....oooOO0OOooo........oooOO0OOooo........oo 365 366 void Histo::AddRow() 367 { 368 if (!fNtupleActive) { 369 return; 370 } 371 fManager->AddNtupleRow(); 372 } 373 374 //....oooOO0OOooo........oooOO0OOooo........oo 375 376 void Histo::SetFileName(const G4String& nam) 377 { 378 fHistName = nam; 379 fHistoActive = true; 380 } 381 382 //....oooOO0OOooo........oooOO0OOooo........oo 383 384 void Histo::SetFileType(const G4String& nam) 385 { 386 if (nam == "root" || nam == "ROOT") { 387 fHistType = "root"; 388 } 389 else if (nam == "xml" || nam == "XML") { 390 fHistType = "xml"; 391 } 392 else { 393 G4cout << "Histo::SetFileType: Sorry, form 394 << " not supported. Use \"root\" or 395 } 396 } 397 398 //....oooOO0OOooo........oooOO0OOooo........oo 399