source: trunk/source/digits_hits/utils/test/test1/src/Tst1UserScoreWriter.cc

Last change on this file was 1316, checked in by garnier, 14 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 4.2 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27
28#include "Tst1UserScoreWriter.hh"
29
30#include "G4MultiFunctionalDetector.hh"
31#include "G4SDParticleFilter.hh"
32#include "G4VPrimitiveScorer.hh"
33#include "G4VScoringMesh.hh"
34
35#include <map>
36#include <fstream>
37
38Tst1UserScoreWriter::Tst1UserScoreWriter()
39  : G4VScoreWriter() {
40  ;
41}
42
43Tst1UserScoreWriter::~Tst1UserScoreWriter() {
44  ;
45}
46
47void Tst1UserScoreWriter::DumpQuantityToFile(G4String & psName, G4String & fileName, G4String & option) {
48
49  //
50  G4cout << "User-defined DumpQuantityToFile() method." << G4endl;
51  G4cout << "  -- to obtain a projection of the quantity onto the x-y plane --" << G4endl;
52
53  // change the option string into lowercase to the case-insensitive.
54  G4String opt = option;
55  std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
56
57  // confirm the option
58  if(opt.size() == 0) opt = "csv";
59
60  // open the file
61  std::ofstream ofile(fileName);
62  if(!ofile) {
63    G4cerr << "ERROR : DumpToFile : File open error -> "
64           << fileName << G4endl;
65    return;
66  }
67  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
68
69 
70  // retrieve the map
71  MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
72 
73
74  MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
75  if(msMapItr == fSMap.end()) {
76    G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
77           << psName << "\"." << G4endl;
78    return;
79  }
80  std::map<G4int, G4double*> * score = msMapItr->second->GetMap();
81  ofile << "# primitive scorer name: " << msMapItr->first << G4endl;
82
83  // write header info
84  ofile << "# xy projection" << G4endl;
85  ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << G4endl;
86
87  // declare xy array
88  std::vector<double> projy;
89  for(int y = 0; y < fNMeshSegments[1]; y++) projy.push_back(0.);
90  std::vector<std::vector<double> > projxy;
91  for(int x = 0; x < fNMeshSegments[0]; x++) projxy.push_back(projy);
92  // accumulate
93  ofile << std::setprecision(16); // for double value with 8 bytes
94  for(int x = 0; x < fNMeshSegments[0]; x++) {
95    for(int y = 0; y < fNMeshSegments[1]; y++) {
96      for(int z = 0; z < fNMeshSegments[2]; z++) {
97
98        G4int idx = GetIndex(x, y, z);
99       
100        std::map<G4int, G4double*>::iterator value = score->find(idx);
101        if(value != score->end()) projxy[x][y] += *(value->second);
102
103      } // z
104    } // y
105  } // x
106
107  // write quantity
108  ofile << std::setprecision(16); // for double value with 8 bytes
109  for(int x = 0; x < fNMeshSegments[0]; x++) {
110    for(int y = 0; y < fNMeshSegments[1]; y++) {
111
112      ofile << x << "," << y << ",";
113      ofile << projxy[x][y] << G4endl;
114
115    } // y
116  } // x
117  ofile << std::setprecision(6);
118
119  // close the file
120  ofile.close();
121 
122}
123
Note: See TracBrowser for help on using the repository browser.