ProtoDUNEBeamCuts.cxx
Go to the documentation of this file.
1 #include "ProtoDUNEBeamCuts.h"
3 #include "dune/DuneObj/ProtoDUNEBeamEvent.h"
4 
6 
7 
8  std::vector< fhicl::ParameterSet > DataCuts_sets = pset.get< std::vector< fhicl::ParameterSet > >
9  ("DataCuts");
10  std::vector< fhicl::ParameterSet > MCCuts_sets = pset.get< std::vector< fhicl::ParameterSet > >
11  ("MCCuts");
12 
13  //Go through the data cuts
14  for( size_t i = 0; i < DataCuts_sets.size(); ++i ){
15  std::string momentum = DataCuts_sets[i].get<std::string>("Momentum");
16  if( std::find( valid_momenta.begin(), valid_momenta.end(), momentum ) == valid_momenta.end() ){
18  throw e;
19  }
20 
21  if( DataCuts.find( momentum ) != DataCuts.end() ){
22  std::cerr << "Error: Attempting to duplicate a cut parameter set" << std::endl;
24  throw e;
25  }
26 
27  DataCuts[momentum] = DataCuts_sets[i];
28  }
29 
30 
31  //Now go through the MC cuts
32  for( size_t i = 0; i < MCCuts_sets.size(); ++i ){
33 
34  std::string momentum = DataCuts_sets[i].get<std::string>("Momentum");
35  if( std::find( valid_momenta.begin(), valid_momenta.end(), momentum ) == valid_momenta.end() ){
37  throw e;
38  }
39 
40  if( MCCuts.find( momentum ) != MCCuts.end() ){
41  std::cerr << "Error: Attempting to duplicate a cut parameter set" << std::endl;
43  throw e;
44  }
45 
46  MCCuts[momentum] = MCCuts_sets[i];
47  }
48 
49 
50 }
51 
53 
54  if( std::find( valid_momenta.begin(), valid_momenta.end(), momentum ) == valid_momenta.end() ){
55  std::cerr << "Error. Momentum provided not in range" << std::endl;
57  throw e;
58  }
59 
60  fhicl::ParameterSet BeamPars = ( evt.isRealData() ? DataCuts[momentum] : MCCuts[momentum] );
61  BeamVals theBeamVals = ( evt.isRealData() ? GetDataBeam( evt ) : GetMCBeam( evt ) );
62  if( !theBeamVals.Valid ){
63  return false;
64  }
65 
66  double startX = track.Trajectory().Start().X();
67  double startY = track.Trajectory().Start().Y();
68  double startZ = track.Trajectory().Start().Z();
69 
70  double endX = track.Trajectory().End().X();
71  double endY = track.Trajectory().End().Y();
72  double endZ = track.Trajectory().End().Z();
73 
74  auto startDir = track.StartDirection();
75  auto endDir = track.EndDirection();
76  double trackDirX = 0.;
77  double trackDirY = 0.;
78  double trackDirZ = 0.;
79 
80  //'Flip' the track if endZ < startZ
81  if( endZ < startZ ){
82  startX = endX;
83  startY = endY;
84  startZ = endZ;
85 
86  trackDirX = -1. * endDir.X();
87  trackDirY = -1. * endDir.Y();
88  trackDirZ = -1. * endDir.Z();
89  }
90  else{
91  trackDirX = startDir.X();
92  trackDirY = startDir.Y();
93  trackDirZ = startDir.Z();
94  }
95 
96  double deltaX = startX - theBeamVals.X;
97  double deltaY = startY - theBeamVals.Y;
98 
99  double costheta = theBeamVals.DirX*trackDirX + theBeamVals.DirY*trackDirY + theBeamVals.DirZ*trackDirZ;
100 
101  std::pair< double, double > startX_cut = BeamPars.get< std::pair< double, double > >("TrackStartXCut");
102  std::pair< double, double > startY_cut = BeamPars.get< std::pair< double, double > >("TrackStartYCut");
103  std::pair< double, double > startZ_cut = BeamPars.get< std::pair< double, double > >("TrackStartZCut");
104  double costheta_cut = BeamPars.get< double >("TrackDirCut");
105 
106  static bool first = true;
107  if (first){
108  first = false;
109  //std::cout<<"Beam momentum = "<<momentum<<std::endl;
110  //std::cout<<"Cut on deltaX: ["<<startX_cut.first<<","<<startX_cut.second<<"]"<<std::endl;
111  //std::cout<<"Cut on deltaY: ["<<startY_cut.first<<","<<startY_cut.second<<"]"<<std::endl;
112  //std::cout<<"Cut on deltaZ: ["<<startZ_cut.first<<","<<startZ_cut.second<<"]"<<std::endl;
113  }
114 
115  if( deltaX < startX_cut.first || deltaX > startX_cut.second )
116  return false;
117  if( deltaY < startY_cut.first || deltaY > startY_cut.second )
118  return false;
119  if( startZ < startZ_cut.first || startZ > startZ_cut.second )
120  return false;
121  if( costheta < costheta_cut )
122  return false;
123 
124  //If here, the track is in the good beam region
125  return true;
126 
127 }
128 
130 
131  if( std::find( valid_momenta.begin(), valid_momenta.end(), momentum ) == valid_momenta.end() ){
132  std::cerr << "Error. Momentum provided not in range" << std::endl;
134  throw e;
135  }
136 
137  fhicl::ParameterSet BeamPars = ( evt.isRealData() ? DataCuts[momentum] : MCCuts[momentum] );
138  BeamVals theBeamVals = ( evt.isRealData() ? GetDataBeam( evt ) : GetMCBeam( evt ) );
139  if( !theBeamVals.Valid ){
140  return false;
141  }
142 
143  double startX = shower.ShowerStart().X();
144  double startY = shower.ShowerStart().Y();
145  double startZ = shower.ShowerStart().Z();
146 
147  auto startDir = shower.Direction();
148 
149  //flip the direction if pointing backward
150  double flip = ( startDir.Z() < 0. ? -1. : 1. );
151 
152  double showerDirX = flip * startDir.X();
153  double showerDirY = flip * startDir.Y();
154  double showerDirZ = flip * startDir.Z();
155 
156  double deltaX = startX - theBeamVals.X;
157  double deltaY = startY - theBeamVals.Y;
158 
159  double costheta = theBeamVals.DirX*showerDirX + theBeamVals.DirY*showerDirY + theBeamVals.DirZ*showerDirZ;
160  std::pair< double, double > startX_cut = BeamPars.get< std::pair< double, double > >("TrackStartXCut");
161  std::pair< double, double > startY_cut = BeamPars.get< std::pair< double, double > >("TrackStartYCut");
162  std::pair< double, double > startZ_cut = BeamPars.get< std::pair< double, double > >("TrackStartZCut");
163  double costheta_cut = BeamPars.get< double >("TrackDirCut");
164 
165  if( deltaX < startX_cut.first || deltaX > startX_cut.second )
166  return false;
167  if( deltaY < startY_cut.first || deltaY > startY_cut.second )
168  return false;
169  if( startZ < startZ_cut.first || startZ > startZ_cut.second )
170  return false;
171  if( costheta < costheta_cut )
172  return false;
173 
174  //If here, the track is in the good beam region
175  return true;
176 
177 }
178 
179 
180 
182 
184  auto mcTruths = evt.getValidHandle< std::vector< simb::MCTruth > >("generator");
185  const simb::MCParticle* true_beam_particle = truthUtil.GetGeantGoodParticle((*mcTruths)[0],evt);
186 
188  result.Valid = false;
189 
190  if( !true_beam_particle ){
191  std::cout << "No true beam particle" << std::endl;
192  return result;
193  }
194 
195  result.Valid = true;
196  result.DirX = true_beam_particle->Px() / true_beam_particle->P();
197  result.DirY = true_beam_particle->Py() / true_beam_particle->P();
198  result.DirZ = true_beam_particle->Pz() / true_beam_particle->P();
199 
200  //Project the beam to Z = 0
201  result.X = true_beam_particle->Position(0).X() + (-1.*true_beam_particle->Position(0).Z())*(result.DirX / result.DirZ);
202  result.Y = true_beam_particle->Position(0).Y() + (-1.*true_beam_particle->Position(0).Z())*(result.DirY / result.DirZ);
203  return result;
204 }
205 
207  std::vector<art::Ptr<beam::ProtoDUNEBeamEvent>> beamVec;
208  auto beamHandle = evt.getValidHandle< std::vector< beam::ProtoDUNEBeamEvent > >("beamevent");
209 
211  result.Valid = false;
212 
213  if( beamHandle.isValid()){
214  art::fill_ptr_vector(beamVec, beamHandle);
215  }
216  //Should just have one
217  const beam::ProtoDUNEBeamEvent & beamEvent = *(beamVec.at(0));
218 
219  const std::vector< recob::Track > & beamTracks = beamEvent.GetBeamTracks();
220  if( beamTracks.size() == 0 ){
221  std::cout << "Warning: no tracks associated to beam data" << std::endl;
222  return result;
223  }
224  else if( beamTracks.size() > 1 ){
225  std::cout << "Warning: mutiple tracks associated to beam data" << std::endl;
226  return result;
227  }
228 
229  result.Valid = true;
230  result.X = beamTracks.at(0).Trajectory().End().X();
231  result.Y = beamTracks.at(0).Trajectory().End().Y();
232 
233  result.DirX = beamTracks.at(0).EndDirection().X();
234  result.DirY = beamTracks.at(0).EndDirection().Y();
235  result.DirZ = beamTracks.at(0).EndDirection().Z();
236  return result;
237 }
BeamVals GetDataBeam(const art::Event &)
const TVector3 & ShowerStart() const
Definition: Shower.h:192
const simb::MCParticle * GetGeantGoodParticle(const simb::MCTruth &genTruth, const art::Event &evt) const
static QCString result
std::string string
Definition: nybbler.cc:12
std::vector< std::string > valid_momenta
const recob::TrackTrajectory & Trajectory() const
Access to the stored recob::TrackTrajectory.
Definition: Track.h:98
bool IsBeamlike(const recob::Track &, const art::Event &, std::string)
bool isRealData() const
Vector_t StartDirection() const
Access to track direction at different points.
Definition: Track.h:131
const double e
T get(std::string const &key) const
Definition: ParameterSet.h:271
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
const TVector3 & Direction() const
Definition: Shower.h:189
std::map< std::string, fhicl::ParameterSet > DataCuts
Vector_t EndDirection() const
Definition: Track.h:133
Point_t const & End() const
Returns the position of the last valid point of the trajectory [cm].
std::map< std::string, fhicl::ParameterSet > MCCuts
const std::vector< recob::Track > & GetBeamTracks() const
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
def momentum(x1, x2, x3, scale=1.)
Point_t const & Start() const
Returns the position of the first valid point of the trajectory [cm].
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
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
BeamVals GetMCBeam(const art::Event &)