SupernovaAna_module.cc
Go to the documentation of this file.
1  ////////////////////////////////////////////////////////////////////////
2 // Class: SupernovaAna
3 // Module Type: analyzer
4 // File: SupernovaAna_module.cc
5 //
6 // Generated at Mon Jul 11 21:36:48 2016 by Michael Baird using the old
7 // copy and paste...
8 ////////////////////////////////////////////////////////////////////////
9 
10 // C++ includes
11 
12 // ROOT includes
13 #include "TTree.h"
14 #include "TH1F.h"
15 
16 // Framework includes
23 #include "art_root_io/TFileDirectory.h"
24 #include "art_root_io/TFileService.h"
27 #include "fhiclcpp/ParameterSet.h"
29 
30 // DUNETPC specific includes
35 
36 // should this be somewhere else?
37 const int kMaxNumHits=50000;
38 const int kMaxNumParticles=500;
39 
40 class SupernovaAna;
41 
42 class SupernovaAna : public art::EDAnalyzer {
43 
44 public:
45 
46  explicit SupernovaAna(fhicl::ParameterSet const & p);
47 
48  // Plugins should not be copied or assigned.
49  SupernovaAna(SupernovaAna const &) = delete;
50  SupernovaAna(SupernovaAna &&) = delete;
51  SupernovaAna & operator = (SupernovaAna const &) = delete;
52  SupernovaAna & operator = (SupernovaAna &&) = delete;
53 
54  // The main guts...
55  void analyze(art::Event const & e) override;
56 
57  void reconfigure(fhicl::ParameterSet const & p);
58 
59  void beginJob() override;
60 
61 
62 
63 private:
64 
65  // label for the modules that made trigger objects
68 
69  // Define an ntuple to be filled
70  TTree* fNTuple;
71 
72  // Define variables to be used as branches of the nTuple
73  int EventNum;
75  int NHits;
76  double avgRMS;
78  double avgRMSloopsum;
79  double SummedADC;
80  int NMCTruths;
102 
103 };
104 
105 
106 
107 //......................................................
109  :
110  EDAnalyzer(p)
111 {
112  this->reconfigure(p);
113 }
114 
115 
116 
117 //......................................................
119 {
120  fTruthLabel = p.get<std::string> ("TruthLabel");
121  fHitLabel = p.get<std::string> ("HitLabel");
122 }
123 
124 
125 
126 //......................................................
128 {
129 
131 
132  // Make nTuple
133  fNTuple = tfs->make<TTree>("SupernovaAna","Supernonva analysis tree");
134 
135  // Add branches to nTuple
136  fNTuple->Branch("EventNum",&EventNum);
137  fNTuple->Branch("EventNumCut",&EventNumCut);
138  fNTuple->Branch("NHits",&NHits);
139  fNTuple->Branch("avgRMS",&avgRMS);
140  fNTuple->Branch("SummedADC",&SummedADC);
141  fNTuple->Branch("NMCTruths",&NMCTruths);
142  fNTuple->Branch("NParticles",NParticles,"NParticles[NMCTruths]/I");
143  fNTuple->Branch("MomentumYTruths",MomentumYTruths,"MomentumYTruths[NMCTruths]/D");
144  fNTuple->Branch("MomentumZTruths",MomentumZTruths,"MomentumZTruths[NMCTruths]/D");
145  fNTuple->Branch("NuEnergyTruths",NuEnergyTruths,"NuEnergyTruths[NMCTruths]/D");
146  fNTuple->Branch("LeptonEnergyTruths",LeptonEnergyTruths,"LeptonEnergyTruths[NMCTruths]/D");
147  fNTuple->Branch("ThetaTruths",ThetaTruths,"ThetaTruths[NMCTruths]/D");
148  fNTuple->Branch("OriginTruths",OriginTruths,"OriginTruths[NMCTruths]/I");
149  fNTuple->Branch("CCNCTruths",CCNCTruths,"CCNCTruths[NMCTruths]/I");
150  fNTuple->Branch("ModeTruths",ModeTruths,"ModeTruths[NMCTruths]/I");
151  fNTuple->Branch("NuPDGCodeTruths",NuPDGCodeTruths,"NuPDGCodeTruths[NMCTruths]/I");
152  fNTuple->Branch("LeptonPDGCodeTruths",LeptonPDGCodeTruths,"LeptonPDGCodeTruths[NMCTruths]/I");
153  fNTuple->Branch("xTruths",xTruths,"xTruths[NMCTruths]/D");
154  fNTuple->Branch("yTruths",yTruths,"yTruths[NMCTruths]/D");
155  fNTuple->Branch("zTruths",zTruths,"zTruths[NMCTruths]/D");
156  fNTuple->Branch("tTruths",tTruths,"tTruths[NMCTruths]/D");
157  fNTuple->Branch("EndxTruths",EndxTruths,"EndxTruths[NMCTruths]/D");
158  fNTuple->Branch("EndyTruths",EndyTruths,"EndyTruths[NMCTruths]/D");
159  fNTuple->Branch("EndzTruths",EndzTruths,"EndzTruths[NMCTruths]/D");
160  fNTuple->Branch("EndtTruths",EndtTruths,"EndtTruths[NMCTruths]/D");
161  fNTuple->Branch("DeltaXTruths",DeltaXTruths,"DeltaXTruths[NMCTruths]/D");
162  fNTuple->Branch("DeltaYTruths",DeltaYTruths,"DeltaYTruths[NMCTruths]/D");
163 
164 }
165 
166 
167 
168 //......................................................
170 {
171 
172 
173  //==============================HITS================================
174 
175  // Get the Hits out of the event
176  auto hits_list = e.getHandle< std::vector< recob::Hit > >(fHitLabel);
177 
178  // Gets the number of hits per event and fills into a histogram
179  NHits = hits_list->size();
180 
181  // Get summed ADC and average RMS of hit slope
182  SummedADC = 0.0;
183  for(unsigned int i = 0; i < hits_list->size();i++){
184  recob::Hit const& hit = hits_list->at(i);
185  avgRMSloop[i] = hit.RMS();
187  SummedADC += hit.SummedADC();
188  }
189  avgRMS = avgRMSloopsum/(hits_list->size());
190 
191  //============================MCTRUTHS==============================
192 
193  // Get the MCTruths out of the event
194  auto truths_list = e.getHandle< std::vector< simb::MCTruth > >(fTruthLabel);
195 
196  // Gets the number of MC truths per event and fills into a histogram
197  NMCTruths = truths_list->size();
198 
199  // Loop through the truth arrays and sets the values of the arrays
200  // to an arbitrary default (unfilled) value
201  for(int i=0;i<kMaxNumParticles;i++){
202  xTruths[i] = -1.0e9;
203  yTruths[i] = -1.0e9;
204  zTruths[i] = -1.0e9;
205  tTruths[i] = -1.0e9;
206  EndxTruths[i] = -1.0e9;
207  EndyTruths[i] = -1.0e9;
208  EndzTruths[i] = -1.0e9;
209  EndtTruths[i] = -1.0e9;
210  NParticles[i] = -999;
211  MomentumYTruths[i] = -1.0e9;
212  MomentumZTruths[i] = -1.0e9;
213  NuEnergyTruths[i] = -999;
214  LeptonEnergyTruths[i] = -999;
215  ThetaTruths[i] = -999;
216  OriginTruths[i] = -999;
217  CCNCTruths[i] = -999;
218  ModeTruths[i] = -999;
219  NuPDGCodeTruths[i] = 0;
220  LeptonPDGCodeTruths[i] = 0;
221  DeltaXTruths[i] = -1.0e9;
222  DeltaYTruths[i] = -1.0e9;
223 
224  }
225 
226  // Detect if the number of MC truths per event is greater than
227  // or equal to the maximum number of particles per event else
228  // we'll fall off the end of the array and cause a seg fault
229  if(NMCTruths >= kMaxNumParticles) {
230  std::cerr << "ERROR: NMCTruths " << NMCTruths <<
231  " >= kMaxNumParticles " << kMaxNumParticles <<
232  " , will cause a segmentation fault" << std::endl;}
233  // Detect if the number of hits per event is greater than
234  // or equal to the maximum number of particles per event else
235  // we'll fall off the end of the array and cause a seg fault
236  if(NHits >= kMaxNumHits) {
237  std::cerr << "ERROR: NHits " << NHits <<
238  " >= kMaxNumHits " << kMaxNumHits <<
239  " , will cause a segmentation fault" << std::endl;}
240 
241  // Create nTuple data for any number of truths
242  for(unsigned int i = 0; i < truths_list->size();i++){
243  simb::MCTruth const& truth = truths_list->at(i);
244 
245  NParticles[i] = truth.NParticles();
246 
247  // Get the ith particle's Origin truths
248  OriginTruths[i] = truth.Origin();
249 
250  // Only get neutrino truths if they exist to avoid a segmentation fault
251  if (truth.NeutrinoSet()){
252  simb::MCNeutrino const& mc_neutrino = truth.GetNeutrino();
253  // should change notation such that mc_neutrino = truth.GetNeutrino().Nu(); // and then remove .Nu() when called
254  simb::MCParticle const& mc_lepton = mc_neutrino.Lepton();
255 
256  // Get data to be filled into nTuple
257  xTruths[i] = mc_lepton.Vx();
258  yTruths[i] = mc_lepton.Vy();
259  zTruths[i] = mc_lepton.Vz();
260  tTruths[i] = mc_lepton.T();
261  EndxTruths[i] = mc_lepton.EndX();
262  EndyTruths[i] = mc_lepton.EndY();
263  EndzTruths[i] = mc_lepton.EndZ();
264  EndtTruths[i] = mc_lepton.EndT();
265  MomentumYTruths[i] = mc_lepton.Py();
266  MomentumZTruths[i] = mc_lepton.Pz();
267  NuEnergyTruths[i] = mc_neutrino.Nu().E();
268  LeptonEnergyTruths[i] = mc_lepton.E();
269  ThetaTruths[i] = mc_neutrino.Theta();
270  CCNCTruths[i] = mc_neutrino.CCNC();
271  ModeTruths[i] = mc_neutrino.Mode();
272  NuPDGCodeTruths[i] = mc_neutrino.Nu().PdgCode();
273  LeptonPDGCodeTruths[i] = mc_lepton.PdgCode();
274  DeltaXTruths[i] = EndxTruths[i] - xTruths[i];
275  DeltaYTruths[i] = EndyTruths[i] - yTruths[i];
276  }
277  }
278 
279  art::EventNumber_t event = e.id().event();
280  EventNum = event;
281  if (NHits<2) {
282  EventNumCut = event;
283  }
284 
285 
286  // Now we should have NMCTruths = truths_list->size() and
287  // PDGCodeTruths [0 - (NMCTruths - 1)] should contain PdgCodes,
288  // [ NMCTruths - 500 ] should be equal to -999, signifying that
289  // we did not fill this part
290 
291  // Fill nTuple
292  fNTuple->Fill();
293 }
double E(const int i=0) const
Definition: MCParticle.h:233
double NuEnergyTruths[kMaxNumParticles]
int PdgCode() const
Definition: MCParticle.h:212
int CCNC() const
Definition: MCNeutrino.h:148
const int kMaxNumHits
double Theta() const
angle between incoming and outgoing leptons, in radians
Definition: MCNeutrino.cxx:63
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
double Py(const int i=0) const
Definition: MCParticle.h:231
double EndZ() const
Definition: MCParticle.h:228
int LeptonPDGCodeTruths[kMaxNumParticles]
int OriginTruths[kMaxNumParticles]
double avgRMSloop[kMaxNumHits]
SupernovaAna & operator=(SupernovaAna const &)=delete
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
float RMS() const
RMS of the hit shape, in tick units.
Definition: Hit.h:220
double DeltaYTruths[kMaxNumParticles]
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
simb::Origin_t Origin() const
Definition: MCTruth.h:74
SupernovaAna(fhicl::ParameterSet const &p)
double zTruths[kMaxNumParticles]
int NParticles() const
Definition: MCTruth.h:75
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
double yTruths[kMaxNumParticles]
double EndY() const
Definition: MCParticle.h:227
double MomentumYTruths[kMaxNumParticles]
int CCNCTruths[kMaxNumParticles]
int ModeTruths[kMaxNumParticles]
double ThetaTruths[kMaxNumParticles]
const double e
const simb::MCParticle & Lepton() const
Definition: MCNeutrino.h:147
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
const int kMaxNumParticles
void reconfigure(fhicl::ParameterSet const &p)
double xTruths[kMaxNumParticles]
T get(std::string const &key) const
Definition: ParameterSet.h:271
int NuPDGCodeTruths[kMaxNumParticles]
double T(const int i=0) const
Definition: MCParticle.h:224
double EndtTruths[kMaxNumParticles]
double EndxTruths[kMaxNumParticles]
p
Definition: test.py:223
std::string fTruthLabel
double EndzTruths[kMaxNumParticles]
double EndT() const
Definition: MCParticle.h:229
void beginJob() override
Detector simulation of raw signals on wires.
double LeptonEnergyTruths[kMaxNumParticles]
double Vx(const int i=0) const
Definition: MCParticle.h:221
Declaration of signal hit object.
int NParticles[kMaxNumParticles]
void analyze(art::Event const &e) override
double DeltaXTruths[kMaxNumParticles]
double Pz(const int i=0) const
Definition: MCParticle.h:232
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:118
double Vz(const int i=0) const
Definition: MCParticle.h:223
float SummedADC() const
The sum of calibrated ADC counts of the hit (0. by default)
Definition: Hit.h:223
EventNumber_t event() const
Definition: EventID.h:116
std::string fHitLabel
double MomentumZTruths[kMaxNumParticles]
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
bool NeutrinoSet() const
Definition: MCTruth.h:78
Event generator information.
Definition: MCTruth.h:32
double EndX() const
Definition: MCParticle.h:226
double EndyTruths[kMaxNumParticles]
double tTruths[kMaxNumParticles]
Event generator information.
Definition: MCNeutrino.h:18
int Mode() const
Definition: MCNeutrino.h:149
EventID id() const
Definition: Event.cc:34
double Vy(const int i=0) const
Definition: MCParticle.h:222
QTextStream & endl(QTextStream &s)