SculptedAbsorberCompress.cc
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string>
3 #include <fstream>
4 #include <sstream>
5 #include <vector>
6 #include <iostream>
7 //
8 // This is a translated and compiled version of SculptedAbosrber R script. It write a txt file on
9 // /lbne/data, which has the Slice -2 and slice 30 Ntuple. Sile 30: there will two files: all the muon, and only
10 // the muons that are not seen at slice 2 (created in the spoile, or downstream of it.
11 //
12 // July 14, cross-checks with the new flag to signal the intercept with the slice in fron of HA.
13 // This is obsolete.. Since the G4 tracks appear in the ntuple out of order, too complicate and unnecessary to fix..
14 //
15 // However, our colleagues from Coloroda are trying to implement a similar algorythm, from their root ntuple.
16 // So, let us fix this
17 //
18 class SCHAmuon {
19  public:
20  int evt, tr, id, slice;
21  double x, y, z, px, py, pz, ek, w, g2;
22  inline void out(std::ofstream &fOut) const {
23  fOut << " " << evt << " " << tr << " " << id << " ";
24  fOut << " " << (x/10.) << " " << (y/10.) << " " << (z/10.);
25  fOut << " " << (px) << " " << (py) << " " << (pz);
26  fOut << " " << ek << " " << w << " " << g2 << std::endl;
27  }
28 };
29 
30 int main (int argc, char **argv) {
31 
32  const std::string dirOutName("/lbne/data/users/lebrun/SculptedAbsorber/");
33 // const std::string dirPnfs("/pnfs/lbne/scratch/users/lebrun/LBNFSculptHA1f/");
34 // const std::string tokenDir("LBNFSculptHA1g"); // 50 mRad, X offset -1.8 mRad
35  const std::string tokenDir("LBNFSculptHA2a"); // V2, nominal geometry
36 // const std::string tokenDir("LBNEAbsorbV1a"); // Straight, but default LBNE absorber.
37  bool doLBNE = (tokenDir.find("LBNE") != std::string::npos);
38  std::string dirPnfs("/pnfs/lbne/scratch/users/lebrun/");
39  dirPnfs += tokenDir + std::string("/");
40  const std::string dirScratch("/scratch/lbne/lebrun/");
41  const std::string jtitleHA("MuonFluxAtSculptedAbsorber_SculptedAbsorberProd1_");
42 // const std::string jtitleHA("MuonFluxAtSculptedAbsorber_SculptedAbsorberProd3_");
43 // const std::string jtitleHA("MuonFluxAtSculptedAbsorber_LBNEAbsorberProd1_");
44 // std::string header(" evt tr id slice x y z px py pz ek w ");
45  std::string header(" evt tr id x y z px py pz ek w g2 "); // skip the slice, file specific...
46 
47 // const int jCluster = 2048727; // for LBNFSculptHA1f, Horizontal staggering of the cracks.
48 // const int jCluster = 2071904; // for LBNFSculptHA1f, Horizontal staggering of the cracks, + X angle of 0.25 mRad
49 // const int jCluster = 2015073; // Cracks are aligned.. LBNFSculptHA1f
50 // const int jCluster = 2109228; // Cracks are aligned.. LBNFSculptHA1f
51 // const int jCluster = 2109228; // Cracks are not aligned.. LBNFSculptHA1g
52  const int jCluster = 2623210; //LBNEAbsorbV1a
53 
54  std::vector<SCHAmuon> datSl30Tmp;
55  std::vector<SCHAmuon> datSlM2Tmp;
56  std::vector<SCHAmuon> datSl30Cr;
57  std::vector<SCHAmuon> datOneTr;
58 
59  std::ostringstream fOutSlM2StrStr;
60  fOutSlM2StrStr << dirOutName << "MuonFluxAtSculptedAbsorber_" << tokenDir << "_V2Slm2_" << jCluster << ".txt";
61  std::string fOutSlM2Str(fOutSlM2StrStr.str());
62  std::ofstream fOutSlM2(fOutSlM2Str.c_str());
63  fOutSlM2 << header << std::endl;
64  fOutSlM2.precision(6);
65 
66 
67  std::ostringstream fOutSl30StrStr;
68  fOutSl30StrStr << dirOutName << "MuonFluxAtSculptedAbsorber_" << tokenDir << "_V2Sl30_" << jCluster << ".txt";
69  std::string fOutSl30Str(fOutSl30StrStr.str());
70  std::ofstream fOutSl30(fOutSl30Str.c_str());
71  fOutSl30 << header << std::endl;
72  fOutSl30.precision(6);
73 
74 // const size_t numFiles=50; // run e anf g
75  const size_t numFiles=200; //run a, with default absorber.. (and run g..)
76  int nBadFiles = 0;
77 // const size_t numFiles=2; //run g
78  size_t nSlM2=0; size_t nSl30=0;
79 // for (size_t kf=0; kf != numFiles; kf++) {
80  for (size_t kf=0; kf != 3; kf++) {
81 // std::ostringstream cmdStrStr;
82 // cmdStrStr << "ifdh cp -D "<< dirPnfs << jtitleHA << jCluster << "_" << kf << ".txt " << dirScratch;
83 // std::string cmdStr(cmdStrStr.str());
84 // system(cmdStr.c_str());
85  std::ostringstream aFNameStrStr;
86  aFNameStrStr << dirScratch << jtitleHA << jCluster << "_" << kf << ".txt";
87  std::string aFNameStr(aFNameStrStr.str());
88  std::ifstream fIn(aFNameStr.c_str());
89  if (!fIn.is_open()) {
90  std::cerr << " Can't open file " << aFNameStr << std::endl;
91 // return EXIT_FAILURE;
92  nBadFiles++;
93  continue;
94  } else {
95  std::cerr << " Got file " << aFNameStr << std::endl;
96  }
97  size_t k=0;
98  int prevEvt = -1;
99  int prevTr = -1;
100  while (fIn.good() &&(!fIn.eof())) {
101  k++;
102  char line[1024];
103  fIn.getline(line, 1023);
104  if (k == 1) continue;
105  std::string linec(line);
106  if (fIn.eof()) break;
107  if (linec.length() < 5) continue;
108  SCHAmuon a;
109  std::istringstream sIn(linec);
110  sIn >> a.evt; sIn >> a.tr; sIn >> a.id; sIn >> a.slice;
111  if ((a.slice != -2) && (a.slice != 30)) continue;
112  sIn >> a.x; sIn >> a.y; sIn >> a.z;
113  sIn >> a.px; sIn >> a.py; sIn >> a.pz;
114  sIn >> a.ek; sIn >> a.w ; sIn >> a.g2;
115 
116  if (a.slice == -2) {
117  a.out(fOutSlM2);
118  datSlM2Tmp.push_back(a);
119  nSlM2++;
120  }
121  if (a.slice == 30) {
122  if ((!doLBNE) || (doLBNE && (a.z < 236000.))) { // Take only the first detector plane, for sake of consistency with the
123  // sculpted absorber...
124  a.out(fOutSl30);
125  datSl30Tmp.push_back(a);
126  nSl30++;
127  }
128  }
129  if ((prevEvt != a.evt) || (prevTr != a.tr)) { // New track. Analyze the previous track.
130  size_t nSlPrev = datOneTr.size();
131  if (nSlPrev != 0) {
132  if ((datOneTr[nSlPrev-1].slice == 30) && (datOneTr[0].slice != -2)) {
133  datSl30Cr.push_back(datOneTr[nSlPrev-1]);
134  }
135  }
136  datOneTr.clear();
137  datOneTr.push_back(a);
138  prevEvt = a.evt;
139  prevTr = a.tr;
140  } else {
141  datOneTr.push_back(a);
142  }
143  }
144  if (k < 2) {
145  std::cerr << " One empty file " << aFNameStr << std::endl;
146  nBadFiles++;
147  continue;
148  }
149 // std::ostringstream cmdDelStrStr;
150 // cmdDelStrStr << " rm -f " << dirScratch << jtitleHA << jCluster << "_" << kf << ".txt";
151 // std::string cmdDelStr(cmdDelStrStr.str());
152 // system(cmdDelStr.c_str());
153  //
154  // Now pick the muons that are detected in slice 30, but not in slice 2.
155  // We have to do this on g4lbne run (or Dk2nu file) basis...
156  //
157  std::vector<SCHAmuon>::const_iterator itm2 = datSlM2Tmp.begin();
158  if (itm2 == datSlM2Tmp.end()) continue;
159  std::cerr << " Number of muon in slice 30, all " << datSl30Tmp.size() << std::endl;
160  size_t nSl30G2G = 0;
161  size_t nSl30G2B = 0;
162  for(std::vector<SCHAmuon>::const_iterator it30 = datSl30Tmp.begin(); it30 != datSl30Tmp.end(); it30++) {
163  if (it30->g2 == 0) nSl30G2B++;
164  else nSl30G2G++;
165  }
166  size_t nSl30CrG2G = 0;
167  size_t nSl30CrG2B = 0;
168  for(std::vector<SCHAmuon>::const_iterator it30 = datSl30Cr.begin(); it30 != datSl30Cr.end(); it30++) {
169  if (it30->g2 == 0) nSl30CrG2B++;
170  else nSl30CrG2G++;
171  }
172 
173  std::cerr << " ... nSl30G2G " << nSl30G2G << " nSl30G2B " << nSl30G2B << std::endl;
174  std::cerr << " ... nSl30CrG2G " << nSl30CrG2G << " nSl30CrG2B " << nSl30CrG2B << std::endl;
175  } // On files..
176  //
177  fOutSlM2.close();
178  fOutSl30.close();
179  std::cerr << " Num Sl2 " << nSlM2 << " Slice 30 " << nSl30 << " Sl30 Cr " << datSl30Cr.size() << std::endl;
180 
181  std::ostringstream fOutSl30CrStrStr;
182  fOutSl30CrStrStr << dirOutName << "MuonFluxAtSculptedAbsorber_" << tokenDir << "_Sl30Cr_" << jCluster << ".txt";
183  std::string fOutSl30CrStr(fOutSl30CrStrStr.str());
184  std::ofstream fOutSl30Cr(fOutSl30CrStr.c_str());
185  fOutSl30Cr.precision(6);
186  fOutSl30Cr << header << std::endl;
187 
188  for (std::vector<SCHAmuon>::const_iterator it30Cr = datSl30Cr.begin(); it30Cr != datSl30Cr.end(); it30Cr++) {
189  it30Cr->out(fOutSl30Cr);
190  }
191  fOutSl30Cr.close();
192  std::cerr << " Number of empty or missing files: " << nBadFiles << std::endl;
193 }
void out(std::ofstream &fOut) const
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
int precision() const
Definition: qtextstream.h:259
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
int main(int argc, char **argv)
QTextStream & endl(QTextStream &s)