Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:44

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file runAndEvent/RE03/src/RE03UserScoreWriter.cc
0027 /// \brief Implementation of the RE03UserScoreWriter class
0028 //
0029 //
0030 
0031 #include "RE03UserScoreWriter.hh"
0032 
0033 #include "G4MultiFunctionalDetector.hh"
0034 #include "G4SDParticleFilter.hh"
0035 #include "G4VPrimitiveScorer.hh"
0036 #include "G4VScoringMesh.hh"
0037 
0038 #include <fstream>
0039 #include <map>
0040 
0041 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0042 RE03UserScoreWriter::RE03UserScoreWriter() : G4VScoreWriter()
0043 {
0044   ;
0045 }
0046 
0047 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0048 RE03UserScoreWriter::~RE03UserScoreWriter()
0049 {
0050   ;
0051 }
0052 
0053 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0054 void RE03UserScoreWriter::DumpQuantityToFile(const G4String& psName, const G4String& fileName,
0055                                              const G4String& option)
0056 {
0057   using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
0058   //
0059   if (verboseLevel > 0) {
0060     G4cout << "User-defined DumpQuantityToFile() method is invoked." << G4endl;
0061     G4cout << "  -- to obtain a projection of the quantity <" << psName << "> onto the x-y plane --"
0062            << G4endl;
0063   }
0064 
0065   // change the option string into lowercase to the case-insensitive.
0066   G4String opt = option;
0067   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
0068 
0069   // confirm the option
0070   if (opt.size() == 0) opt = "csv";
0071 
0072   // open the file
0073   std::ofstream ofile(fileName);
0074   if (!ofile) {
0075     G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
0076     return;
0077   }
0078   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
0079 
0080   // retrieve the map
0081   MeshScoreMap scMap = fScoringMesh->GetScoreMap();
0082 
0083   MeshScoreMap::const_iterator msMapItr = scMap.find(psName);
0084   if (msMapItr == scMap.end()) {
0085     G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" << psName << "\"." << G4endl;
0086     return;
0087   }
0088   std::map<G4int, G4StatDouble*>* score = msMapItr->second->GetMap();
0089   ofile << "# primitive scorer name: " << msMapItr->first << G4endl;
0090 
0091   // write header info
0092   ofile << "# xy projection" << G4endl;
0093   ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << G4endl;
0094 
0095   // declare xy array
0096   std::vector<double> projy;
0097   for (int y = 0; y < fNMeshSegments[1]; y++)
0098     projy.push_back(0.);
0099   std::vector<std::vector<double>> projxy;
0100   for (int x = 0; x < fNMeshSegments[0]; x++)
0101     projxy.push_back(projy);
0102   // accumulate
0103   ofile << std::setprecision(16);  // for double value with 8 bytes
0104   for (int x = 0; x < fNMeshSegments[0]; x++) {
0105     for (int y = 0; y < fNMeshSegments[1]; y++) {
0106       for (int z = 0; z < fNMeshSegments[2]; z++) {
0107         G4int idx = GetIndex(x, y, z);
0108 
0109         std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
0110         if (value != score->end()) projxy[x][y] += value->second->sum_wx();
0111 
0112       }  // z
0113     }  // y
0114   }  // x
0115 
0116   // write quantity
0117   ofile << std::setprecision(16);  // for double value with 8 bytes
0118   for (int x = 0; x < fNMeshSegments[0]; x++) {
0119     for (int y = 0; y < fNMeshSegments[1]; y++) {
0120       ofile << x << "," << y << ",";
0121       ofile << projxy[x][y] << G4endl;
0122 
0123     }  // y
0124   }  // x
0125   ofile << std::setprecision(6);
0126 
0127   // close the file
0128   ofile.close();
0129 }