All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRTAutoVeto_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTAutoVeto
3 // Plugin Type: analyzer (art v3_05_01)
4 // File: CRTAutoVeto_module.cc
5 //
6 // Generated at Sat Jun 13 10:33:50 2020 by Christopher Hilgenberg using cetskelgen
7 // from cetlib version v3_10_00.
8 ////////////////////////////////////////////////////////////////////////
9 
10 //framework includes
11 #include "art/Framework/Core/EDAnalyzer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "art/Framework/Principal/Handle.h"
15 #include "art/Framework/Principal/Run.h"
16 #include "art/Framework/Principal/SubRun.h"
17 #include "canvas/Utilities/InputTag.h"
18 #include "fhiclcpp/ParameterSet.h"
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 #include "art_root_io/TFileService.h"
21 #include "art/Framework/Services/Registry/ServiceHandle.h"
22 
23 //larsoft includes
24 #include "nusimdata/SimulationBase/GTruth.h"
25 #include "nusimdata/SimulationBase/MCTruth.h"
26 #include "nusimdata/SimulationBase/MCParticle.h"
28 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
30 
31 //C++ includes
32 #include <vector>
33 #include <map>
34 #include <string>
35 #include <utility>
36 
37 //ROOT includes
38 #include <TH1F.h>
39 #include <TH2F.h>
40 #include <TTree.h>
41 #include <TVector3.h>
42 #include <TEfficiency.h>
43 
44 //local includes
48 
49 
50 namespace icarus {
51  namespace crt {
52  class CRTAutoVeto;
53  }
54 }
55 
56 using namespace icarus::crt;
57 using std::vector;
58 using std::map;
59 
60 class icarus::crt::CRTAutoVeto : public art::EDAnalyzer {
61 
62  public:
63 
64  explicit CRTAutoVeto(fhicl::ParameterSet const& p);
65  // The compiler-generated destructor is fine for non-base
66  // classes without bare pointers or other resource use.
67 
68  // Plugins should not be copied or assigned.
69  CRTAutoVeto(CRTAutoVeto const&) = delete;
70  CRTAutoVeto(CRTAutoVeto&&) = delete;
71  CRTAutoVeto& operator=(CRTAutoVeto const&) = delete;
72  CRTAutoVeto& operator=(CRTAutoVeto&&) = delete;
73 
74  // Required functions.
75  void analyze(art::Event const& e) override;
76 
77  private:
78 
79  bool IsAV(TVector3 const& point);
80  bool IsFV(TVector3 const& point);
81 
82  //config vars
83  art::InputTag fGenLabel;
84  art::InputTag fSimLabel;
85  art::InputTag fCRTHitLabel;
86 
87  //define fiducial volume
88  double fFidXOut; //distance from outer drift LAr face, outer TPCs [cm]
89  double fFidXIn; //distance from outer drift LAr face, inner TPCs [cm]
90  double fFidYTop; //distance from top active LAr face [cm]
91  double fFidYBot; //distance from bottom active LAr face [cm]
92  double fFidZUp; //distance from upstream active LAr face [cm]
93  double fFidZDown; //distance from downstream active LAr face [cm]
94 
95  //utilities
96  CRTBackTracker* bt; //CRTBackTracker extracts truth info from CRT hits
97  CRTCommonUtils* crtutil; //CRT related utilities
98  geo::GeometryCore const* fGeoService; //access geometry
99 
100  TEfficiency* fVetoEffAV; //veto eff. vs. nu energy (vertex in AV)
101  TEfficiency* fVetoEffFV; //veto eff. vs. nu energy (vertex in FV)
102  TEfficiency* fVetoEffAV_tot;
103  TEfficiency* fVetoEffFV_tot;
104 
105  //tree
106  TTree* fTree; //pointer to analysis tree
107  int fEvent; //art event number
108  int fNumNu; //number of neutrinos in this event
109  int fNuPDG; //neutrino PDG code
110  float fNuE; //nu energy [GeV]
111  bool fNuCC; //is the nu interaction CC?
112  bool fNuAV; //was nu vertex in active volume?
113  bool fNuFV; //was nu vertex in fiducial volume?
114  vector<double> fNuXYZT; //nu vertex position
115  int fNuInt; //nu interaction code
116  int fNuMode; //nu interaction mode
117  double fNuLepE; //outgoing lepton energy
118  double fNuTheta; //angle of outgoing lepton w.r.t. beam axis
119  int fNHit; //number of CRTHits
120  vector<int> fHitRegs; //region code of each hit
121  vector<vector<float>> fHitXYZT; //position/time of each hit vector<{x,y,z,t}>
122  vector<vector<int>> fHitPDGs; //PDG code of all particles generating a CRTHit
123  vector<int> fNHitPDGs; //number of particles contributing to CRT hit
124  vector<int> fHitMaxPDG; //PDG code of particle contrubuting most energy to CRTHit
125  vector<float> fDist; //distance between true vertex and CRTHit
126  vector<float> fDelay; //delay between true nu time and CRTHit time
127 };//end class
128 
129 
130 //constructor
131 CRTAutoVeto::CRTAutoVeto(fhicl::ParameterSet const& p)
132  : EDAnalyzer{p} ,
133  fGenLabel(p.get<art::InputTag>("GenLabel","generator")),
134  fSimLabel(p.get<art::InputTag>("SimLabel","largeant")),
135  fCRTHitLabel(p.get<art::InputTag>("CRTHitLabel","crthit")),
136  fFidXOut(p.get<double>("FiducialXOuter", 25.0)),
137  fFidXIn(p.get<double>("FiducialXInner", 0.0)),
138  fFidYTop(p.get<double>("FiducialYTop", 25.0)),
139  fFidYBot(p.get<double>("FiducialYBottom",25.0)),
140  fFidZUp(p.get<double>("FiducialZUpstream",30.0)),
141  fFidZDown(p.get<double>("FiducialZDownstream",50.0)),
142  bt(new CRTBackTracker(p.get<fhicl::ParameterSet>("CRTBackTrack"))),
143  crtutil(new CRTCommonUtils())
144 {
145  fGeoService = lar::providerFrom<geo::Geometry>();
146 
147  // Access ART's TFileService, which will handle creating and writing
148  // histograms and n-tuples for us.
149  art::ServiceHandle<art::TFileService> tfs;
150 
151  Double_t bins[14] = {0.2,0.35,0.5,0.65,0.8,0.95,1.1,1.3,1.5,1.75,2.,3.,5.,10.};
152  fVetoEffAV = new TEfficiency("effAV","#nu Veto Efficiency (AV)",13,bins);
153  fVetoEffFV = new TEfficiency("effFV","#nu Veto Efficiency (FV)",13,bins);
154  fVetoEffAV_tot = new TEfficiency("effAVTot","#nu Veto Efficiency (AV)",1,0,2);
155  fVetoEffFV_tot = new TEfficiency("effFVTot","#nu Veto Efficiency (FV)",1,0,2);
156 
157  //Tree
158  fTree = tfs->make<TTree>("VetoTree", "auto-veto info");
159  fTree->Branch("event", &fEvent, "event/I");
160  fTree->Branch("numNu", &fNumNu, "numNu/I");
161  fTree->Branch("nuPDG", &fNuPDG, "nuPDG/I");
162  fTree->Branch("nuE", &fNuE, "nuE/F");
163  fTree->Branch("cc", &fNuCC, "cc/O");
164  fTree->Branch("av", &fNuAV, "av/O");
165  fTree->Branch("fv", &fNuFV, "fv/O");
166  fTree->Branch("nuXYZT", &fNuXYZT);
167  fTree->Branch("nuLepE", &fNuLepE, "nuLepE/D");
168  fTree->Branch("nuTheta", &fNuTheta,"nuTheta/D");
169  fTree->Branch("nuMode", &fNuMode, "nuMode/I");
170  fTree->Branch("nuInt", &fNuInt, "nuInt/I");
171  fTree->Branch("nHit", &fNHit, "numHit/I");
172  fTree->Branch("hitReg", &fHitRegs);
173  fTree->Branch("hitXYZT", &fHitXYZT);
174  fTree->Branch("hitPDG", &fHitPDGs);
175  fTree->Branch("nHitPDG", &fNHitPDGs);
176  fTree->Branch("hitMaxPDG",&fHitMaxPDG);
177  fTree->Branch("dist", &fDist);
178  fTree->Branch("delay", &fDelay);
179 
180 }
181 
182 void CRTAutoVeto::analyze(art::Event const& ev)
183 {
184 
185  auto const& mctruths = //vector of MCTruths from GENIE
186  *ev.getValidHandle<vector<simb::MCTruth>>(fGenLabel);
187 
188  auto const& simparticles = //vector of MCParticles from G4
189  *ev.getValidHandle<vector<simb::MCParticle>>(fSimLabel);
190 
191  auto const& crthits = //vector of CRTHits
192  *ev.getValidHandle<vector<sbn::crt::CRTHit>>(fCRTHitLabel);
193 
194  map< int, const simb::MCParticle*> particleMap;
195 
196  fEvent = ev.id().event();
197  fNumNu = mctruths.size();
198  fNHit = crthits.size();
199 
200  //loop over neutrinos
201  for(auto const& mctruth : mctruths) {
202 
203  auto const& nu = mctruth.GetNeutrino();
204 
205  fNuPDG = nu.Nu().PdgCode();
206  fNuE = nu.Nu().E();
207  fNuMode = nu.Mode();
208  fNuInt = nu.InteractionType();
209  fNuLepE = nu.Lepton().E();
210  fNuTheta = nu.Theta();
211 
212  fNuCC = false;
213  if(nu.CCNC()==0)
214  fNuCC = true;
215 
216  const TLorentzVector nuxyzt = mctruth.GetNeutrino().Nu().Position(0);
217  const TVector3 point = nuxyzt.Vect();
218  fNuXYZT = {nuxyzt.X(), nuxyzt.Y(), nuxyzt.Z(), nuxyzt.T()};
219  fNuAV = IsAV(point);
220  fNuFV = false;
221  if(fNuAV)
222  fNuFV = IsFV(point);
223 
224  }//for nus
225 
226  //loop over G4 tracks
227  for(auto const& particle : simparticles){
228 
229  particleMap[particle.TrackId()] = &particle;
230 
231  }//G4 tracks
232 
233  fHitRegs.clear();
234  fHitXYZT.clear();
235  fHitPDGs.clear();
236  fNHitPDGs.clear();
237  fHitMaxPDG.clear();
238  fDist.clear();
239  fDelay.clear();
240 
241  //loop over CRTHits
242  for(auto const& hit : crthits) {
243  std::cout << "found hit in region, " << hit.tagger << std::endl;
244  fHitRegs.push_back(crtutil->AuxDetRegionNameToNum(hit.tagger));
245  fHitXYZT.push_back({hit.x_pos,hit.y_pos,hit.z_pos,(float)(hit.ts0_ns-1.6e6)});
246  fHitPDGs.push_back({});
247 
248  int npdg=0;
249  for(const int id : bt->AllTrueIds(ev,hit)){
250  if(id<0) continue;
251  if(particleMap.find(id)==particleMap.end()) continue;
252  fHitPDGs.back().push_back(particleMap[id]->PdgCode());
253  npdg++;
254  }
255 
256  fNHitPDGs.push_back(npdg);
257  int maxid = bt->TrueIdFromTotalEnergy(ev,hit);
258  if(maxid<0 || particleMap.find(maxid)==particleMap.end())
259  fHitMaxPDG.push_back(INT_MAX);
260  else
261  fHitMaxPDG.push_back(particleMap[maxid]->PdgCode());
262 
263  float dist=0.;
264  for(int i=0; i<3; i++) dist+=pow(fHitXYZT.back()[i]-fNuXYZT[i],2);
265  fDist.push_back(sqrt(dist));
266  fDelay.push_back(fHitXYZT.back()[3]-fNuXYZT[3]);
267  }//for CRTHits
268 
269  fTree->Fill();
270 
271  //fill efficiency plots
272  if(fNuCC && fNuAV && fNumNu>0){
273  bool veto = false;
274  if(fNHit>0)
275  veto = true;
276  fVetoEffAV->Fill(veto,fNuE);
277  fVetoEffAV_tot->Fill(veto,1);
278  if(fNuFV){
279  fVetoEffFV->Fill(veto,fNuE);
280  fVetoEffFV_tot->Fill(veto,1);
281  }
282  }
283 
284 }//analyze
285 
286 bool CRTAutoVeto::IsAV(TVector3 const& point){
287 
288  geo::CryostatGeo const& cryo0 = fGeoService->Cryostat(0);
289  geo::CryostatGeo const& cryo1 = fGeoService->Cryostat(1);
290  geo::TPCGeo const& tpc00 = cryo0.TPC(0);
291  geo::TPCGeo const& tpc01 = cryo0.TPC(1);
292  geo::TPCGeo const& tpc10 = cryo1.TPC(0);
293  geo::TPCGeo const& tpc11 = cryo1.TPC(1);
294 
295  if(tpc00.ContainsPosition(point) ||
296  tpc01.ContainsPosition(point) ||
297  tpc10.ContainsPosition(point) ||
298  tpc11.ContainsPosition(point) )
299  return true;
300 
301  return false;
302 }
303 
304 bool CRTAutoVeto::IsFV(TVector3 const& point) {
305 
306  geo::CryostatGeo const& cryo0 = fGeoService->Cryostat(0);
307  geo::CryostatGeo const& cryo1 = fGeoService->Cryostat(1);
308  geo::TPCGeo const& tpc00 = cryo0.TPC(0);
309  geo::TPCGeo const& tpc01 = cryo0.TPC(1);
310  geo::TPCGeo const& tpc10 = cryo1.TPC(0);
311  geo::TPCGeo const& tpc11 = cryo1.TPC(1);
312 
313  if(tpc00.ContainsPosition(point)
314  && tpc00.InFiducialX(point.X(),fFidXOut,0.)
315  && tpc00.InFiducialY(point.Y(),fFidYBot,fFidYTop)
316  && tpc00.InFiducialZ(point.Z(),fFidZUp,fFidZDown) )
317  return true;
318  if(tpc01.ContainsPosition(point)
319  && tpc01.InFiducialX(point.X(),0.,fFidXIn)
320  && tpc01.InFiducialY(point.Y(),fFidYBot,fFidYTop)
321  && tpc01.InFiducialZ(point.Z(),fFidZUp,fFidZDown) )
322  return true;
323  if(tpc10.ContainsPosition(point)
324  && tpc10.InFiducialX(point.X(),fFidXIn,0.)
325  && tpc10.InFiducialY(point.Y(),fFidYBot,fFidYTop)
326  && tpc10.InFiducialZ(point.Z(),fFidZUp,fFidZDown) )
327  return true;
328  if(tpc11.ContainsPosition(point)
329  && tpc11.InFiducialX(point.X(),0.,fFidXOut)
330  && tpc11.InFiducialY(point.Y(),fFidYBot,fFidYTop)
331  && tpc11.InFiducialZ(point.Z(),fFidZUp,fFidZDown) )
332  return true;
333 
334  return false;
335 }
336 
337 DEFINE_ART_MODULE(CRTAutoVeto)
process_name opflashCryo1 flashfilter analyze
bool IsFV(TVector3 const &point)
Utilities related to art service access.
bool IsAV(TVector3 const &point)
void analyze(art::Event const &e) override
pdgs p
Definition: selectors.fcl:22
Geometry information for a single TPC.
Definition: TPCGeo.h:38
vector< vector< int > > fHitPDGs
process_name hit
Definition: cheaterreco.fcl:51
Geometry information for a single cryostat.
Definition: CryostatGeo.h:43
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
geo::GeometryCore const * fGeoService
CryostatGeo const & Cryostat(geo::CryostatID const &cryoid) const
Returns the specified cryostat.
Description of geometry of one entire detector.
CRTAutoVeto(fhicl::ParameterSet const &p)
bool InFiducialY(double y, double neg_margin, double pos_margin) const
Returns whether TPC fiducial volume contains world y coordinate.
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
const TPCGeo & TPC(unsigned int itpc) const
Return the itpc&#39;th TPC in the cryostat.
Definition: CryostatGeo.cxx:93
do i e
std::vector< int > AllTrueIds(const art::Event &event, const CRTData &data)
int TrueIdFromTotalEnergy(const art::Event &event, const CRTData &data)
vector< vector< float > > fHitXYZT
art::ServiceHandle< art::TFileService > tfs
bool InFiducialX(double x, double neg_margin, double pos_margin) const
Returns whether TPC fiducial volume contains world x coordinate.
bool InFiducialZ(double z, double neg_margin, double pos_margin) const
Returns whether TPC fiducial volume contains world z coordinate.
process_name crt
BEGIN_PROLOG SN nu
bool ContainsPosition(geo::Point_t const &point, double wiggle=1.0) const
Returns whether this volume contains the specified point.
art framework interface to geometry description
BEGIN_PROLOG could also be cout