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