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