SaveSpacePoints_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: SaveSpacePoints
3 // Plugin Type: analyzer (art v2_11_02)
4 // File: SaveSpacePoints_module.cc
5 //
6 // Generated at Wed Jul 11 09:18:07 2018 by Tingjun Yang using cetskelgen
7 // from cetlib version v3_03_01.
8 ////////////////////////////////////////////////////////////////////////
9 
16 #include "art_root_io/TFileService.h"
18 #include "canvas/Persistency/Common/FindManyP.h"
19 #include "fhiclcpp/ParameterSet.h"
21 
30 
31 #include "dune/DuneObj/ProtoDUNEBeamEvent.h"
33 
34 // ROOT includes
35 #include "TTree.h"
36 #include "TTimeStamp.h"
37 
38 namespace proto {
39  class SaveSpacePoints;
40 }
41 
42 
44 public:
45  explicit SaveSpacePoints(fhicl::ParameterSet const & p);
46  // The compiler-generated destructor is fine for non-base
47  // classes without bare pointers or other resource use.
48 
49  // Plugins should not be copied or assigned.
50  SaveSpacePoints(SaveSpacePoints const &) = delete;
51  SaveSpacePoints(SaveSpacePoints &&) = delete;
52  SaveSpacePoints & operator = (SaveSpacePoints const &) = delete;
54 
55  // Required functions.
56  void analyze(art::Event const & e) override;
57 
58  // Selected optional functions.
59  void beginJob() override;
60 
61 private:
62 
67 
69 
70  TTree *fTree;
71  // Run information
72  int run;
73  int subrun;
74  int event;
75  int trigger;
76  double evttime;
77 
78  // space point information
79  std::vector<double> vx;
80  std::vector<double> vy;
81  std::vector<double> vz;
82  std::vector<double> vcharge;
83  std::vector<int> vtpc;
84  std::vector<int> vtrackid;
85  std::vector<int> vpdg;
86  std::vector<int> vg4id;
87  std::vector<int> vorigin;
88 
89  // beam information
90  std::vector<double> beamPosx;
91  std::vector<double> beamPosy;
92  std::vector<double> beamPosz;
93 
94  std::vector<double> beamDirx;
95  std::vector<double> beamDiry;
96  std::vector<double> beamDirz;
97 
98  std::vector<double> beamMomentum;
99 
100  double tof;
101  short ckov0status;
102  short ckov1status;
103 
104 };
105 
106 
108  :
109  EDAnalyzer(p),
110  fSpacePointModuleLabel(p.get< art::InputTag >("SpacePointModuleLabel")),
111  fBeamModuleLabel(p.get< art::InputTag >("BeamModuleLabel")),
112  fTrackModuleLabel(p.get< art::InputTag >("TrackModuleLabel")),
113  fTimeDecoderModuleLabel(p.get< art::InputTag >("TimeDecoderModuleLabel")),
114  fBeamlineUtils(p.get<fhicl::ParameterSet>("BeamlineUtils"))
115 {}
116 
118 {
119  // Implementation of required member function here.
120  run = evt.run();
121  subrun = evt.subRun();
122  event = evt.id().event();
123  art::Timestamp ts = evt.time();
124  //std::cout<<ts.timeHigh()<<" "<<ts.timeLow()<<std::endl;
125  if (ts.timeHigh() == 0){
126  TTimeStamp tts(ts.timeLow());
127  evttime = tts.AsDouble();
128  }
129  else{
130  TTimeStamp tts(ts.timeHigh(), ts.timeLow());
131  evttime = tts.AsDouble();
132  }
133  vx.clear();
134  vy.clear();
135  vz.clear();
136  vcharge.clear();
137  vtpc.clear();
138  vtrackid.clear();
139  vpdg.clear();
140  vg4id.clear();
141  vorigin.clear();
142  beamPosx.clear();
143  beamPosy.clear();
144  beamPosz.clear();
145  beamDirx.clear();
146  beamDiry.clear();
147  beamDirz.clear();
148  beamMomentum.clear();
149 
150  if (evt.isRealData()){
152 
153  //Access the Beam Event
154  auto beamHandle = evt.getValidHandle<std::vector<beam::ProtoDUNEBeamEvent>>("beamevent");
155 
156  std::vector<art::Ptr<beam::ProtoDUNEBeamEvent>> beamVec;
157  if( beamHandle.isValid()){
158  art::fill_ptr_vector(beamVec, beamHandle);
159  }
160 
161  const beam::ProtoDUNEBeamEvent & beamEvent = *(beamVec.at(0)); //Should just have one
162 
163  //Access momentum
164  const std::vector< double > & the_momenta = beamEvent.GetRecoBeamMomenta();
165  std::cout << "Number of reconstructed momenta: " << the_momenta.size() << std::endl;
166 
167  beamMomentum.insert( beamMomentum.end(), the_momenta.begin(), the_momenta.end() );
168 
169  tof = -1;
170  ckov0status = -1;
171  ckov1status = -1;
172 
173  //Access time of flight
174  const std::vector< double > & the_tofs = beamEvent.GetTOFs();
175 
176  if( the_tofs.size() > 0){
177  tof = the_tofs[0];
178  }
179  ckov0status = beamEvent.GetCKov0Status();
180  ckov1status = beamEvent.GetCKov1Status();
181  }
182 
183  trigger = -1;
184  art::ValidHandle<std::vector<raw::RDTimeStamp>> timeStamps = evt.getValidHandle<std::vector<raw::RDTimeStamp>>(fTimeDecoderModuleLabel);
185 
186  // Check that we have good information
187  if(timeStamps.isValid() && timeStamps->size() == 1){
188  // Access the trigger information. Beam trigger flag = 0xc
189  const raw::RDTimeStamp& timeStamp = timeStamps->at(0);
190  trigger = timeStamp.GetFlags();
191  }
192  }
193 
194  std::vector< art::Ptr<recob::SpacePoint> > sps;
195  auto spsHandle = evt.getHandle< std::vector<recob::SpacePoint> >(fSpacePointModuleLabel);
196  if (spsHandle) art::fill_ptr_vector(sps, spsHandle);
197 
198  std::vector< art::Ptr<recob::PointCharge> > pcs;
199  auto pcsHandle = evt.getHandle< std::vector<recob::PointCharge> >(fSpacePointModuleLabel);
200  if (pcsHandle) art::fill_ptr_vector(pcs, pcsHandle);
201 
202  art::FindManyP<recob::Hit> fmhsp(spsHandle, evt, fSpacePointModuleLabel);
203 
204  //Services
207 
208  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(evt);
209 
210  for (size_t i = 0; i<sps.size(); ++i){
211  vx.push_back(sps[i]->XYZ()[0]);
212  vy.push_back(sps[i]->XYZ()[1]);
213  vz.push_back(sps[i]->XYZ()[2]);
214  vcharge.push_back(pcs[i]->charge());
215  vtrackid.push_back(-1);
216  auto const& hits = fmhsp.at(i);
217  if (!evt.isRealData()){
218  int TrackID = 0;
219  std::map<int,double> trkide;
220  for (auto const & hit : hits){
221  std::vector<sim::TrackIDE> TrackIDs = bt_serv->HitToTrackIDEs(clockData, hit);
222  for(size_t e = 0; e < TrackIDs.size(); ++e){
223  trkide[TrackIDs[e].trackID] += TrackIDs[e].energy;
224  }
225  }
226  // Work out which IDE despoited the most charge in the hit if there was more than one.
227  double maxe = -1;
228  double tote = 0;
229  for (std::map<int,double>::iterator ii = trkide.begin(); ii!=trkide.end(); ++ii){
230  tote += ii->second;
231  if ((ii->second)>maxe){
232  maxe = ii->second;
233  TrackID = ii->first;
234  }
235  }
236  // Now have trackID, so get PdG code and T0 etc.
237  vg4id.push_back(TrackID);
238  const simb::MCParticle *particle = pi_serv->TrackIdToParticle_P(TrackID);
239  if (particle){
240  vpdg.push_back(particle->PdgCode());
241  vorigin.push_back(pi_serv->ParticleToMCTruth_P(particle)->Origin());
242  }
243  else{
244  vpdg.push_back(0);
245  vorigin.push_back(0);
246  }
247  }
248  else{
249  vg4id.push_back(0);
250  vpdg.push_back(0);
251  vorigin.push_back(0);
252  }
253  int spTPC = -1;
254  for (auto const & hit : hits){
255  spTPC = hit->WireID().TPC;
256  }
257  vtpc.push_back(spTPC);
258  }
259 
260  std::vector< art::Ptr<recob::Track> > trks;
261  auto trkHandle = evt.getHandle< std::vector<recob::Track> >(fTrackModuleLabel);
262  if (trkHandle) art::fill_ptr_vector(trks, trkHandle);
263 
264  for (size_t i = 0; i<trks.size(); ++i){
265  auto & trk = trks[i];
266  for (size_t j = 0; j<trk->NPoints(); ++j){
267  if (trk->HasValidPoint(j)){
268  vx.push_back(trk->TrajectoryPoint(j).position.X());
269  vy.push_back(trk->TrajectoryPoint(j).position.Y());
270  vz.push_back(trk->TrajectoryPoint(j).position.Z());
271  vcharge.push_back(0);
272  vtpc.push_back(-1);
273  vtrackid.push_back(trk->ID());
274  vpdg.push_back(-1);
275  vg4id.push_back(-1);
276  vorigin.push_back(-1);
277  }
278  }
279  }
280 
281  fTree->Fill();
282 }
283 
285 {
287  fTree = tfs->make<TTree>("spt","space point tree");
288  fTree->Branch("run",&run,"run/I");
289  fTree->Branch("subrun",&subrun,"subrun/I");
290  fTree->Branch("event",&event,"event/I");
291  fTree->Branch("trigger",&trigger,"trigger/I");
292  fTree->Branch("evttime",&evttime,"evttime/D");
293  fTree->Branch("vx",&vx);
294  fTree->Branch("vy",&vy);
295  fTree->Branch("vz",&vz);
296  fTree->Branch("vcharge",&vcharge);
297  fTree->Branch("vtpc",&vtpc);
298  fTree->Branch("vtrackid",&vtrackid);
299  fTree->Branch("vpdg",&vpdg);
300  fTree->Branch("vg4id",&vg4id);
301  fTree->Branch("vorigin",&vorigin);
302  fTree->Branch("beamPosx",&beamPosx);
303  fTree->Branch("beamPosy",&beamPosy);
304  fTree->Branch("beamPosz",&beamPosz);
305  fTree->Branch("beamDirx",&beamDirx);
306  fTree->Branch("beamDiry",&beamDiry);
307  fTree->Branch("beamDirz",&beamDirz);
308  fTree->Branch("beamMomentum",&beamMomentum);
309  fTree->Branch("tof", &tof, "tof/D");
310  fTree->Branch("ckov0status", &ckov0status, "ckov0status/S");
311  fTree->Branch("ckov1status", &ckov1status, "ckov1status/S");
312 }
313 
std::vector< double > beamPosx
intermediate_table::iterator iterator
int PdgCode() const
Definition: MCParticle.h:212
constexpr std::uint32_t timeLow() const
Definition: Timestamp.h:29
uint16_t GetFlags() const
Definition: RDTimeStamp.h:46
const std::vector< double > & GetTOFs() const
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
std::vector< double > beamDirz
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::vector< double > vcharge
std::vector< TrackID > TrackIDs
const simb::MCParticle * TrackIdToParticle_P(int id) const
protoana::ProtoDUNEBeamlineUtils fBeamlineUtils
const art::InputTag fTrackModuleLabel
constexpr std::uint32_t timeHigh() const
Definition: Timestamp.h:34
simb::Origin_t Origin() const
Definition: MCTruth.h:74
SaveSpacePoints(fhicl::ParameterSet const &p)
const short & GetCKov1Status() const
const art::InputTag fSpacePointModuleLabel
Information about charge reconstructed in the active volume.
std::vector< double > beamMomentum
std::vector< double > beamDiry
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
const art::Ptr< simb::MCTruth > & ParticleToMCTruth_P(const simb::MCParticle *p) const
const art::InputTag fTimeDecoderModuleLabel
std::vector< double > vy
std::vector< double > beamDirx
const art::InputTag fBeamModuleLabel
bool isRealData() const
std::vector< double > vx
const double e
Timestamp time() const
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
SaveSpacePoints & operator=(SaveSpacePoints const &)=delete
std::vector< double > beamPosz
const std::vector< double > & GetRecoBeamMomenta() const
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
General LArSoft Utilities.
bool IsGoodBeamlineTrigger(art::Event const &evt) const
RunNumber_t run() const
Definition: DataViewImpl.cc:71
Detector simulation of raw signals on wires.
std::vector< double > vz
Declaration of signal hit object.
std::vector< double > beamPosy
Provides recob::Track data product.
EventNumber_t event() const
Definition: EventID.h:116
const short & GetCKov0Status() const
void analyze(art::Event const &e) override
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)
Event finding and building.
bool isValid() const
Definition: Handle.h:330