readWeightLocations.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4 #include <string>
5 
6 #include "tree/readWeightLocations.h"
7 
8 #include "tree/dkmeta.h"
9 
10 #include "TSystem.h"
11 
12 /// Read a text file that contains a header line followed by
13 /// quartets of "<xpos> <ypos> <zpos> <text string>" on separate
14 /// lines. Fill the supplied vectors. Trim off leading/trailing
15 /// blanks and quotes (single/double) from the string.
16 /// Convention has it that positions are given in (cm).
18  std::vector<bsim::Location>& locations)
19 {
20 
21  const char* path = gSystem->ExpandPathName(locfilename.c_str());
22  std::ifstream locfile(path);
23 
24  int iline=0;
25 
26  char comment_buffer[1000];
27 
28  // read lines
29  char tmp[1001];
30  size_t tmplen = sizeof(tmp);
31  while ( ! locfile.eof() ) {
32  char c;
33  while ( ( c = locfile.get() ) == ' ' ) {}; // eat leading spaces
34  if ( c == '#' ) {
35  // comment_buffer line
36  locfile.getline(comment_buffer,sizeof(comment_buffer));
37  continue;
38  }
39  locfile.putback(c);
40  double x, y, z;
41  locfile >> x >> y >> z;
42  locfile.getline(tmp,tmplen-1);
43  size_t i = locfile.gcount();
44  // make sure the c-string is null terminated
45  size_t inull = i;
46  //if ( inull < 0 ) inull = 0;
47  if ( inull > tmplen-1 ) inull = tmplen-1;
48  tmp[inull] = '\0';
49  std::string name(tmp);
50  // ignore leading & trailing blanks (and any single/double quotes)
51  size_t ilast = name.find_last_not_of(" \t\n'\"");
52  name.erase(ilast+1,std::string::npos); // trim tail
53  size_t ifirst = name.find_first_not_of(" \t\n'\"");
54  name.erase(0,ifirst); // trim head
55 
56  ++iline;
57  if ( ! locfile.good() ) {
58  //if ( verbose)
59  // std::cout << "stopped reading on line " << iline << std::endl;
60  break;
61  }
62  bsim::Location alocation(x,y,z,name);
63  locations.push_back(alocation);
64  }
65 
66 }
67 
68 /// a variant that will fill the dkmeta object
70 {
71  /// read & print the locations where weights are to be calculated
72 
73  std::vector<bsim::Location>& locations = dkmeta->location;
74  /// make an entry for the random decay
75  bsim::Location rndmloc(0,0,0,"random decay");
76  locations.push_back(rndmloc);
77 
78  /// read and parse the text file for additional positions
79  /// use the vector version
80  readWeightLocations(locfilename, locations);
81 }
82 
83 void bsim::printWeightLocations(std::vector<bsim::Location>& locations)
84 {
85  size_t nl = locations.size();
86  std::cout << nl << " locations:\n";
87  for ( size_t l = 0; l < nl; ++l ) {
88  std::cout << " [" << std::setw(2) << l << "] " << locations[l] << "\n";
89  }
90 }
91 
93 {
95 }
const XML_Char * name
Definition: expat.h:151
std::vector< bsim::Location > location
locations
Definition: dkmeta.h:119
void readWeightLocations(std::string locfilename, std::vector< bsim::Location > &locations)
Float_t tmp
Definition: plot.C:36
void printWeightLocations(std::vector< bsim::Location > &locations)
print the locations
z
Definition: test.py:28
OStream cout
Definition: OStream.cxx:6
const std::string path
Definition: plot_BEN.C:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bsim::DkMeta * dkmeta
std::string nl(std::size_t i=1)