ProtoDUNEUnstableHVFilter_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // ProtoDUNEUnstableHVFilter class
4 //
5 // author: Owen Goodwin
6 // email: owen.goodwin@manchester.ac.uk
7 //
8 // - A filter to reject events between given start and end times of unstable HV periods. Using raw decoder timestamp.
9 //
10 // - All dates and times should be in unix time (UTC)
11 //
12 ////////////////////////////////////////////////////////////////////////
13 // C++
14 extern "C" {
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 }
18 #include <math.h>
19 #include <algorithm>
20 #include <iostream>
21 #include <fstream>
22 // ROOT
23 #include "TMath.h"
24 #include "TTimeStamp.h"
25 
26 #include "TH1.h"
27 #include "TFile.h"
28 /// Framework
32 #include "fhiclcpp/ParameterSet.h"
37 #include "art_root_io/TFileService.h"
38 #include "art_root_io/TFileDirectory.h"
40 
43 ///filters for events, etc
44 namespace filter {
46  public:
48  virtual ~ProtoDUNEUnstableHVFilter();
49  uint64_t GetRawDecoderInfo(art::Event & e);
50  bool filter(art::Event& evt);
51  void beginJob();
52  void endJob();
53  void reconfigure(fhicl::ParameterSet const& p);
54  private:
55 
56  std::vector<std::pair<UInt_t,UInt_t>> fTimeRanges;
57  UInt_t fTimeRangeLow;
59  bool fDebug;
60  long long RDTSTime;
61  double RDTSTimeSec;
64 
65 
67  TH1D* fTotalEvents;
68 
69 
70  }; //class ProtoDUNEUnstableHVFilter
71 }
73 
75  fSelectedEvents = tfs->make<TH1D>("fSelectedEvents", "Number of Selected Events", 3, 0, 3); //counts the number of selected events
76  fTotalEvents = tfs->make<TH1D>("fTotalEvents", "Total Events", 3, 0, 3); //counts the initial number of events in the unfiltered root input file
77 }
79 
80 
82  fTimeRanges = p.get<std::vector <std::pair<UInt_t,UInt_t >>>("TimeRanges");
83  fDebug = p.get<int>("Debug");
84 
85 }
86 
87 
89 : EDFilter(pset) {
90  this->reconfigure(pset);
91 }
93 
94 
96  MF_LOG_INFO("BeamEvent") << "\n";
97  MF_LOG_INFO("BeamEvent") << "Getting Raw Decoder Info" << "\n";
98 
99  art::InputTag itag1("timingrawdecoder","daq");
100  RDTimeStampHandle = e.getHandle< std::vector<raw::RDTimeStamp> >(itag1);
101  MF_LOG_INFO("BeamEvent") << "RDTS valid? " << RDTimeStampHandle.isValid() << "\n";
102  for (auto const & RDTS : *RDTimeStampHandle){
103  MF_LOG_INFO("BeamEvent") << "High: " << RDTS.GetTimeStamp_High() << "\n";
104  MF_LOG_INFO("BeamEvent") << "Low: " << RDTS.GetTimeStamp_Low() << "\n";
105 
106  uint64_t high = RDTS.GetTimeStamp_High();
107  uint64_t low = RDTS.GetTimeStamp_Low();
108 
109  high = high << 32;
110  uint64_t joined = (high | low);
111 
112  MF_LOG_INFO("BeamEvent") << "Raw Decoder Timestamp: " << joined << "\n";
113 
114  RDTSTime = joined;
115  RDTSTrigger = RDTS.GetFlags();
116  MF_LOG_INFO("BeamEvent") << "Trigger: " << RDTSTrigger << "\n";
117 
118  //Separates seconds portion of the ticks
119  //From the nanoseconds
120  long long RDTSTickSec = (RDTSTime * 2) / (int)(TMath::Power(10,8));
121  RDTSTickSec = RDTSTickSec * (int)(TMath::Power(10,8)) / 2;
122  //long long RDTSTickNano = RDTSTime - RDTSTickSec;
123 
124  //Units are 20 nanoseconds ticks
125  RDTSTimeSec = 20.e-9 * RDTSTickSec;
126  //RDTSTimeNano = 20. * RDTSTickNano;
127 
128 
129  }
130  return RDTSTimeSec;
131 }
132 
134 
135 
136  fTotalEvents->Fill(1);
137 
138 
139 
140  if(!evt.isRealData()){
141  fSelectedEvents->Fill(1);
142  return true; //Filter is designed for Data only. Don't want to filter on MC
143  }
144 
145 
146 
147  const std::string myname = "ProtoDUNEUnstableHVFilter::filter: ";
148  bool keep = true;
149  TTimeStamp * evtTTS;
150  evtTTS = new TTimeStamp(GetRawDecoderInfo(evt));
151  // if (evtTime.timeHigh() == 0) { evtTTS = new TTimeStamp(evtTime.timeLow()); }
152  // else { evtTTS = new TTimeStamp(evtTime.timeHigh(), evtTime.timeLow()); }
153  if (fDebug) std::cout << "Event time: " << evtTTS -> AsString() << std::endl;
154  // Requested time range lower end
155 
156  for (auto TimeRange : fTimeRanges){ //loop through unstable hv time ranges
157 
158  fTimeRangeLow=TimeRange.first;
159  fTimeRangeHigh=TimeRange.second;
160 
161 
162  // Check that input time is in correct format
163 
164 
165  // Check that input times are in correct format
166  if (fTimeRangeHigh < 1000000000 || fTimeRangeLow < 1000000000 ) {
167  std::cout << "Warning: please provide time in POSIX foramt, event time "
168  << "filter returning false." << std::endl;
169  return false;
170 
171  }
172 
173 
174 
175  if (fTimeRangeHigh < fTimeRangeLow ) {
176  std::cout << "Warning: Lower limit bigger than lower limit "
177  << "filter returning false." << std::endl;
178  return false;
179 
180  }
181  //no idea what this is supposed to be doing!! will reject any times before 10am
182  //
183  // if (fTimeRangeHigh > 0 && fTimeRangeHigh < 100000) {
184  // std::cout << "Warning: please provide time in format HHMMSS, event time "
185  // << "filter returning false.2" << std::endl;
186  // return false;
187  // }
188  // if (fTimeRangeLow > 0 && fTimeRangeLow < 100000) {
189  // std::cout << "Warning: please provide time in format HHMMSS, event time "
190  // << "filter returning false.3" << std::endl;
191  // return false;
192  // }
193  // Event time
194  //art::Timestamp evtTime = evt.time();
195 
196 
197  // all the checking he dies then ask if its in the correct range
198 
199  TTimeStamp * ttsLow(nullptr);
200 
201 
202  ttsLow = new TTimeStamp(fTimeRangeLow);
203  // Requested time range higher end
204  TTimeStamp * ttsHigh(nullptr);
205 
206  ttsHigh = new TTimeStamp(fTimeRangeHigh);
207 
208  // Filter decision
209  if(fDebug){
210  std::cout << "Lower Limit: " << ttsLow -> AsString() << std::endl;
211  std::cout << "Upper Limit: " << ttsHigh -> AsString() << std::endl;
212  }
213 
214 
215  if (evtTTS -> GetSec() > ttsLow -> GetSec() &&
216  evtTTS -> GetSec() < ttsHigh -> GetSec()) { keep=false; }
217 
218 
219  }
220  if ( fDebug ) std::cout << myname << (keep ? "Keep" : "Reject") << "ing event." << std::endl;
221  if (keep==true) fSelectedEvents->Fill(1); //count total events
222  //keep=false; //for testing
223  return keep; //need this to stop the get to end error make sure to check this is working as intended
224 }
ProtoDUNEUnstableHVFilter(fhicl::ParameterSet const &)
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
std::vector< std::pair< UInt_t, UInt_t > > fTimeRanges
art::Handle< std::vector< raw::RDTimeStamp > > RDTimeStampHandle
bool isValid() const noexcept
Definition: Handle.h:191
bool isRealData() const
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
#define MF_LOG_INFO(category)
EDFilter(fhicl::ParameterSet const &pset)
Definition: EDFilter.h:21
const char * AsString(Resonance_t res)
resonance id -> string
TCEvent evt
Definition: DataStructs.cxx:7
void reconfigure(fhicl::ParameterSet const &p)
QTextStream & endl(QTextStream &s)