Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-30 07:51:14

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 RE03UserScoreWriter.cc
0027 /// \brief Implementation of the RE03UserScoreWriter class
0028 
0029 #include "RE03UserScoreWriter.hh"
0030 
0031 #include "G4MultiFunctionalDetector.hh"
0032 #include "G4SDParticleFilter.hh"
0033 #include "G4VPrimitiveScorer.hh"
0034 #include "G4VScoringMesh.hh"
0035 
0036 #include <fstream>
0037 #include <map>
0038 
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0040 RE03UserScoreWriter::RE03UserScoreWriter() : G4VScoreWriter()
0041 {
0042   ;
0043 }
0044 
0045 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0046 RE03UserScoreWriter::~RE03UserScoreWriter()
0047 {
0048   ;
0049 }
0050 
0051 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0052 void RE03UserScoreWriter::DumpQuantityToFile(const G4String& psName, const G4String& fileName,
0053                                              const G4String& option)
0054 {
0055   using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
0056   //
0057   if (verboseLevel > 0) {
0058     G4cout << "User-defined DumpQuantityToFile() method is invoked." << G4endl;
0059     G4cout << "  -- to obtain a projection of the quantity <" << psName << "> onto the x-y plane --"
0060            << G4endl;
0061   }
0062 
0063   // change the option string into lowercase to the case-insensitive.
0064   G4String opt = option;
0065   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
0066 
0067   // confirm the option
0068   if (opt.size() == 0) opt = "csv";
0069 
0070   // open the file
0071   std::ofstream ofile(fileName);
0072   if (!ofile) {
0073     G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
0074     return;
0075   }
0076   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
0077 
0078   // retrieve the map
0079   MeshScoreMap scMap = fScoringMesh->GetScoreMap();
0080 
0081   MeshScoreMap::const_iterator msMapItr = scMap.find(psName);
0082   if (msMapItr == scMap.end()) {
0083     G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" << psName << "\"." << G4endl;
0084     return;
0085   }
0086   std::map<G4int, G4StatDouble*>* score = msMapItr->second->GetMap();
0087   ofile << "# primitive scorer name: " << msMapItr->first << G4endl;
0088 
0089   // write header info
0090   ofile << "# xy projection" << G4endl;
0091   ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << G4endl;
0092 
0093   // declare xy array
0094   std::vector<double> projy;
0095   for (int y = 0; y < fNMeshSegments[1]; y++)
0096     projy.push_back(0.);
0097   std::vector<std::vector<double>> projxy;
0098   for (int x = 0; x < fNMeshSegments[0]; x++)
0099     projxy.push_back(projy);
0100   // accumulate
0101   ofile << std::setprecision(16);  // for double value with 8 bytes
0102   for (int x = 0; x < fNMeshSegments[0]; x++) {
0103     for (int y = 0; y < fNMeshSegments[1]; y++) {
0104       for (int z = 0; z < fNMeshSegments[2]; z++) {
0105         G4int idx = GetIndex(x, y, z);
0106 
0107         std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
0108         if (value != score->end()) projxy[x][y] += value->second->sum_wx();
0109 
0110       }  // z
0111     }  // y
0112   }  // x
0113 
0114   // write quantity
0115   ofile << std::setprecision(16);  // for double value with 8 bytes
0116   for (int x = 0; x < fNMeshSegments[0]; x++) {
0117     for (int y = 0; y < fNMeshSegments[1]; y++) {
0118       ofile << x << "," << y << ",";
0119       ofile << projxy[x][y] << G4endl;
0120 
0121     }  // y
0122   }  // x
0123   ofile << std::setprecision(6);
0124 
0125   // close the file
0126   ofile.close();
0127 }