RemoveTBSpills_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief Remove all events that occur too close to 4.2s Test Beam spill
3 /// \author Teresa Lackey - lackey32@fnal.gov
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <string>
7 #include <iostream>
8 #include <time.h>
9 
16 #include "fhiclcpp/ParameterSet.h"
17 
18 #include "Geometry/Geometry.h"
19 #include "NovaTimingUtilities/TimingUtilities.h"
20 #include "RawData/RawTrigger.h"
21 
22 #include "TH1.h"
23 
24 #include "IFBeam_service.h"
25 
26 namespace util
27 {
28  /// Reject triggers that are too close to beam spills
30  {
31  public:
32  explicit RemoveTBSpills(const fhicl::ParameterSet& pset);
33  virtual ~RemoveTBSpills();
34 
35  void beginJob();
36  bool filter(art::Event& evt);
37 
38  protected:
41 
43 
44  std::unique_ptr<BeamFolder> fFolder;
45 
46  TH1* fdeltaTAll;
49  };
50 
51  //..............................................................
53  : fTotTrigs(0), fRejTrigs(0)
54  {
55  fRawDataLabel = pset.get<std::string>("RawDataLabel");
56  fVetoWindow = pset.get<double>("VetoWindow");
57  fEpsilon = pset.get<double>("Epsilon");
58  fOffset = pset.get<double>("Offset");
59 
61  fFolder = ifbeam->getBeamFolder(pset.get<std::string>("Bundle"),
62  pset.get<std::string>("URL"),
63  pset.get<double>("DBTimeWindow"));
64 
65  // Look this far back in time for a spill, in seconds
66  fFolder->set_epsilon(fEpsilon);
67  }
68 
69  //......................................................................
71  {
72  if(fTotTrigs)
73  std::cout << "RemoveTBSpills: of " << fTotTrigs
74  << " triggers, rejected "
75  << fRejTrigs << " ("
76  << .01*((10000*fRejTrigs)/fTotTrigs) << "%)" << std::endl;
77  }
78 
79  //......................................................................
81  {
83  fdeltaTAll = tfs->make<TH1F>("all", ";#Deltat (sec)", 400, -20, +20);
84  fdeltaTVeto = tfs->make<TH1F>("veto", ";#Deltat (sec)", 400, -20, +20);
85  fdeltaTKeep = tfs->make<TH1F>("keep", ";#Deltat (sec)", 400, -20, +20);
86  }
87 
88  //......................................................................
90  {
91  // Doesn't apply to MC
92  if(!evt.isRealData()) return true;
93 
94  ++fTotTrigs;
95 
96  // Get trigger information to extract trigger time
98  evt.getByLabel(fRawDataLabel, trigv);
99  assert(!trigv->empty());
100  const rawdata::RawTrigger& trig = (*trigv)[0];
101 
102  // Get trigger time in unix time
103  struct timespec unixTime;
104  novadaq::timeutils::convertNovaTimeToUnixTime(trig.fTriggerTimingMarker_TimeStart, unixTime);
105  const double eventTime = unixTime.tv_sec + 1e-9*unixTime.tv_nsec;
106 
107  // Query the database to get MC6IC end spill time.
108  // Since we are querying $36, have to add an offset to get the end spill time,
109  // rather than when the spill actually starts. The epsilon on the beam folder should be
110  // large enough to encapsulate the entire spill, plus any offset.
111  // Test Beam spills are ~4.2s long.
112 
113  double mc6ic = 0;
114  double time = 0; // Forces large deltaT when no spill found (and therefore keeps event)
115  try{
116  fFolder->GetNamedData(eventTime+fOffset, "F:MC6IC@", &mc6ic, &time);
117  }
118  catch(std::exception& e) {
119  std::string err(e.what());
120  // If the variable can't be found, it means there was no spill within Epsilon of eventTime+fOffset
121  if (!err.find("variable not found")) {
122  mf::LogError("RemoveTBSpills") << "Exception encountered: " << e.what();
123  abort();
124  }
125  }
126 
127  double deltaT = eventTime + fOffset - time;
128 
129  // Reject anything inside the beam window. Offset is set such that the spill falls close to or > 0.
130  bool reject = (0 < deltaT) && (deltaT < fVetoWindow);
131  if (reject) {
132  ++fRejTrigs;
133  mf::LogInfo("RemoveTBSpills") << "Event: " << evt.event() <<" Vetoed event with trigger time "
134  << std::fixed << eventTime << " due to spill at time "
135  << time << " (deltaT = " << deltaT << ")" << std::defaultfloat;
136  fdeltaTVeto->Fill(deltaT);
137  }
138  else
139  fdeltaTKeep->Fill(deltaT);
140  fdeltaTAll->Fill(deltaT);
141  return !reject;
142 
143  }
144 
146 
147 } // end namespace ifdb
Filter events based on their run/event numbers.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool isRealData() const
Definition: Event.h:83
DEFINE_ART_MODULE(TestTMapFile)
bool convertNovaTimeToUnixTime(uint64_t const &inputNovaTime, struct timespec &outputUnixTime)
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
T get(std::string const &key) const
Definition: ParameterSet.h:231
EventNumber_t event() const
Definition: Event.h:67
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T * make(ARGS...args) const
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
Reject triggers that are too close to beam spills.
RemoveTBSpills(const fhicl::ParameterSet &pset)
assert(nhit_max >=nhit_nbins)
bool filter(art::Event &evt)
Float_t e
Definition: plot.C:35
std::unique_ptr< BeamFolder > fFolder
Encapsulate the geometry of one entire detector (near, far, ndos)