G4VScoreWriter.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 
00030 #include "G4VScoreWriter.hh"
00031 
00032 #include "G4MultiFunctionalDetector.hh"
00033 #include "G4SDParticleFilter.hh"
00034 #include "G4VPrimitiveScorer.hh"
00035 #include "G4VScoringMesh.hh"
00036 
00037 #include <map>
00038 #include <fstream>
00039 
00040 G4VScoreWriter::G4VScoreWriter()
00041   : fScoringMesh(0), verboseLevel(0) {
00042   fNMeshSegments[0] = fNMeshSegments[1] = fNMeshSegments[2] = 0;
00043 }
00044 
00045 G4VScoreWriter::~G4VScoreWriter() {
00046 }
00047 
00048 void G4VScoreWriter::SetScoringMesh(G4VScoringMesh * sm) {
00049     fScoringMesh = sm;
00050     fScoringMesh->GetNumberOfSegments(fNMeshSegments);
00051 }
00052 
00053 void G4VScoreWriter::DumpQuantityToFile(const G4String& psName,
00054                                         const G4String& fileName,
00055                                         const G4String& option) {
00056 
00057   // change the option string into lowercase to the case-insensitive.
00058   G4String opt = option;
00059   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
00060 
00061   // confirm the option
00062   if(opt.size() == 0) opt = "csv";
00063   if(opt.find("csv") == std::string::npos &&
00064      opt.find("sequence") == std::string::npos) {
00065     G4cerr << "ERROR : DumpToFile : Unknown option -> "
00066            << option << G4endl;
00067     return;
00068   }
00069 
00070   // open the file
00071   std::ofstream ofile(fileName);
00072   if(!ofile) {
00073     G4cerr << "ERROR : DumpToFile : File open error -> "
00074            << fileName << G4endl;
00075     return;
00076   }
00077   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
00078 
00079   
00080   // retrieve the map
00081   MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
00082   
00083 
00084   MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
00085   if(msMapItr == fSMap.end()) {
00086     G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
00087            << psName << "\"." << G4endl;
00088     return;
00089   }
00090 
00091 
00092   std::map<G4int, G4double*> * score = msMapItr->second->GetMap();
00093   ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
00094 
00095 
00096   G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
00097   G4String unit = fScoringMesh->GetPSUnit(psName);
00098   G4String divisionAxisNames[3];
00099   fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
00100   // index order
00101   ofile << "# i" << divisionAxisNames[0]
00102         << ", i" << divisionAxisNames[1]
00103         << ", i" << divisionAxisNames[2];
00104   // unit of scored value
00105   ofile << ", value ";
00106   if(unit.size() > 0) ofile << "[" << unit << "]";
00107   ofile << G4endl;
00108 
00109   // "sequence" option: write header info 
00110   if(opt.find("sequence") != std::string::npos) {
00111     ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
00112           << G4endl;
00113   }
00114 
00115   // write quantity
00116   long count = 0;
00117   ofile << std::setprecision(16); // for double value with 8 bytes
00118   for(int x = 0; x < fNMeshSegments[0]; x++) {
00119     for(int y = 0; y < fNMeshSegments[1]; y++) {
00120       for(int z = 0; z < fNMeshSegments[2]; z++) {
00121         G4int idx = GetIndex(x, y, z);
00122         
00123         if(opt.find("csv") != std::string::npos)
00124           ofile << x << "," << y << "," << z << ",";
00125 
00126         std::map<G4int, G4double*>::iterator value = score->find(idx);
00127         if(value == score->end()) {
00128           ofile << 0.;
00129         } else {
00130           ofile << *(value->second)/unitValue;
00131         }
00132 
00133         if(opt.find("csv") != std::string::npos) {
00134           ofile << G4endl;
00135         } else if(opt.find("sequence") != std::string::npos) {
00136           ofile << " ";
00137           if(count++%5 == 4) ofile << G4endl;
00138         }
00139 
00140       } // z
00141     } // y
00142   } // x
00143   ofile << std::setprecision(6);
00144 
00145   // close the file
00146   ofile.close();
00147   
00148 }
00149 
00150 void G4VScoreWriter::DumpAllQuantitiesToFile(const G4String& fileName,
00151                                              const G4String& option) {
00152 
00153   // change the option string into lowercase to the case-insensitive.
00154   G4String opt = option;
00155   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
00156 
00157   // confirm the option
00158   if(opt.size() == 0) opt = "csv";
00159   if(opt.find("csv") == std::string::npos &&
00160      opt.find("sequence") == std::string::npos) {
00161     G4cerr << "ERROR : DumpToFile : Unknown option -> "
00162            << option << G4endl;
00163     return;
00164   }
00165 
00166   // open the file
00167   std::ofstream ofile(fileName);
00168   if(!ofile) {
00169     G4cerr << "ERROR : DumpToFile : File open error -> "
00170            << fileName << G4endl;
00171     return;
00172   }
00173   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
00174 
00175   // retrieve the map
00176   MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
00177   MeshScoreMap::const_iterator msMapItr = fSMap.begin();
00178   std::map<G4int, G4double*> * score;
00179   for(; msMapItr != fSMap.end(); msMapItr++) {
00180 
00181     G4String psname = msMapItr->first;
00182 
00183     score = msMapItr->second->GetMap();
00184     ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
00185 
00186     G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
00187     G4String unit = fScoringMesh->GetPSUnit(psname);
00188     G4String divisionAxisNames[3];
00189     fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
00190     // index order
00191     ofile << "# i" << divisionAxisNames[0]
00192           << ", i" << divisionAxisNames[1]
00193           << ", i" << divisionAxisNames[2];
00194     // unit of scored value
00195     ofile << ", value ";
00196     if(unit.size() > 0) ofile << "[" << unit << "]";
00197     ofile << G4endl;
00198 
00199 
00200     // "sequence" option: write header info 
00201     if(opt.find("sequence") != std::string::npos) {
00202       ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
00203             << G4endl;
00204     }
00205 
00206     // write quantity
00207     long count = 0;
00208     ofile << std::setprecision(16); // for double value with 8 bytes
00209     for(int x = 0; x < fNMeshSegments[0]; x++) {
00210       for(int y = 0; y < fNMeshSegments[1]; y++) {
00211         for(int z = 0; z < fNMeshSegments[2]; z++) {
00212           G4int idx = GetIndex(x, y, z);
00213           
00214           if(opt.find("csv") != std::string::npos)
00215             ofile << x << "," << y << "," << z << ",";
00216           
00217           std::map<G4int, G4double*>::iterator value = score->find(idx);
00218           if(value == score->end()) {
00219             ofile << 0.;
00220           } else {
00221             ofile << *(value->second)/unitValue;
00222           }
00223 
00224           if(opt.find("csv") != std::string::npos) {
00225             ofile << G4endl;
00226           } else if(opt.find("sequence") != std::string::npos) {
00227             ofile << " ";
00228             if(count++%5 == 4) ofile << G4endl;
00229           }
00230 
00231         } // z
00232       } // y
00233     } // x
00234     ofile << std::setprecision(6);
00235 
00236   } // for(; msMapItr ....)
00237 
00238   // close the file
00239   ofile.close();
00240   
00241 }
00242 
00243 G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const {
00244     //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1];
00245     return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z;
00246 }
00247 

Generated on Mon May 27 17:50:22 2013 for Geant4 by  doxygen 1.4.7