All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpHitAna_module.cc
Go to the documentation of this file.
1 // Christie Chiu and Ben Jones, MIT, 2012
2 //
3 // This is an analyzer module which writes the raw optical
4 // detector pulses for each PMT to an output file
5 //
6 
7 // LArSoft includes
11 
12 // Framework includes
13 #include "art/Framework/Core/EDAnalyzer.h"
14 #include "art/Framework/Core/ModuleMacros.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "art/Framework/Principal/Handle.h"
17 #include "art/Framework/Services/Registry/ServiceHandle.h"
18 #include "art_root_io/TFileService.h"
19 #include "canvas/Persistency/Common/Ptr.h"
20 #include "fhiclcpp/ParameterSet.h"
21 #include "messagefacility/MessageLogger/MessageLogger.h"
22 
23 // ROOT includes
24 #include "TH1D.h"
25 #include "TLorentzVector.h"
26 #include "TTree.h"
27 
28 // C++ Includes
29 #include "math.h"
30 
31 namespace opdet {
32 
33  class OpHitAna : public art::EDAnalyzer{
34  public:
35 
36  // Standard constructor and destructor for an ART module.
37  OpHitAna(const fhicl::ParameterSet&);
38 
39  // The analyzer routine, called once per event.
40  void analyze (const art::Event&);
41 
42  private:
43 
44  // The stuff below is the part you'll most likely have to change to
45  // go from this custom example to your own task.
46 
47  // The parameters we'll read from the .fcl file.
48  std::string fInputModule; // Input tag for OpDet collection
49  float fSampleFreq; // in MHz
50  float fTimeBegin; // in us
51  float fTimeEnd; // in us
52  // short fPEheight; // in ADC counts
53 
54  // Flags to enable or disable output of debugging TH1 / TH2s
58 
59  // Output TTree and its branch variables
60  TTree * fHitTree;
61  Int_t fEventID;
62  Int_t fOpChannel;
63  Float_t fPeakTime;
64  Float_t fNPe;
65 
66 
67  };
68 
69 }
70 
71 
72 // OpHitAna_module.cc
73 
74 // This is a short program required by the ART framework. It enables
75 // a program (OpHitAna, in this case) to be called as a module
76 // from a .fcl file. It is unlikely that you'll have to make any
77 // changes to this file.
78 
79 namespace opdet {
80  DEFINE_ART_MODULE(OpHitAna)
81 }
82 
83 namespace opdet {
84 
85  //-----------------------------------------------------------------------
86  // Constructor
87  OpHitAna::OpHitAna(fhicl::ParameterSet const& pset)
88  : EDAnalyzer(pset)
89  {
90 
91  // Indicate that the Input Module comes from .fcl
92  fInputModule = pset.get<std::string>("InputModule");
93 
94  art::ServiceHandle<OpDigiProperties> odp;
95  fTimeBegin = odp->TimeBegin();
96  fTimeEnd = odp->TimeEnd();
97  fSampleFreq = odp->SampleFreq();
98 
99 
100  fMakeHistPerChannel = pset.get<bool>("MakeHistPerChannel");
101  fMakeHistAllChannels = pset.get<bool>("MakeHistAllChannels");
102  fMakeHitTree = pset.get<bool>("MakeHitTree");
103 
104  // If required, make TTree for output
105 
106  if(fMakeHitTree)
107  {
108  art::ServiceHandle<art::TFileService const> tfs;
109  fHitTree = tfs->make<TTree>("HitTree","HitTree");
110  fHitTree->Branch("EventID", &fEventID, "EventID/I");
111  fHitTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
112  fHitTree->Branch("PeakTime", &fPeakTime, "PeakTime/F");
113  fHitTree->Branch("NPe", &fNPe, "NPe/F");
114  }
115 
116 
117  }
118 
119  //-----------------------------------------------------------------------
120  void OpHitAna::analyze(const art::Event& evt)
121  {
122 
123  // Create a handle for our vector of pulses
124  art::Handle< std::vector< recob::OpHit > > HitHandle;
125 
126  // Create string for histogram name
127  char HistName[50];
128 
129 
130  // Read in HitHandle
131  evt.getByLabel(fInputModule, HitHandle);
132 
133  // Access ART's TFileService, which will handle creating and writing
134  // histograms for us.
135  art::ServiceHandle<art::TFileService const> tfs;
136 
137  art::ServiceHandle<geo::Geometry const> geom;
138  int NOpChannels = geom->NOpChannels();
139 
140  std::vector<TH1D*> HitHist;
141 
142  sprintf(HistName, "Event %d AllOpDets", evt.id().event());
143 
144  TH1D * AllHits = nullptr;
146  {
147  AllHits = tfs->make<TH1D>(HistName, ";t (ns);",
148  int((fTimeEnd - fTimeBegin) * fSampleFreq),
149  fTimeBegin * 1000.,
150  fTimeEnd * 1000.);
151  }
152 
153  for(int i=0; i!=NOpChannels; ++i)
154  {
155 
156  sprintf(HistName, "Event %d OpDet %i", evt.id().event(), i);
157  if(fMakeHistPerChannel) HitHist.push_back ( tfs->make<TH1D>(HistName, ";t (ns);",
158  int((fTimeEnd - fTimeBegin) * fSampleFreq),
159  fTimeBegin * 1000.,
160  fTimeEnd * 1000.));
161 
162  }
163 
164 
165  fEventID = evt.id().event();
166 
167 
168  // For every OpHit in the vector
169  for(unsigned int i = 0; i < HitHandle->size(); ++i)
170  {
171  // Get OpHit
172  art::Ptr< recob::OpHit > TheHitPtr(HitHandle, i);
173  recob::OpHit TheHit = *TheHitPtr;
174 
175  fOpChannel = TheHit.OpChannel();
176  fNPe = TheHit.PE();
177  fPeakTime = TheHit.PeakTime();
178 
179  if(fMakeHitTree)
180  fHitTree->Fill();
181 
183  {
184  if(fOpChannel>int(HitHist.size()))
185  {
186  mf::LogError("OpHitAna")<<"Error : Trying to fill channel out of range, " << fOpChannel;
187  }
188  HitHist[fOpChannel]->Fill(fPeakTime,fNPe);
189  }
190 
191  if(fMakeHistAllChannels) AllHits->Fill(fPeakTime, fNPe);
192 
193  }
194 
195  }
196 
197 } // namespace opdet
double PeakTime() const
Definition: OpHit.h:88
double PE() const
Definition: OpHit.h:95
std::string fInputModule
void analyze(const art::Event &)
art::ServiceHandle< art::TFileService > tfs
TCEvent evt
Definition: DataStructs.cxx:8
int OpChannel() const
Definition: OpHit.h:86
OpHitAna(const fhicl::ParameterSet &)
art framework interface to geometry description