All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BooNEInterface.cxx
Go to the documentation of this file.
1 #include <sstream>
2 #include <string>
3 #include <stdlib.h>
4 #include <cmath>
5 #include <regex>
6 
7 #include "TFile.h"
8 #include "TTree.h"
9 
10 #include "Framework/ParticleData/PDGUtils.h"
11 
12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 
14 #include "BooNEInterface.h"
15 
16 
17 namespace fluxr {
19  {
20  }
21 
23  {
24  }
25 
26  void BooNEInterface::SetRootFile(TFile* fluxInputFile)
27  {
28  fBNBtree = dynamic_cast<TTree*>(fluxInputFile->Get("h201"));
29  fBNBtree->SetBranchAddress("beamwgt",&fBooneNtp.beamwgt);
30  fBNBtree->SetBranchAddress("ntp",&fBooneNtp.ntp);
31  fBNBtree->SetBranchAddress("npart",&fBooneNtp.npart);
32  fBNBtree->SetBranchAddress("id",fBooneNtp.id);
33  fBNBtree->SetBranchAddress("ini_pos",&fBooneNtp.ini_pos[0][0]);
34  fBNBtree->SetBranchAddress("ini_mom",&fBooneNtp.ini_mom[0][0]);
35  fBNBtree->SetBranchAddress("ini_eng",fBooneNtp.ini_eng);
36  fBNBtree->SetBranchAddress("ini_t",fBooneNtp.ini_t);
37  fBNBtree->SetBranchAddress("fin_mom",&fBooneNtp.fin_mom[0][0]);
38  fBNBtree->SetBranchAddress("fin_pol",&fBooneNtp.fin_pol[0][0]);
39 
40  fWindowtree = dynamic_cast<TTree*>(fluxInputFile->Get("h220"));
41  fWindowtree->SetBranchAddress("tank_pos_beam",&fBeamNtp.tank_pos_beam[0]);
42  fWindowtree->SetBranchAddress("targ_pos_beam",&fBeamNtp.targ_pos_beam[0]);
43  fWindowtree->SetBranchAddress("pot",&fBeamNtp.pot);
44  fWindowtree->SetBranchAddress("windowbase",&fBeamNtp.windowbase[0]);
45  fWindowtree->SetBranchAddress("windowdir1",&fBeamNtp.windowdir1[0]);
46  fWindowtree->SetBranchAddress("windowdir2",&fBeamNtp.windowdir2[0]);
47 
48 
49  fNEntries = fBNBtree->GetEntries();
50  mf::LogInfo("BooNEInterface") << "Reading " << fNEntries << " events" << std::endl;
51 
52  fMaxWeight = 0;
53  fMinWeight = 100;
54  fSumWeights = 0;
55 
56  std::string run_number = fluxInputFile->GetName();
57  std::regex r("\\d\\d\\d\\d");
58  std::smatch m;
59  std::regex_search(run_number,m,r);
60 
61  std::istringstream buffer(m[0].str().c_str());
62  buffer >> fRun;
63 
64 
65  for ( int iev = 0 ; iev < fNEntries ; iev++ ) {
66  fBNBtree->GetEntry(iev);
70  }
71 
72  mf::LogInfo("BooNEInterface") << "Min Weight: " << fMinWeight
73  << ", Max Weight: " << fMaxWeight
74  << ", Sum Weights: " << fSumWeights
75  << std::endl;
76 
77 
78  fWindowtree->GetEntry(0);
79  fPOT = fBeamNtp.pot;
80 
81  std::cout << "Window:" << std::endl;
82  std::cout << "\tBase: " << fBeamNtp.windowbase[0] << ", " << fBeamNtp.windowbase[1] << ", " << fBeamNtp.windowbase[2] << std::endl;
83  std::cout << "\tDir 1: " << fBeamNtp.windowdir1[0] << ", " << fBeamNtp.windowdir1[1] << ", " << fBeamNtp.windowdir1[2] << std::endl;
84  std::cout << "\tDir 2: " << fBeamNtp.windowdir2[0] << ", " << fBeamNtp.windowdir2[1] << ", " << fBeamNtp.windowdir2[2] << std::endl;
85  }
86 
87 
88  bool BooNEInterface::FillMCFlux(Long64_t ientry, simb::MCFlux& flux)
89  {
90  if (!fBNBtree->GetEntry(ientry))
91  return false;
92 
93  mf::LogDebug("BooNEInterface") << "At entry: " << ientry << std::endl;
94 
95  fWindowtree->GetEntry(0);
96 
97 
98  if ( fBooneNtp.ntp == 1 ) {
99  flux.fntype = 12; //nue
100  }
101  else if ( fBooneNtp.ntp == 2 ) {
102  flux.fntype = -12; //nuebar
103  }
104  else if ( fBooneNtp.ntp == 3 ) {
105  flux.fntype = 14; //numu
106  }
107  else if ( fBooneNtp.ntp == 4 ) {
108  flux.fntype = -14; //numubar
109  }
110  else{
111  mf::LogWarning("BooNEInterface") << "Neutrino type not recognized! ntp = " << fBooneNtp.ntp
112  << std::endl;
113  }
114 
115  flux.fnimpwt = fBooneNtp.beamwgt;
116 
117  // Convert to meters
118  double nu_x = fBooneNtp.ini_pos[0][0] / 100;
119  double nu_y = fBooneNtp.ini_pos[0][1] / 100;
120  double nu_z = fBooneNtp.ini_pos[0][2] / 100;
121 
122  double nu_momx = fBooneNtp.ini_mom[0][0];
123  double nu_momy = fBooneNtp.ini_mom[0][1];
124  double nu_momz = fBooneNtp.ini_mom[0][2];
125 
126  double targ_x = fBeamNtp.targ_pos_beam[0] / 100;
127  double targ_y = fBeamNtp.targ_pos_beam[1] / 100;
128  double targ_z = fBeamNtp.targ_pos_beam[2] / 100;
129 
130  double tank_z = (fBeamNtp.tank_pos_beam[2] + fBeamNtp.windowbase[2]) / 100;
131 
132  // Calculate the neutrino x, y, and z position at the window location
133  double gsimple_vtxx = nu_x + (nu_momx / nu_momz) * (tank_z - nu_z) + targ_x;
134  double gsimple_vtxy = nu_y + (nu_momy / nu_momz) * (tank_z - nu_z) + targ_y;
135  double gsimple_vtxz = tank_z+targ_z;
136 
137  // Calculate the distance from the neutrino production point to the window
138  double dist = std::sqrt(std::pow(nu_x-gsimple_vtxx,2) +
139  std::pow(nu_y-gsimple_vtxy,2) +
140  std::pow(nu_z-tank_z,2));
141  flux.fdk2gen = dist;
142 
143  // Calculate the neutrino time at the window
144  float nu_time = fBooneNtp.ini_t[0]; // ns
145  nu_time += dist / (TMath::C() / 1e9);
146 
147  mf::LogDebug("BooNEInterface") << "weight = " << flux.fnimpwt
148  << ", nu time = " << nu_time
149  << ", nu energy = " << fBooneNtp.ini_eng[0]
150  << ", dist = " << dist
151  << std::endl;
152 
153  fNuPos = TLorentzVector(gsimple_vtxx * 100,
154  gsimple_vtxy * 100,
155  gsimple_vtxz * 100,
156  nu_time);
157  fNuMom = TLorentzVector(fBooneNtp.ini_mom[0][0],
158  fBooneNtp.ini_mom[0][1],
159  fBooneNtp.ini_mom[0][2],
160  fBooneNtp.ini_eng[0]);
161 
162  flux.fnenergyn = flux.fnenergyf = fBooneNtp.ini_eng[0];
163 
164  flux.frun = fRun;
165  flux.fevtno = ientry;
166 
167  int npart = fBooneNtp.npart;
168  flux.ftpx = fBooneNtp.ini_mom[npart-2][0]; // npart-2
169  flux.ftpy = fBooneNtp.ini_mom[npart-2][1]; // npart-2
170  flux.ftpz = fBooneNtp.ini_mom[npart-2][2]; // npart-2
171 
172  flux.fvx = fBooneNtp.ini_pos[0][0]; //0
173  flux.fvy = fBooneNtp.ini_pos[0][1]; //0
174  flux.fvz = fBooneNtp.ini_pos[0][2]; //0
175 
176  flux.fpdpx = fBooneNtp.fin_mom[1][0]; //1 final
177  flux.fpdpy = fBooneNtp.fin_mom[1][1]; //1 final
178  flux.fpdpz = fBooneNtp.fin_mom[1][2]; //1 final
179 
180  flux.fpppz = fBooneNtp.ini_mom[1][2]; //1 init
181  double pppx = fBooneNtp.ini_mom[1][0]; //1 init
182  double pppy = fBooneNtp.ini_mom[1][1]; //1 init
183  double apppz = flux.fpppz;
184  if (TMath::Abs(flux.fpppz) < 1.0e-30) apppz = 1.0e-30;
185  flux.fppdxdz = pppx / apppz;
186  flux.fppdydz = pppy / apppz;
187 
188 
189  flux.fppmedium = 0.; // empty
190 
191 
192  int ptype_input = fBooneNtp.id[1];
193  int tptype_input = fBooneNtp.id[npart-2];
194 
195  if (ptype_input != 0) ptype_input = genie::pdg::GeantToPdg(ptype_input);
196  if (tptype_input != 0) tptype_input= genie::pdg::GeantToPdg(tptype_input);
197 
198  flux.fptype = ptype_input;
199  flux.ftptype = tptype_input;
200 
201 
202  /////
203  // Now need to calculate ndecay
204  /////
205 
206  double Nenergy = fBooneNtp.ini_eng[0];
207  double Ndxdz = fBooneNtp.ini_mom[0][0] / fBooneNtp.ini_mom[0][2];
208  double Ndydz = fBooneNtp.ini_mom[0][1] / fBooneNtp.ini_mom[0][2];
209 
210  double ppenergy = fBooneNtp.ini_eng[1];
211  double pdPx = fBooneNtp.fin_mom[1][0];
212  double pdPy = fBooneNtp.fin_mom[1][1];
213  double pdPz = fBooneNtp.fin_mom[1][2];
214 
215  double ppdxdz = fBooneNtp.ini_mom[1][0] / fBooneNtp.ini_mom[1][2];
216  double ppdydz = fBooneNtp.ini_mom[1][1] / fBooneNtp.ini_mom[1][2];
217  double pppz = fBooneNtp.ini_mom[1][2];
218 
219  // Get the neutrino energy in the parent decay cm
220  double parent_mass = std::sqrt(ppenergy * ppenergy -
221  pppz * pppz * (ppdxdz * ppdxdz +
222  ppdydz * ppdydz +
223  1.));
224 
225  double parent_energy = std::sqrt(pdPx * pdPx +
226  pdPy * pdPy +
227  pdPz * pdPz +
228  parent_mass * parent_mass);
229 
230  double gamma = parent_energy / parent_mass;
231  double beta[3];
232  beta[0] = pdPx / parent_energy;
233  beta[1] = pdPy / parent_energy;
234  beta[2] = pdPz / parent_energy;
235 
236  double partial = fBooneNtp.ini_mom[0][2] * gamma *
237  (beta[0] * Ndxdz +
238  beta[1] * Ndydz +
239  beta[2]);
240 
241  double Necm = gamma * Nenergy - partial;
242 
243  if (fBooneNtp.id[1] == 10 && fBooneNtp.ntp == 1) flux.fndecay = 1;
244  else if (fBooneNtp.id[1] == 10 && fBooneNtp.ntp == 2) flux.fndecay = 2;
245  else if (fBooneNtp.id[1] == 10 && fBooneNtp.ntp == 3) flux.fndecay = 3;
246  else if (fBooneNtp.id[1] == 10 && fBooneNtp.ntp == 4) flux.fndecay = 4;
247  else if (fBooneNtp.id[1] == 11 && fBooneNtp.ntp == 3) {
248  //check if it is a two or three body decay
249  if (fabs((parent_mass*parent_mass-0.105658389*0.105658389)/(2.*parent_mass)-Necm)/Necm <= 0.001)
250  //two body decay (numu + mu+)
251  flux.fndecay = 5;
252  else {
253  //three body decay (numu + pi0 + mu+)
254  flux.fndecay = 7;
255  }
256  }
257  else if (fBooneNtp.id[1] == 11 && fBooneNtp.ntp == 1) flux.fndecay = 6;
258  else if (fBooneNtp.id[1] == 12 && fBooneNtp.ntp == 4) {
259  if (fabs((parent_mass*parent_mass-0.105658389*0.105658389)/(2.*parent_mass)-Necm)/Necm <= 0.001) {
260  //two body decay (numu + mu+)
261  flux.fndecay = 8;
262  }
263  else {
264  //three body decay (numu + pi0 + mu+)
265  flux.fndecay = 10;
266  }
267  }
268  else if (fBooneNtp.id[1] == 12 && fBooneNtp.ntp == 2) flux.fndecay = 9;
269  else if (fBooneNtp.id[1] == 5 ) flux.fndecay = 11;
270  else if (fBooneNtp.id[1] == 6 ) flux.fndecay = 12;
271  else if (fBooneNtp.id[1] == 8 ) flux.fndecay = 13;
272  else if (fBooneNtp.id[1] == 9 ) flux.fndecay = 14;
273 
274  /////
275  // End calculation of ndecay
276  /////
277 
278  double mupare;
279  double muparpx;
280  double muparpy;
281  double muparpz;
282 
283  if ( fBooneNtp.id[1] == 5 || fBooneNtp.id[1] == 6) {
284  mupare = fBooneNtp.ini_eng[2];
285  muparpx = fBooneNtp.fin_mom[2][0];
286  muparpy = fBooneNtp.fin_mom[2][1];
287  muparpz = fBooneNtp.fin_mom[2][2];
288  } else {
289  mupare = -9999.;
290  muparpx = -9999.;
291  muparpy = -9999.;
292  muparpz = -9999.;
293  }
294 
295  flux.fmuparpx = muparpx;
296  flux.fmuparpy = muparpy;
297  flux.fmuparpz = muparpz;
298  flux.fmupare = mupare;
299 
300  flux.fnwtnear = flux.fnwtfar = 1;
301 
302  return true;
303  }
304 }
float ini_t[20]
E of particle in before chain, array length &#39;npart&#39;.
int npart
1,2,3,4: nue, nuebar, numu, numubar
float ini_pos[20][3]
id of each particle in before chain, array length &#39;npart&#39;
float windowbase[3]
pot in the file
float ini_eng[20]
3-mom of particle in before chain, array length &#39;npart&#39;
float pot
Frame conversion from beam to flux frame.
tuple m
now if test mode generate materials, CRT shell, world, gdml header else just generate CRT shell for u...
bool FillMCFlux(Long64_t ientry, simb::MCFlux &mcflux)
Interface to read flux files in BooNE tuple format.
void SetRootFile(TFile *rootFileName)
float fin_mom[20][3]
float windowdir2[3]
Flux window direction vector 1.
float windowdir1[3]
Flux window base vector.
float ini_mom[20][3]
3-pos of particle in before chain, array length &#39;npart&#39;
float tank_pos_beam[3]
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
float targ_pos_beam[3]
3-position of the flux window;
float fin_pol[20][3]
final 3-mom of particle in before chain, array length &#39;npart&#39;
TLorentzVector fNuMom
esac echo uname r
int ntp
Magic Weight: CrossSect * BooBeamNT.
BEGIN_PROLOG could also be cout
TLorentzVector fNuPos