All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PhotBackground_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: PhotBackground
3 // Plugin Type: analyzer (art v3_05_01)
4 // File: PhotBackground_module.cc
5 //
6 // Generated at Tue Jul 14 12:07:00 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 
22 //LArSoft includes
23 #include "nusimdata/SimulationBase/MCParticle.h"
25 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
27 
28 //local includes
32 
33 //C++ includes
34 #include <vector>
35 #include <string>
36 #include <map>
37 
38 //ROOT includes
39 #include <TTree.h>
40 
41 namespace icarus {
42  class PhotBackground;
43 }
44 
45 using namespace icarus;
46 using std::vector;
47 using std::string;
48 using std::map;
49 
50 class icarus::PhotBackground : public art::EDAnalyzer {
51 public:
52  explicit PhotBackground(fhicl::ParameterSet const& p);
53  // The compiler-generated destructor is fine for non-base
54  // classes without bare pointers or other resource use.
55 
56  // Plugins should not be copied or assigned.
57  PhotBackground(PhotBackground const&) = delete;
58  PhotBackground(PhotBackground&&) = delete;
59  PhotBackground& operator=(PhotBackground const&) = delete;
60  PhotBackground& operator=(PhotBackground&&) = delete;
61 
62  // Required functions.
63  void analyze(art::Event const& e) override;
64 
65 private:
66 
67  int EnteredLar(const TLorentzVector& v, bool& iv, bool& fv);
68 
71 
72  geo::GeometryCore const* fGeometryService = lar::providerFrom<geo::Geometry>(); ///< pointer to Geometry provider
73  geo::CryostatGeo const& cryo0 = fGeometryService->Cryostat(0);
74  geo::CryostatGeo const& cryo1 = fGeometryService->Cryostat(1);
75 
76  geo::TPCGeo const& tpc00 = cryo0.TPC(0);
77  geo::TPCGeo const& tpc01 = cryo0.TPC(1);
78  geo::TPCGeo const& tpc10 = cryo1.TPC(0);
79  geo::TPCGeo const& tpc11 = cryo1.TPC(1);
80 
81 
82  TTree* fTree;
83  int fIcode; ///< end process code [-1->do not use; 0-> pair prod; 1-> compton]
84  double fStartPos[4]; ///< photon start x,y,z,t [cm/ns]
85  double fEndPos[4]; ///< photon end x,y,z,t [cm/ns]
86  double fStartE; ///< photon start energy [GeV]
87  double fEndE; ///< photon end energy [GeV]
88  int fStartReg; ///< region where photon was produced
89  int fEndReg; ///< region where photon interacted
90  bool fPhotIV;
91  bool fPhotAV;
92  vector<int> fMuRegs;
93  bool fMuIV;
94  bool fMuAV;
95  bool fMuTag;
96  int fMuNHit;
97  vector<vector<double>> fMuHitPos;
98  vector<double> fMuHitDx;
99  vector<double> fMuHitDt;
100 };
101 
102 
103 PhotBackground::PhotBackground(fhicl::ParameterSet const& p)
104  : EDAnalyzer{p},
105  bt(p.get<fhicl::ParameterSet>("CRTBackTrack")),
106  fCrtutils(new crt::CRTCommonUtils())
107 {
108  art::ServiceHandle<art::TFileService> tfs;
109 
110  fTree = tfs->make<TTree>("anatree","analysis tree for photon backgrounds");
111  fTree->Branch("icode", &fIcode, "icode/I");
112  fTree->Branch("startpos", fStartPos, "startpos[4]/D");
113  fTree->Branch("endpos", fEndPos, "endpos[4]/D");
114  fTree->Branch("starte", &fStartE, "starte/D");
115  fTree->Branch("ende", &fEndE, "ende/D");
116  fTree->Branch("startreg", &fStartReg,"startreg/I");
117  fTree->Branch("endreg", &fEndReg, "endreg/I");
118  fTree->Branch("photav", &fPhotAV, "photav/O");
119  fTree->Branch("photiv", &fPhotIV, "photiv/O");
120  fTree->Branch("regs", &fMuRegs);
121  fTree->Branch("muav", &fMuAV, "muav/O");
122  fTree->Branch("muiv", &fMuIV, "muiv/O");
123  fTree->Branch("mutag", &fMuTag, "mutag/O");
124  fTree->Branch("nhit", &fMuNHit, "nhit/I");
125  fTree->Branch("hitpos", &fMuHitPos);
126  fTree->Branch("dx", &fMuHitDx);
127  fTree->Branch("dt", &fMuHitDt);
128 }
129 
130 void PhotBackground::analyze(art::Event const& e)
131 {
132  // Define a "handle" to point to a vector of MCParticle objects.
133  art::Handle< vector<simb::MCParticle> > particleHandle;
134  e.getByLabel("largeant", particleHandle);
135  map<int,const simb::MCParticle*> idToMu;
136  vector<const simb::MCParticle*> photList;
137 
138  //true CRT hits
139  art::Handle< vector<sbn::crt::CRTHit> > trueHitHandle;
140  vector< art::Ptr<sbn::crt::CRTHit> > trueHitList;
141  map<int,vector<art::Ptr<sbn::crt::CRTHit>>> muToTrueHits;
142 
143  if( e.getByLabel("crttruehit",trueHitHandle) )
144  art::fill_ptr_vector(trueHitList,trueHitHandle);
145 
146  for(auto const& particle : *particleHandle) {
147 
148  if(abs(particle.PdgCode())==13){
149  idToMu[particle.TrackId()] = &particle;
150  }
151 
152  else if(particle.PdgCode()==22 && particle.Process()=="muBrems")
153  photList.push_back(&particle);
154 
155  else
156  continue;
157  }//for MCParticles
158 
159  for(auto const& hit : trueHitList){
160  for(const int id: bt.AllTrueIds(e,*hit)) {
161  if(idToMu.find(id)==idToMu.end())
162  continue;
163  muToTrueHits[id].push_back(hit);
164  }//for trackIDs
165  }//for CRTTrueHits
166 
167  for(auto const& phot : photList) {
168 
169  const TLorentzVector& positionStart = phot->Position();
170  const TLorentzVector& positionEnd = phot->EndPosition();
171 
172  fPhotIV = false;
173  fPhotAV = false;
174  fEndReg = EnteredLar(positionEnd, fPhotIV, fPhotAV);
175  if(fEndReg==-1)
176  continue;
177  bool iv = false, av = false;
178  fStartReg = EnteredLar(positionStart, iv, av);
179 
180  for(size_t i=0; i<4; i++) {
181  fStartPos[i] = positionStart[i];
182  fEndPos[i] = positionEnd[i];
183  }
184  fStartE = phot->E();
185  fEndE = phot->E(phot->NumberTrajectoryPoints()-2);
186 
187  fIcode = -1;
188  string endprocess = phot->EndProcess();
189  if(endprocess=="conv") fIcode = 0; //pair prod
190  else if(endprocess=="compt") fIcode = 1; //compton scatter
191  else if(endprocess=="phot") fIcode = 2; //photoelectric
192  else std::cout << "uknown end process: " << endprocess << std::endl;
193 
194 
195 
196  if(idToMu.find(phot->Mother())==idToMu.end())
197  throw cet::exception("PhotBackground") <<
198  "no match muon found for mubrems photons" << std::endl;
199 
200  fMuIV = false;
201  fMuAV = false;
202  fMuTag = false;
203  fMuNHit = 0;
204  fMuRegs.clear();
205  fMuHitPos.clear();
206  fMuHitDx.clear();
207  fMuHitDt.clear();
208 
209  if(muToTrueHits.find(phot->Mother())!=muToTrueHits.end()){
210  fMuTag = true;
211  fMuNHit=muToTrueHits[phot->Mother()].size();
212  for(auto const& hit : muToTrueHits[phot->Mother()]){
213  vector<double> xyzt = {hit->x_pos,hit->y_pos,hit->z_pos,(double)hit->ts0_ns};
214  fMuHitPos.push_back(xyzt);
215  double l = 0.;
216  for(int j=0; j<3; j++) l+=pow(xyzt[j]-fEndPos[j],2);
217  fMuHitDx.push_back(sqrt(l));
218  fMuHitDt.push_back((double)hit->ts0_ns-1.6e6-fEndPos[3]);
219  string reg = hit->tagger;
220  //fMuRegType.push_back(fCrtutils->GetRegTypeFromRegName(reg));
221  fMuRegs.push_back(fCrtutils->AuxDetRegionNameToNum(reg));
222  }
223  }
224 
225  for(size_t i=0; i<idToMu[phot->Mother()]->NumberTrajectoryPoints(); i++){
226 
227  const TLorentzVector& position = idToMu[phot->Mother()]->Position(i);
228  fMuRegs.push_back(EnteredLar(position, fMuIV, fMuAV));
229 
230  }//for traj pts
231 
232  std::sort(fMuRegs.begin(),fMuRegs.end());
233  fMuRegs.resize(std::distance(fMuRegs.begin(),std::unique(fMuRegs.begin(),fMuRegs.end())));
234 
235  fTree->Fill();
236 
237  }//for photons
238 
239 }//end analyze
240 
241 int PhotBackground::EnteredLar(const TLorentzVector& v, bool& iv, bool& av){
242 
243  double pos[3] = {v.X(),v.Y(),v.Z()};
244  int reg = -1;
245 
246  if(cryo0.ContainsPosition(pos)){
247 
248  if(tpc00.ContainsPosition(pos)) {
249  reg = 5;
250  av = true;
251  iv = false;
252  }
253  else if(tpc01.ContainsPosition(pos)) {
254  reg = 6;
255  av = true;
256  iv = false;
257  }
258  else {
259  reg = 10;
260  if(!av)iv = true;
261  }
262  }
263  else if(cryo1.ContainsPosition(pos)){
264 
265  if(tpc10.ContainsPosition(pos)){
266  reg = 7;
267  av = true;
268  iv = false;
269  }
270  else if(tpc11.ContainsPosition(pos)) {
271  reg = 8;
272  av = true;
273  iv = false;
274  }
275  else {
276  reg = 12;
277  if(!av)iv = true;
278  }
279  }
280 
281  return reg;
282 
283 }
284 
285 DEFINE_ART_MODULE(PhotBackground)
process_name opflashCryo1 flashfilter analyze
vector< vector< double > > fMuHitPos
Utilities related to art service access.
double fEndPos[4]
photon end x,y,z,t [cm/ns]
int EnteredLar(const TLorentzVector &v, bool &iv, bool &fv)
geo::CryostatGeo const & cryo1
pdgs p
Definition: selectors.fcl:22
Geometry information for a single TPC.
Definition: TPCGeo.h:38
double fStartPos[4]
photon start x,y,z,t [cm/ns]
process_name hit
Definition: cheaterreco.fcl:51
int fStartReg
region where photon was produced
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.
T abs(T value)
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
int fIcode
end process code [-1-&gt;do not use; 0-&gt; pair prod; 1-&gt; compton]
Description of geometry of one entire detector.
geo::CryostatGeo const & cryo0
PhotBackground(fhicl::ParameterSet const &p)
do i e
std::vector< int > AllTrueIds(const art::Event &event, const CRTData &data)
double fStartE
photon start energy [GeV]
art::ServiceHandle< art::TFileService > tfs
crt::CRTCommonUtils * fCrtutils
void analyze(art::Event const &e) override
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
double fEndE
photon end energy [GeV]
int fEndReg
region where photon interacted