ProtoDUNEBeamTPCMatching_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: ProtoDUNEBeamTPCMatching
3 // Plugin Type: analyzer (art v2_07_03)
4 // File: ProtoDUNEBeamTPCMatching_module.cc
5 //
6 // Generated at Mon Sep 4 06:55:33 2017 by Leigh Whitehead using cetskelgen
7 // from cetlib version v3_00_01.
8 ////////////////////////////////////////////////////////////////////////
9 //// Modified by Pablo and Leigh H. Howard,
10 //Smear important variables of beam monitors, mimic Cherenkov monitors response
11 //and store some histograms through art utilities
12 //// pablo.fer@cern.ch
13 //// July 2018
14 ///////////////////////////////////////////////////////////
15 
23 #include "canvas/Persistency/Common/FindManyP.h"
24 #include "fhiclcpp/ParameterSet.h"
26 
36 //Histogram utilities
37 #include "art_root_io/TFileService.h"
38 #include "art_root_io/TFileDirectory.h"
39 #include <fstream>
40 #include "TH1F.h"
41 #include "TTree.h"
42 #include "TFile.h"
43 #include "TString.h"
44 
45 
46 #include "dune/EventGenerator/ProtoDUNEbeamDataProducts/ProtoDUNEbeamsim.h"
49 
50 
51 namespace sim {
52  class ProtoDUNEBeamTPCMatching;
53 }
54 
55 
57 public:
58 
60  // The compiler-generated destructor is fine for non-base
61  // classes without bare pointers or other resource use.
62 
63  // Plugins should not be copied or assigned.
68 
69  virtual void beginJob() override;
70  virtual void endJob() override;
71  void reconfigure(fhicl::ParameterSet const & p) ;
72 
73 
74  // Required functions.
75  void analyze(art::Event const & e) override;
76 
77 
78 
79 
80 private:
85 
87 //float tpc_true_E, tpc_true_mom, dummy;
89 TVector3 tpc_pos, tpc_dir;
90 
91 
93 
94 TTree *beamtpc;
95 /*TH1F *fT0FHist_zoom;
96 TH1F *fT0FsHist_zoom;
97 TH1F *fUxHist_zoom;
98 TH1F *fUxsHist_zoom;
99 TH1F *fUyHist_zoom;
100 TH1F *fUysHist_zoom;
101 TH1F *fUzHist_zoom;
102 TH1F *fUzsHist_zoom;
103 TH1F *fT0FHist;
104 TH1F *fT0FsHist;
105 TH1F *fUxHist;
106 TH1F *fUxsHist;
107 TH1F *fUyHist;
108 TH1F *fUysHist;
109 TH1F *fUzHist;
110 TH1F *fUzsHist;
111 */};
112 
113 
115  : EDAnalyzer(p)
116 {
117 
118 }
119 
121 {
122 // Declaring histograms
124 /* fT0FHist_zoom = tfs->make<TH1F>("TOF_TRUE_zoom","T0F (ns)",100,95,96);
125  fT0FsHist_zoom = tfs->make<TH1F>("TOF_SMEARED_zoom","T0F (ns)",100,95,96);
126  fUxHist_zoom = tfs->make<TH1F>("X_DIRECTION_TRUE_zoom","Ux",100,-0.05,0.05);
127  fUyHist_zoom = tfs->make<TH1F>("Y_DIRECTION_TRUE_zoom","Uy",100,-0.05,0.05);
128  fUzHist_zoom = tfs->make<TH1F>("Z_DIRECTION_TRUE_zoom","Uz",100,-1,-0.95);
129  fUxsHist_zoom = tfs->make<TH1F>("X_DIRECTION_SMEARED_zoom","Ux",100,-0.05,0.05);
130  fUysHist_zoom = tfs->make<TH1F>("Y_DIRECTION_SMEARED_zoom","Uy",100,-0.05,0.05);
131  fUzsHist_zoom = tfs->make<TH1F>("Z_DIRECTION_SMEARED_zoom","Uz",100,-1,-0.95);
132  fT0FHist = tfs->make<TH1F>("TOF_TRUE","T0F (ns)",100,90,100);
133  fT0FsHist = tfs->make<TH1F>("TOF_SMEARED","T0F (ns)",100,90,100);
134  fUxHist = tfs->make<TH1F>("X_DIRECTION_TRUE","Ux",100,-1,1);
135  fUyHist = tfs->make<TH1F>("Y_DIRECTION_TRUE","Uy",100,-1,1);
136  fUzHist = tfs->make<TH1F>("Z_DIRECTION_TRUE","Uz",100,-1,1);
137  fUxsHist = tfs->make<TH1F>("X_DIRECTION_SMEARED","Ux",100,-1,1);
138  fUysHist = tfs->make<TH1F>("Y_DIRECTION_SMEARED","Uy",100,-1,1);
139  fUzsHist = tfs->make<TH1F>("Z_DIRECTION_SMEARED","Uz",100,-1,1);
140 */
141  beamtpc = tfs->make<TTree>("beam-tpc", "Beam + TPC matiching Tree");
142  beamtpc->Branch("beam_true_tof", &beam_true_tof, "beam_true_tof/F");
143  beamtpc->Branch("beam_true_dir", &beam_true_dir, "beam_true_dir[3]/F");
144  beamtpc->Branch("beam_true_bprof4_pos",&beam_true_bprof4_pos, "beam_true_bprof4_pos[4]/F");
145  beamtpc->Branch("beam_smeared_tof", &beam_smeared_tof, "beam_smeared_tof/F");
146  beamtpc->Branch("beam_smeared_dir", &beam_smeared_dir, "beam_smeared_dir[3]/F");
147  beamtpc->Branch("beam_smeared_bprof4_pos",&beam_smeared_bprof4_pos, "beam_true_bprof4_pos[4]/F");
148 
149  beamtpc->Branch("tpc_true_startpos", &tpc_true_startpos, "tpc_true_startpos[4]/F");
150  beamtpc->Branch("tpc_true_dir", &tpc_true_dir, "tpc_true_dir[3]/F");
151  beamtpc->Branch("tpc_reco_startpos", &tpc_reco_startpos, "tpc_reco_startpos[4]/F");
152  beamtpc->Branch("tpc_reco_dir", &tpc_reco_dir, "tpc_reco_dir[3]/F");
153 }
154 
155 //--------------------------------------------------------------------
157 {
158  // Rotation angles from last beam section to TPC cordinates
159 // fBeamTPC_theta = pset.get<float>("BeamTPC_theta");
160 // fBeamTPC_phi = pset.get<float>("BeamTPC_phi");
161  // The name of the module that produced the tracks
162  fpandoraTrack = pset.get<std::string>("TrackLabel");
163  } // reconfigure
164 
165 
167 {
168  fBeamTPC_theta=0.27361019;
169  fBeamTPC_phi=3.9651559;
170 //========Start of Beam part==========
171  // Get the reconstructed tracks
172  auto beamsim = evt.getValidHandle<std::vector<sim::ProtoDUNEbeamsim> >("generator");
173  const sim::ProtoDUNEbeamsim temp = (*beamsim)[0];
174 // unsigned short nInst = temp.NInstruments();
175 // Computing true and smeared TOF (between TOF1 and TRIG2)
176  sim::ProtoDUNEBeamInstrument tof1 = temp.GetInstrument("TOF1");
177  sim::ProtoDUNEBeamInstrument trig2 = temp.GetInstrument("TRIG2");
178  beam_true_tof = trig2.GetT() - tof1.GetT();
180 // std::cout << "TOF " << tof << std::endl;
181 // std::cout << "TOFS " << tof_smeared << std::endl;
182 
183  sim::ProtoDUNEBeamInstrument bprofext = temp.GetInstrument("BPROFEXT");
184  sim::ProtoDUNEBeamInstrument bprof4 = temp.GetInstrument("BPROF4");
185 
186  beam_true_bprof4_pos[0] = bprof4.GetX() - 0.0;//?
187  beam_true_bprof4_pos[1] = bprof4.GetY() - 0.0;//?
188  beam_true_bprof4_pos[2] = bprof4.GetZ() - 717242.5;
189  beam_true_bprof4_pos[3] = bprof4.GetT();
190  beam_smeared_bprof4_pos[0] = bprof4.GetSmearedVar1() - 0.0;//?
191  beam_smeared_bprof4_pos[1] = bprof4.GetSmearedVar2() - 0.0;//?
192  beam_smeared_bprof4_pos[2] = bprof4.GetZ() - 717242.5;
193  beam_smeared_bprof4_pos[3] = bprof4.GetT();
194 
195  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(evt);
196 
197  double dummy;
198  dummy = pow(pow(bprofext.GetX() -bprof4.GetX(),2)+pow(bprofext.GetY() -bprof4.GetY(),2)+pow(bprofext.GetZ() -bprof4.GetZ(),2),0.5);
199  beam_true_dir[0] = (bprofext.GetX() -bprof4.GetX())/dummy;
200  beam_true_dir[1] = (bprofext.GetY() -bprof4.GetY())/dummy;
201  beam_true_dir[2] = (bprofext.GetZ() -bprof4.GetZ())/dummy;
203  beam_true_dir[1] = -sin(fBeamTPC_phi)*beam_true_dir[0] + cos(fBeamTPC_phi)*cos(fBeamTPC_theta)*beam_true_dir[1] + sin(fBeamTPC_theta)*cos(fBeamTPC_phi)*beam_true_dir[2];
204  beam_true_dir[2] = -sin(fBeamTPC_theta)*beam_true_dir[1] + cos(fBeamTPC_theta)*beam_true_dir[2];
205 // std::cout << dir[2] << " " << sin(fBeamTPC_phi)*sin(fBeamTPC_theta)*dir[0] << " + " << - cos(fBeamTPC_phi)*sin(fBeamTPC_theta)*dir[1] << "+ " << cos(fBeamTPC_theta)*dir[2] << std::endl;
206  dummy = pow(pow(bprofext.GetSmearedVar1() -bprof4.GetSmearedVar1(),2)+pow(bprofext.GetSmearedVar2() -bprof4.GetSmearedVar2(),2)+pow(bprofext.GetZ() -bprof4.GetZ(),2),0.5);
207  beam_smeared_dir[0] = (bprofext.GetSmearedVar1() -bprof4.GetSmearedVar1())/dummy;
208  beam_smeared_dir[1] = (bprofext.GetSmearedVar2() -bprof4.GetSmearedVar2())/dummy;
209  beam_smeared_dir[2] = (bprofext.GetZ() -bprof4.GetZ())/dummy;
210  beam_smeared_dir[0] = cos(fBeamTPC_phi)*beam_true_dir[0] + sin(fBeamTPC_phi)*cos(fBeamTPC_theta)*beam_true_dir[1] - sin(fBeamTPC_phi)*sin(fBeamTPC_theta)*beam_true_dir[2];
211  beam_smeared_dir[1] = -sin(fBeamTPC_phi)*beam_true_dir[0] + cos(fBeamTPC_phi)*cos(fBeamTPC_theta)*beam_true_dir[1] + sin(fBeamTPC_theta)*cos(fBeamTPC_phi)*beam_true_dir[2];
212  beam_smeared_dir[2] = -sin(fBeamTPC_theta)*beam_true_dir[1] + cos(fBeamTPC_theta)*beam_true_dir[2];
213 // std::cout << fBeamTPC_phi << " " << fBeamTPC_theta << std::endl;
214 
215 // std::cout << "cos" << dir[0]*dir_smeared[0]+dir[1]*dir_smeared[1]+dir[2]*dir_smeared[2] << std::endl;
216 // std::cout << bprofext.GetX() << "," << bprofext.GetSmearedVar1() << std::endl;
217 // std::cout << dir[0] << "," << dir_smeared[0] << std::endl;
218 
219 //Filling hisotgrams
220 /* fT0FHist_zoom->Fill(beam_true_tof);
221  fT0FsHist_zoom->Fill(beam_smeared_tof);
222  fUxHist_zoom->Fill(beam_true_dir[0]);
223  fUxsHist_zoom->Fill(beam_smeared_dir[0]);
224  fUyHist_zoom->Fill(beam_true_dir[1]);
225  fUysHist_zoom->Fill(beam_smeared_dir[1]);
226  fUzHist_zoom->Fill(beam_true_dir[2]);
227  fUzsHist_zoom->Fill(beam_smeared_dir[2]);
228  fT0FHist->Fill(beam_true_tof);
229  fT0FsHist->Fill(beam_smeared_tof);
230  fUxHist->Fill(beam_true_dir[0]);
231  fUxsHist->Fill(beam_smeared_dir[0]);
232  fUyHist->Fill(beam_true_dir[1]);
233  fUysHist->Fill(beam_smeared_dir[1]);
234  fUzHist->Fill(beam_true_dir[2]);
235  fUzsHist->Fill(beam_smeared_dir[2]);*/
236 //========End of Beam part==========
237 //
238 //========Start of TPC part==========
239 //Get tracks
240 /* art::Handle< std::vector<simb::MCTruth> > mctruthhandle;
241  std::vector< art::Ptr<simb::MCTruth> > mclist;
242  art::Ptr<simb::MCTruth> mctruth;
243  mctruth = mclist[0];
244 */
245  auto recoTracks = evt.getValidHandle<std::vector<recob::Track> >(fpandoraTrack);
248  dummy = 999999.;
249  TVector3 pos, dir_start, dir_end;
250  for(unsigned int t = 0; t < recoTracks->size(); ++t){
251  const recob::Track thisTrack = (*recoTracks)[t];
252  tpc_pos = thisTrack.Vertex<TVector3>();
253  tpc_dir = thisTrack.VertexDirection<TVector3>();
254  std::vector<anab::T0> trackT0 = trackUtil.GetRecoTrackT0(thisTrack,evt,fpandoraTrack);
255  tpc_reco_startpos[3] = trackT0[0].fTime;
256  tpc_reco_startpos[0] = tpc_pos.X();
257  tpc_reco_startpos[1] = tpc_pos.Y();
258  tpc_reco_startpos[2] = tpc_pos.Z();
259 // tpc_true_startpos = thisTrack->StartMomentumVector();
260  if (dummy >= tpc_reco_startpos[3]){
261  dummy = tpc_reco_startpos[3];
262  const simb::MCParticle *trueMatch = truthUtil.GetMCParticleFromRecoTrack(clockData, thisTrack,evt,fpandoraTrack);
263  bool hasTruth = (trueMatch != 0x0);
264  if (hasTruth == true) {
265 // std::cout << "hey" << std::endl;
266  tpc_true_startpos[0] = trueMatch->Vx();
267  tpc_true_startpos[1] = trueMatch->Vy();
268  tpc_true_startpos[2] = trueMatch->Vz();
269  tpc_true_startpos[3] = trueMatch->T();
270  tpc_true_dir[0] = trueMatch->Px() / trueMatch->P();
271  tpc_true_dir[1] = trueMatch->Py() / trueMatch->P();
272  tpc_true_dir[2] = trueMatch->Pz() / trueMatch->P();
273  }
274  }
275  }
276 
277 
278 
279 
280 /*// Find the associations between tracks and T0
281  const art::FindManyP<anab::T0> findTrackT0(trackHandle,evt,fpandoraTrack);
282 
283 // Also look for cosmic tags so we can make a T0 plot for cosmic tagged events only
284  const art::FindManyP<anab::CosmicTag> findCosmicTag(trackHandle,evt,fpandoraTrack);
285 */
286 beamtpc->Fill();
287 }
288 
289 
290 
291 
293 {
294 
295 }
296 
double Py(const int i=0) const
Definition: MCParticle.h:231
std::string string
Definition: nybbler.cc:12
constexpr T pow(T x)
Definition: pow.h:72
double Px(const int i=0) const
Definition: MCParticle.h:230
Vector_t VertexDirection() const
Definition: Track.h:132
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
Particle class.
std::vector< anab::T0 > GetRecoTrackT0(const recob::Track &track, art::Event const &evt, std::string trackModule) const
Get the T0(s) from a given reco track.
void reconfigure(fhicl::ParameterSet const &p)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
double P(const int i=0) const
Definition: MCParticle.h:234
T get(std::string const &key) const
Definition: ParameterSet.h:271
Point_t const & Vertex() const
Definition: Track.h:124
double T(const int i=0) const
Definition: MCParticle.h:224
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
ProtoDUNEBeamTPCMatching(fhicl::ParameterSet const &p)
void analyze(art::Event const &e) override
Code to link reconstructed objects back to the MC truth information.
double Vx(const int i=0) const
Definition: MCParticle.h:221
Declaration of signal hit object.
ProtoDUNEBeamTPCMatching & operator=(ProtoDUNEBeamTPCMatching const &)=delete
double Pz(const int i=0) const
Definition: MCParticle.h:232
Provides recob::Track data product.
double Vz(const int i=0) const
Definition: MCParticle.h:223
cet::LibraryManager dummy("noplugin")
TCEvent evt
Definition: DataStructs.cxx:7
double Vy(const int i=0) const
Definition: MCParticle.h:222
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49