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