TimeOffset_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: TimeOffset
3 // Plugin Type: analyzer (art v2_11_03)
4 // File: TimeOffset_module.cc
5 // Brief: A module to quantify time offsets between ProtoDUNE-SP
6 // CRT raw data and TPC raw data. This module shall produce
7 // a plot of time difference between CRT timestamps and
8 // RDTimeStamps in an event. I expect lots of "false matches",
9 // but a peak in this plot indicates a relative time offset
10 // between the CRT and the TPC raw data. This module will
11 // also produce diagnostics for CRT timing.
12 //
13 // Generated at Tue Oct 16 08:06:54 2018 by Andrew Olivier using cetskelgen
14 // from cetlib version v3_03_01.
15 ////////////////////////////////////////////////////////////////////////
16 
17 //Framework includes
25 #include "fhiclcpp/ParameterSet.h"
27 #include "art_root_io/TFileService.h"
28 
29 //CRT includes
31 
32 //lardataobj includes
34 
35 //ROOT includes
36 #include "TH1D.h"
37 
38 namespace CRT {
39  class TimeOffset;
40 }
41 
42 namespace
43 {
44  //Helper struct to reuse code.
45  //Keep track of the minimum and maximum values
46  //passed to operator().
47  template <class COMPARABLE> //COMPARABLE is any assignable type for which operator < and operator > are defined
48  struct limits
49  {
50  limits(): fMin(std::numeric_limits<COMPARABLE>::max()), fMax(std::numeric_limits<COMPARABLE>::min()) {}
51  ~limits() = default;
52 
53  void operator()(const COMPARABLE& comp)
54  {
55  if(comp < fMin) fMin = comp;
56  if(comp > fMax) fMax = comp;
57  }
58 
59  //TODO: Return a const reference instead for types
60  // that cannot be trivially copied?
61  inline COMPARABLE min() const { return fMin; }
62  inline COMPARABLE max() const { return fMax; }
63  inline COMPARABLE range() const { return fMax - fMin; }
64 
65  private:
66  COMPARABLE fMin; //Minimum value seen
67  COMPARABLE fMax; //Maximum value seen
68  };
69 }
70 
72 public:
73  explicit TimeOffset(fhicl::ParameterSet const & p);
74  // The compiler-generated destructor is fine for non-base
75  // classes without bare pointers or other resource use.
76 
77  // Plugins should not be copied or assigned.
78  TimeOffset(TimeOffset const &) = delete;
79  TimeOffset(TimeOffset &&) = delete;
80  TimeOffset & operator = (TimeOffset const &) = delete;
81  TimeOffset & operator = (TimeOffset &&) = delete;
82 
83  // Required functions.
84  void analyze(art::Event const & e) override;
85 
86  // Selected optional functions.
87  //void beginJob() override;
88 
89 private:
90 
91  // Create all of the histograms filled by this module. Callback
92  // for TFileService in case file switching is enabled.
93  void onFileClose();
94 
95  // Labels to identify instances of CRT raw data and
96  // RDTimeStamps to be compared. Use lar -c eventdump.fcl
97  // on a prospective input file to find what labels to
98  // use. An art::InputTag consists of the label of a
99  // module that put() a data product into the Event and
100  // an optional instance name.
101  const art::InputTag fCRTLabel; //Instance of CRT::Triggers to read
102  const art::InputTag fTimestampLabel; //Instance of RDTimeStamps to read
103 
104  // Common plotting parameters. Could become FHICL parameters
105  // if I'm editing them enough.
106  const int fNIntervalBins;
107  const double fIntervalMin;
108  const double fIntervalMax;
109 
110  // Plots that may be produced for each input file depending on
111  // how TFileService is configured. All pointers are observer
112  // pointers to objects owned by a TDirectory that is managed
113  // by a TFileService.
114  TH1D* fTimestampMinusCRT; // Time difference between each RDTimeStamp
115  // and CRT::Trigger
116  TH1D* fEarliestDeltaT; // Time difference between earliest CRT::Trigger
117  // and earliest RDTimeStamp in each Event.
118  TH1D* fCRTDeltaT; // Differences in timestamps of earliest and latest
119  // CRT::Triggers in each Event.
120  TH1D* fMinDeltaT; // Difference between timestamps of CRT::Trigger and RDTimestamp that are closest in time.
121 };
122 
124  :
125  EDAnalyzer(p),
126  fCRTLabel(p.get<art::InputTag>("CRTLabel", "crt")),
127  fTimestampLabel(p.get<art::InputTag>("TimestampLabel")),
128  fNIntervalBins(500),
129  fIntervalMin(0),
130  fIntervalMax(125000)
131 {
132  // Tell "scheduler" which data products this module needs as input
133  consumes<std::vector<CRT::Trigger>>(fCRTLabel);
134  consumes<std::vector<raw::RDTimeStamp>>(fTimestampLabel);
135 
136  // Set up file change callback
138  tfs->registerFileSwitchCallback(this, &CRT::TimeOffset::onFileClose);
139  onFileClose(); // Setting up the callback above doesn't actually call
140  // it for the first file. So, call it now to create
141  // initial histograms.
142 }
143 
145 {
146  std::stringstream ss;
147  ss << fTimestampLabel;
148 
150  fTimestampMinusCRT = tfs->make<TH1D>("TimestampMinusCRT", (std::string("Time Difference Between CRT and ")+ss.str()+";Time [ticks];").c_str(),
152  fEarliestDeltaT = tfs->make<TH1D>("EarliestDeltaT", "Time Difference Between Earliest CRT::Trigger and Earliest RDTimestamp;Time [ticks];",
154  fCRTDeltaT = tfs->make<TH1D>("CRTDeltaT", "Range of CRT Timestamps;Time [ticks];Events", fNIntervalBins, fIntervalMin, fIntervalMax);
155  fMinDeltaT = tfs->make<TH1D>("MinDeltaT", "Minimum Time Difference Between a CRT::Trigger and any RDTimestamp;Time [ticks];", 601, -100, 500);
156 }
157 
159 {
160  // Get the CRT::Triggers and RDTimeStamps that we will compare.
161  // If we fail to get either data product, skip this event without
162  // killing the job. Propagate an error message to the user about
163  // which data product wasn't found first.
164  try
165  {
166  const auto& crtHandle = e.getValidHandle<std::vector<CRT::Trigger>>(fCRTLabel);
167  const auto& timeHandle = e.getValidHandle<std::vector<raw::RDTimeStamp>>(fTimestampLabel);
168 
169  using timestamp_t = int64_t; //Make sure timestamp differences are signed
170  ::limits<timestamp_t> crtLimits, rawLimits;
171  timestamp_t minAbsDeltaT = std::numeric_limits<timestamp_t>::max();
172  for(const auto& trigger: *crtHandle)
173  {
174  //Fill plots for all combinations of a CRT::Trigger and an RDTimeStamp
175  const timestamp_t& crtTime = trigger.Timestamp();
176  for(const auto& time: *timeHandle)
177  {
178  const timestamp_t& rawTime = time.GetTimeStamp();
179  const auto deltaT = rawTime - crtTime;
180  fTimestampMinusCRT->Fill(deltaT);
181  rawLimits(rawTime);
182  if(labs(deltaT) < labs(minAbsDeltaT)) minAbsDeltaT = deltaT;
183  } //For each RDTimeStamp from fTimestampLabel
184 
185  // Fill plots for this CRT::Trigger
186  crtLimits(crtTime);
187  } //For each CRT::Trigger from fCRTLabel
188 
189  fEarliestDeltaT->Fill(rawLimits.min()-crtLimits.min());
190  fCRTDeltaT->Fill(crtLimits.range());
191  fMinDeltaT->Fill(minAbsDeltaT);
192  }
193  catch(const cet::exception& e) //Don't crash the whole job if this module doesn't find the data products it needs.
194  {
195  mf::LogWarning("Product not found") << "Failed to find one of the data products that "
196  << "CRT::TimeOffset needs to make timing plots. "
197  << "This module needs CRT::Triggers and raw::RDTimeStamps.\n"
198  << e.what() << "\n";
199  }
200 }
201 
202 /*void CRT::TimeOffset::beginJob()
203 {
204 }*/
205 
std::string string
Definition: nybbler.cc:12
STL namespace.
TimeOffset(fhicl::ParameterSet const &p)
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
const double fIntervalMin
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
const double fIntervalMax
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
struct ptb::content::word::timestamp_t timestamp_t
static int max(int a, int b)
const art::InputTag fTimestampLabel
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
const art::InputTag fCRTLabel
TimeOffset & operator=(TimeOffset const &)=delete
void analyze(art::Event const &e) override
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33