Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 /// \file electromagnetic/TestEm17/src/HistoManager.cc 27 /// \brief Implementation of the HistoManager class 28 // 29 // 30 // 31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 33 34 #include "HistoManager.hh" 35 36 #include "HistoMessenger.hh" 37 38 #include "G4PhysicalConstants.hh" 39 #include "G4SystemOfUnits.hh" 40 #include "G4UnitsTable.hh" 41 42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 43 44 HistoManager::HistoManager() : fHistoMessenger(0) 45 { 46 fileName[0] = "testem17"; 47 factoryOn = false; 48 fNbHist = 0; 49 50 // histograms 51 for (G4int k = 0; k < kMaxHisto; k++) { 52 fHistId[k] = 0; 53 fHistPt[k] = 0; 54 fExist[k] = false; 55 fUnit[k] = 1.0; 56 fWidth[k] = 1.0; 57 fAscii[k] = false; 58 } 59 60 fHistoMessenger = new HistoMessenger(this); 61 } 62 63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 64 65 HistoManager::~HistoManager() 66 { 67 delete fHistoMessenger; 68 } 69 70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 71 72 void HistoManager::Book() 73 { 74 // if no histos, do nothing 75 if (fNbHist == 0) return; 76 77 // Create or get analysis manager 78 // The choice of analysis technology is done via selection of a namespace 79 // in HistoManager.hh 80 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 81 analysisManager->SetDefaultFileType("root"); 82 analysisManager->SetVerboseLevel(0); 83 G4String extension = analysisManager->GetFileType(); 84 fileName[1] = fileName[0] + "." + extension; 85 86 // Open an output file 87 // 88 G4bool fileOpen = analysisManager->OpenFile(fileName[0]); 89 if (!fileOpen) { 90 G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1] << G4endl; 91 return; 92 } 93 94 // create selected histograms 95 // 96 analysisManager->SetFirstHistoId(1); 97 98 for (G4int k = 0; k < kMaxHisto; k++) { 99 if (fExist[k]) { 100 fHistId[k] = analysisManager->CreateH1(fLabel[k], fTitle[k], fNbins[k], fVmin[k], fVmax[k]); 101 fHistPt[k] = analysisManager->GetH1(fHistId[k]); 102 factoryOn = true; 103 } 104 } 105 106 if (factoryOn) G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl; 107 } 108 109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 110 111 void HistoManager::Save() 112 { 113 if (factoryOn) { 114 G4cout << "\n----> HistoManager::save " << G4endl; 115 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 116 analysisManager->Write(); 117 analysisManager->CloseFile(); 118 SaveAscii(); // Write fAscii file, if any 119 G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl; 120 121 analysisManager->Clear(); 122 factoryOn = false; 123 } 124 } 125 126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 127 128 void HistoManager::FillHisto(G4int ih, G4double e, G4double weight) 129 { 130 if (ih > kMaxHisto) { 131 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih 132 << "does not fExist; e= " << e << " w= " << weight << G4endl; 133 return; 134 } 135 136 if (fHistPt[ih]) fHistPt[ih]->fill(e / fUnit[ih], weight); 137 } 138 139 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 140 141 void HistoManager::SetHisto(G4int ih, G4int nbins, G4double vmin, G4double vmax, 142 const G4String& unit) 143 { 144 if (ih > kMaxHisto) { 145 G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih << "does not fExist" 146 << G4endl; 147 return; 148 } 149 150 const G4String id[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 151 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 152 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"}; 153 const G4String title[] = { 154 "dummy", // 0 155 "log10(Eloss/Emu) muIonization", // 1 156 "log10(Eloss/Emu) muPair", // 2 157 "log10(Eloss/Emu) muBrems", // 3 158 "log10(Eloss/Emu) muNuclear", // 4 159 "log10(Eloss/Emu) hIonization", // 5 160 "log10(Eloss/Emu) hPair", // 6 161 "log10(Eloss/Emu) hBrems", // 7 162 "log10(Eloss/Emu) muToMuonPair", // 8 163 "dummy", // 9 164 "dummy", // 10 165 "log10(Eloss/Emu) muIonization", // 11 166 "log10(Eloss/Emu) muPair", // 12 167 "log10(Eloss/Emu) muBrems", // 13 168 "log10(Eloss/Emu) muNuclear", // 14 169 "dummy", // 15 170 "dummy", // 16 171 "dummy", // 17 172 "log10(Eloss/Emu) muToMuonPair", // 18 173 "dummy", // 19 174 "dummy", // 20 175 "CS(1/mm) vs ekin muIonisation", // 21 176 "CS(1/mm) vs ekin muPair", // 22 177 "CS(1/mm) vs ekin muBrems", // 23 178 "CS(1/mm) vs ekin muNuclear", // 24 179 "dummy", // 25 180 "dummy", // 26 181 "dummy", // 27 182 "CS(1/mm) vs ekin muToMuonPair", // 28 183 "dummy" // 29 184 }; 185 186 G4String titl = title[ih]; 187 fUnit[ih] = 1.; 188 189 if (unit != "none") { 190 titl = title[ih] + " (" + unit + ")"; 191 fUnit[ih] = G4UnitDefinition::GetValueOf(unit); 192 } 193 194 fExist[ih] = true; 195 fLabel[ih] = "h" + id[ih]; 196 fTitle[ih] = titl; 197 fNbins[ih] = nbins; 198 fVmin[ih] = vmin; 199 fVmax[ih] = vmax; 200 fWidth[ih] = fUnit[ih] * (vmax - vmin) / nbins; 201 202 fNbHist++; 203 204 G4cout << "----> SetHisto " << ih << ": " << titl << "; " << nbins << " bins from " << vmin 205 << " " << unit << " to " << vmax << " " << unit << G4endl; 206 } 207 208 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 209 210 void HistoManager::Normalize(G4int ih, G4double fac) 211 { 212 if (ih >= kMaxHisto) { 213 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih << " fac= " << fac 214 << G4endl; 215 return; 216 } 217 218 if (fHistPt[ih]) fHistPt[ih]->scale(fac); 219 } 220 221 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 222 223 void HistoManager::PrintHisto(G4int ih) 224 { 225 if (ih < kMaxHisto) { 226 fAscii[ih] = true; 227 fAscii[0] = true; 228 } 229 else 230 G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih << "does not exist" 231 << G4endl; 232 } 233 234 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 235 236 #include <fstream> 237 238 void HistoManager::SaveAscii() 239 { 240 if (!fAscii[0]) return; 241 242 G4String name = fileName[0] + ".ascii"; 243 std::ofstream File(name, std::ios::out); 244 if (!File) { 245 G4cout << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl; 246 return; 247 } 248 249 File.setf(std::ios::scientific, std::ios::floatfield); 250 251 // write selected histograms 252 for (G4int ih = 0; ih < kMaxHisto; ih++) { 253 if (fHistPt[ih] && fAscii[ih]) { 254 File << "\n 1D histogram " << ih << ": " << fTitle[ih] << "\n \n \t X \t\t Y" 255 << G4endl; 256 257 for (G4int iBin = 0; iBin < fNbins[ih]; iBin++) { 258 File << " " << iBin << "\t" << fHistPt[ih]->axis().bin_center(iBin) << "\t" 259 << fHistPt[ih]->bin_height(iBin) << G4endl; 260 } 261 } 262 } 263 } 264 265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 266