src/BDSScoreWriter.cc

00001 #ifdef USE_ROOT
00002 
00003 #include "BDSScoreWriter.hh"
00004 
00005 #include "G4MultiFunctionalDetector.hh"
00006 #include "G4SDParticleFilter.hh"
00007 #include "G4VPrimitiveScorer.hh"
00008 #include "G4VScoringMesh.hh"
00009 
00010 #include <map>
00011 #include <fstream>
00012 
00013 #ifdef G4ANALYSIS_USE
00014 #include "TH1D.h"
00015 #include "TH2D.h"
00016 #include "TH3D.h"
00017 #include "TFile.h"
00018 #include "TTree.h"
00019 #endif
00020 
00021 
00022 BDSScoreWriter::BDSScoreWriter()
00023         : G4VScoreWriter() {
00024                 ;
00025 }
00026 
00027 BDSScoreWriter::~BDSScoreWriter() {
00028         ;
00029 }
00030 
00031 void BDSScoreWriter::DumpQuantityToFile(const G4String & psName, G4String & fileName, const G4String & option) {
00032   time_t rawtime;
00033   struct tm * timeinfo;
00034   time ( &rawtime );
00035   timeinfo = localtime ( &rawtime );
00036   
00037   strftime (fStartTime,50,"%Y-%m-%d-%H-%M-%S",timeinfo);
00038   std::ostringstream s;
00039   s << fFireAngle;
00040   fileName = fMaterial + "_" + s.str() + "_score_"+ fStartTime + ".root";
00041   
00042   TFile* rootFile = new TFile(fileName,"RECREATE");
00043   if(!rootFile) {
00044     G4cout << " HistoManager::book :" 
00045            << " problem creating the ROOT TFile "
00046            << G4endl;
00047     return;
00048   }
00049   DumpQuantityToFile(psName, rootFile, option);
00050   return;
00051 }
00052 
00053 
00054 
00055 
00056 void BDSScoreWriter::DumpQuantityToFile(const G4String & psName, TFile* tFile, const G4String & option) {
00057 
00058         //
00059   if(verboseLevel > 0) {
00060     G4cout << "User-defined DumpQuantityToFile() method is invoked."
00061            << G4endl;
00062     G4cout << "  -- to obtain a projection of the quantity <"
00063            << psName
00064            << "> onto the x-y plane --" << G4endl;
00065   }
00066   
00067   // change the option string into lowercase to the case-insensitive.
00068   G4String opt = option;
00069   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
00070   
00071   // confirm the option
00072   if(opt.size() == 0) opt = "";
00073   TH2D* histo2D[4]; 
00074   
00075   // retrieve the map
00076   MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
00077   
00078   
00079   MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
00080   if(msMapItr == fSMap.end()) {
00081     G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
00082            << psName << "\"." << G4endl;
00083     return;
00084   }
00085   std::map<G4int, G4double*> * score = msMapItr->second->GetMap();
00086   
00087   G4RotationMatrix nullRotationMatrix;
00088   
00089   if(fScoringMesh->GetRotationMatrix() != nullRotationMatrix){
00090     G4cerr << "BDSScoreWriter::DumpQuantityToFile : Error - cannot handle rotation of scaring mesh. Exiting." << G4endl;
00091     exit(0);
00092   }
00093   
00094   G4double xMin=fScoringMesh->GetTranslation().x()-fScoringMesh->GetSize().x();
00095   G4double xMax=fScoringMesh->GetTranslation().x()+fScoringMesh->GetSize().x();
00096 
00097   G4double yMin=fScoringMesh->GetTranslation().y()-fScoringMesh->GetSize().y();
00098   G4double yMax=fScoringMesh->GetTranslation().y()+fScoringMesh->GetSize().y();
00099 
00100   G4double zMin=fScoringMesh->GetTranslation().z()-fScoringMesh->GetSize().z();
00101   G4double zMax=fScoringMesh->GetTranslation().z()+fScoringMesh->GetSize().z();
00102 
00103   histo2D[1] = new TH2D("projection_xy",msMapItr->first, fNMeshSegments[0], xMin, xMax, fNMeshSegments[1], yMin, yMax);
00104   if (!histo2D[1]) G4cout << "\n can't create histo2D" << G4endl;
00105   for(int x = 0; x < fNMeshSegments[0]; x++) {
00106     for(int y = 0; y < fNMeshSegments[1]; y++) {
00107       for(int z = 0; z < fNMeshSegments[2]; z++) {
00108         G4int idx = GetIndex(x, y, z);
00109         std::map<G4int, G4double*>::iterator value = score->find(idx);
00110         if(value != score->end()) 
00111           {
00112             double val = histo2D[1]->GetBinContent(x+1,y+1);
00113             val += (*(value->second));
00114             histo2D[1]->SetBinContent(x+1,y+1,val);
00115           }
00116       }
00117     } 
00118   } 
00119   
00120   histo2D[2] = new TH2D("projection_zy",msMapItr->first, fNMeshSegments[2], zMin, zMax, fNMeshSegments[1], yMin, yMax);
00121   if (!histo2D[2]) G4cout << "\n can't create histo2D" << G4endl;
00122   for(int z = 0; z < fNMeshSegments[2]; z++) {
00123     for(int y = 0; y < fNMeshSegments[1]; y++) {
00124       for(int x = 0; x < fNMeshSegments[0]; x++) {
00125         G4int idx = GetIndex(x, y, z);
00126         std::map<G4int, G4double*>::iterator value = score->find(idx);
00127         if(value != score->end()) 
00128           {
00129             double val = histo2D[2]->GetBinContent(z+1,y+1);
00130             val += (*(value->second));
00131             histo2D[2]->SetBinContent(z+1,y+1,val);
00132           }
00133       }
00134     } 
00135   } 
00136   
00137   histo2D[3] = new TH2D("projection_zx",msMapItr->first, fNMeshSegments[2], zMin, zMax, fNMeshSegments[0], xMin, xMax);
00138   if (!histo2D[3]) G4cout << "\n can't create histo2D" << G4endl;
00139   for(int z = 0; z < fNMeshSegments[2]; z++) {
00140     for(int x = 0; x < fNMeshSegments[0]; x++) {
00141       for(int y = 0; y < fNMeshSegments[1]; y++) {
00142         G4int idx = GetIndex(x, y, z);
00143         std::map<G4int, G4double*>::iterator value = score->find(idx);
00144         if(value != score->end()) 
00145           {
00146             double val = histo2D[3]->GetBinContent(z+1,x+1);
00147             val += (*(value->second));
00148             histo2D[3]->SetBinContent(z+1,x+1,val);
00149           }
00150       }
00151     } 
00152   } 
00153 
00154 
00155   TH3D* histo3D  = new TH3D("projection_xyz",msMapItr->first, fNMeshSegments[0], xMin, xMax, fNMeshSegments[1], yMin, yMax, fNMeshSegments[2], zMin, zMax);
00156   if (!histo3D) G4cout << "\n can't create histo3D" << G4endl;
00157   for(int x = 0; x < fNMeshSegments[0]; x++) {
00158     for(int y = 0; y < fNMeshSegments[1]; y++) {
00159       for(int z = 0; z < fNMeshSegments[2]; z++) {
00160         G4int idx = GetIndex(x, y, z);
00161         std::map<G4int, G4double*>::iterator value = score->find(idx);
00162         if(value != score->end()) 
00163           {
00164             double val = histo3D->GetBinContent(x+1,y+1,z+1);
00165             val += (*(value->second));
00166             histo3D->SetBinContent(x+1,y+1,z+1,val);
00167           }
00168       }
00169     } 
00170   } 
00171   
00172   if (tFile) {
00173     tFile->Write();       // Writing the histograms to the file
00174     tFile->Close();        // and closing the tree (and the file)
00175     G4cout << "\n----> Score File is saved \n" << G4endl;
00176   }
00177 }
00178 
00179 #endif // USE_ROOT

Generated on 27 Aug 2013 for BDSIM by  doxygen 1.4.7