All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRTEventProducer_module.cc
Go to the documentation of this file.
1 // Framework includes
2 #include "art/Framework/Core/EDProducer.h"
3 #include "art/Framework/Core/ModuleMacros.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "art/Framework/Principal/Event.h"
6 #include "canvas/Persistency/Common/Ptr.h"
7 #include "canvas/Persistency/Common/PtrVector.h"
8 #include "art/Framework/Principal/Run.h"
9 #include "art/Framework/Principal/SubRun.h"
10 #include "art_root_io/TFileService.h"
11 #include "art_root_io/TFileDirectory.h"
12 #include "art/Framework/Services/Registry/ServiceHandle.h"
13 #include "art/Framework/Core/ModuleMacros.h"
14 #include "canvas/Persistency/Common/FindManyP.h"
15 #include "art/Persistency/Common/PtrMaker.h"
16 #include "canvas/Utilities/InputTag.h"
17 #include "fhiclcpp/ParameterSet.h"
18 #include "messagefacility/MessageLogger/MessageLogger.h"
19 #include <memory>
20 #include <iostream>
21 #include <map>
22 #include <iterator>
23 #include <algorithm>
24 
25 // LArSoft
31 
32 // ROOT
33 #include "TTree.h"
34 #include "TFile.h"
35 #include "TH1D.h"
36 #include "TH2D.h"
37 #include "TVector3.h"
38 
41 
42 using std::string;
43 
44 namespace icarus {
45 namespace crt {
46 
47  class CRTEventProducer : public art::EDProducer {
48  public:
49 
50  explicit CRTEventProducer(fhicl::ParameterSet const & p);
51 
52  // The destructor generated by the compiler is fine for classes
53  // without bare pointers or other resource use.
54 
55  // Plugins should not be copied or assigned.
56  CRTEventProducer(CRTEventProducer const &) = delete;
58  CRTEventProducer & operator = (CRTEventProducer const &) = delete;
60 
61  // Required functions.
62  void produce(art::Event & e) override;
63 
64  // Selected optional functions.
65  void beginJob() override;
66 
67  void endJob() override;
68 
69  void reconfigure(fhicl::ParameterSet const & p);
70 
71  private:
72 
73  std::string fInFile;
74  float fTimeWindow;
76 
77  std::map<size_t,size_t> fEventEntryStart;
78  std::map<size_t,size_t> fEventEntryEnd;
79 
80  }; // class CRTEventProducer
81 
82  CRTEventProducer::CRTEventProducer(fhicl::ParameterSet const & p)
83  : EDProducer{p}
84  // Initialize member data here, if know don't want to reconfigure on the fly
85  {
86 
87  // Call appropriate produces<>() functions here.
88  produces< std::vector<icarus::crt::CRTData> >();
89 
90  reconfigure(p);
91 
92  /*std::cout << "opening file " << fInFile << std::endl;
93  TFile fin(fInFile.c_str());
94  TTree* tree=(TTree*)fin.FindObjectAny("anaTree");
95  if(tree==nullptr) std::cout << "anaTree not found!" << std::endl;
96  fAnaTree = new icarus::crt::CRTPreProcessTree(tree);
97  std::cout << "constructed CRTPreProcessTree with " << fAnaTree->GetNEntries() << " entries" << std::endl;
98  std::cout << "earliest timestamp: " << fAnaTree->GetAbsTime(0) << std::endl;
99 */
100  } // CRTEventProducer()
101 
102  void CRTEventProducer::reconfigure(fhicl::ParameterSet const & p)
103  {
104  //fCrtModuleLabel = (p.get<art::InputTag> ("CrtModuleLabel"));
105  fInFile = p.get<string>("infile");
106  fTimeWindow = p.get<float>("time_window");
107 
108  } // CRTEventProducer::reconfigure()
109 
111  {
112  std::cout << "opening file " << fInFile << std::endl;
113  TFile fin(fInFile.c_str());
114  TTree* tree=(TTree*)fin.FindObjectAny("anaTree");
115  if(tree==nullptr) std::cout << "anaTree not found!" << std::endl;
117 
118  std::cout << "running 'beginJob'" << std::endl;
119  size_t event=0;
120  uint64_t ti = fAnaTree->GetAbsTime(0);
121  fEventEntryStart[0] = 0;
122 
123  std::cout << "time slicing tree with " << fAnaTree->GetNEntries() << " entries" << std::endl;
124 
125  for(size_t ientry =0; ientry<fAnaTree->GetNEntries(); ientry++){
126  if(fAnaTree->GetAbsTime(ientry)>=ti &&
127  fAnaTree->GetAbsTime(ientry)<ti+fTimeWindow*1.0e9)
128  continue;
129 
130  else if(fAnaTree->GetAbsTime(ientry)>=ti+fTimeWindow*1.0e9 ||
131  ientry == fAnaTree->GetNEntries()-1){
132  ti += fTimeWindow*1.0e9;
133  fEventEntryEnd[event] = ientry-1;
134  event++;
135  if (ientry < fAnaTree->GetNEntries()-1)
136  fEventEntryStart[event] = ientry;
137 
138  }
139  }
140 
141  std::cout << "found " << event << " time slices" << std::endl;
142  if(fEventEntryEnd.size()!=fEventEntryStart.size())
143  std::cout << "event map start/end size mismatch!" << std::endl;
144 
145  fin.Close();
146  delete fAnaTree;
147 
148  } // CRTEventProducer::beginJob()
149 
150  void CRTEventProducer::produce(art::Event & event)
151  {
152  TFile fin(fInFile.c_str());
153  TTree* tree=(TTree*)fin.FindObjectAny("anaTree");
154  if(tree==nullptr) std::cout << "anaTree not found!" << std::endl;
156 
157  std::unique_ptr< std::vector<icarus::crt::CRTData> > crtdata( new std::vector<icarus::crt::CRTData>);
158 
159  size_t eveId = (size_t) event.event();
160  std::cout << "processing event " << eveId << " with " << fEventEntryEnd[eveId]+1-fEventEntryStart[eveId] << " triggers" << std::endl;
161 
162  int entry=0;
163  for(size_t ientry=fEventEntryStart[eveId]; ientry<=fEventEntryEnd[eveId]; ientry++){
164  fAnaTree->Load(ientry);
165  CRTData data;
166  data.fMac5 = fAnaTree->Mac5();
167  data.fTs0 = fAnaTree->GetAbsTime();
168  data.fTs1 = data.fTs0;
169  for(size_t ichan=0; ichan<32; ichan++){
170  if(fAnaTree->Above(ichan)&&fAnaTree->Active(ichan)) {
171  data.fAdc[ichan] = fAnaTree->PE(ichan);
172  }//if channel active and above threshold
173  else
174  data.fAdc[ichan] = 0;
175  }//for channels
176 
177  crtdata->push_back(data);
178  entry++;
179 
180  }//for entries in time slice
181 
182  delete fAnaTree;
183  fin.Close();
184 
185  event.put(std::move(crtdata));
186 
187  } // CRTEventProducer::produce()
188 
190  {
191 
192  } // CRTEventProducer::endJob()
193 
194  DEFINE_ART_MODULE(icarus::crt::CRTEventProducer)
195 
196 }
197 } //end namespace
uint8_t fMac5
FEB address following numbering convention common for both data and MC.
std::map< size_t, size_t > fEventEntryStart
CRTEventProducer(fhicl::ParameterSet const &p)
pdgs p
Definition: selectors.fcl:22
for pfile in ack l reconfigure(.*) override"` do echo "checking $
std::map< size_t, size_t > fEventEntryEnd
CRTEventProducer & operator=(CRTEventProducer const &)=delete
icarus::crt::CRTPreProcessTree * fAnaTree
void reconfigure(fhicl::ParameterSet const &p)
void Load(size_t ientry) const
uint64_t GetAbsTime(size_t ientry) const
Single hit (self trigger) of a CRT board.
Definition of data types for geometry description.
uint16_t fAdc[64]
ADC readout for each channel. CAEN (Bern) CRT FEBs use only indices 0-31.
void produce(art::Event &e) override
do i e
bool Above(uint8_t chan) const
bool Active(uint8_t chan) const
Collection of Physical constants used in LArSoft.
float PE(uint8_t chan) const
process_name crt
BEGIN_PROLOG could also be cout
uint64_t fTs0
Absolute hit timestamp [ns].
uint64_t fTs1
Trigger time, not well defined as of Apr 14, 2021.