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