RemoveBeamSpillsPrecise_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief Reject triggers that are too close to beam spills
3 /// \author Christopher Backhouse - bckhouse@caltech.edu
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 
27 namespace util
28 {
29  /// Reject triggers that are too close to beam spills
30  ///
31  /// *** WARNING! AROUND 0.3% OF THE TIME THIS DOESN'T WORK!!! ***
33  {
34  public:
35  explicit RemoveBeamSpillsPrecise(const fhicl::ParameterSet& pset);
36  virtual ~RemoveBeamSpillsPrecise();
37 
38  void beginJob();
39  bool filter(art::Event& evt);
40 
41  protected:
43  double fVetoWindow;
44 
46 
47  std::unique_ptr<BeamFolder> fFolder;
48 
49  TH1* fClosest;
50  };
51 
52  //..............................................................
54  : fTotTrigs(0), fRejTrigs(0)
55  {
56  fRawDataLabel = pset.get<std::string>("RawDataLabel");
57  fVetoWindow = pset.get<double>("VetoWindow");
58 
60  fFolder = ifbeam->getBeamFolder(pset.get<std::string>("Bundle"),
61  pset.get<std::string>("URL"),
62  pset.get<double>("DBTimeWindow"));
63  }
64 
65  //......................................................................
67  {
68  if(fTotTrigs)
69  std::cout << "RemoveBeamSpillsPrecise: of " << fTotTrigs
70  << " triggers, rejected "
71  << fRejTrigs << " ("
72  << .01*((10000*fRejTrigs)/fTotTrigs) << "%)" << std::endl;
73  }
74 
75  //......................................................................
77  {
79  fClosest = tfs->make<TH1F>("closest", ";#Deltat (sec)", 100000, -.01, +.01);
80  }
81 
82  /// Fractional part
83  double frac(double x)
84  {
85  return x-int(x);
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  struct timespec unixTime;
103  novadaq::timeutils::convertNovaTimeToUnixTime(trig.fTriggerTimingMarker_TimeStart, unixTime);
104 
105  const double eventTime = unixTime.tv_sec + 1e-9*unixTime.tv_nsec;
106 
107  double ea9sct, e8fsct;
108  try{
109  fFolder->GetNamedData(eventTime, "G:EA9SCT,G:E8FSCT", &ea9sct, &e8fsct);
110  }
111  catch(...){
112  mf::LogError("RemoveBeamSpillsPrecise") << "Exception getting spill times. Vetoing event to be safe";
113  return false;
114  }
115 
116  double tof = 0; // Time-of-flight correction
118  if(geom->DetId() == novadaq::cnv::kFARDET) tof = 810e3/3e8; // 810km/c
119 
120  // Find DB time closest to event time
121  double closest = FLT_MAX;
122 
123  // Rounding can sometimes pick the wrong second
124  // TODO - figure out what range of fudge is actually needed
125  for(int fudge = -3; fudge <= +3; ++fudge){
126  const double dt = eventTime - (round(eventTime) + frac(ea9sct) - frac(e8fsct) + fudge) - tof;
127  if(fabs(dt) < fabs(closest)) closest = dt;
128  }
129 
130  if(trig.fTriggerMask_TriggerType == 0 && fabs(closest) > fVetoWindow){
131  mf::LogWarning("RemoveBeamSpillsPrecise") << "This is a NuMI trigger, but the closest spill is "
132  << closest << " seconds away.";
133  }
134 
135  fClosest->Fill(closest);
136 
137  const bool reject = (fabs(closest) < fVetoWindow);
138  if(reject) ++fRejTrigs;
139 
140  return !reject;
141  }
142 
144 
145 } // end namespace ifdb
Filter events based on their run/event numbers.
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool isRealData() const
Definition: Event.h:83
DEFINE_ART_MODULE(TestTMapFile)
fvar< T > round(const fvar< T > &x)
Definition: round.hpp:23
bool convertNovaTimeToUnixTime(uint64_t const &inputNovaTime, struct timespec &outputUnixTime)
Definition: Cand.cxx:23
Far Detector at Ash River, MN.
T get(std::string const &key) const
Definition: ParameterSet.h:231
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
int evt
double frac(double x)
Fractional part.
OStream cout
Definition: OStream.cxx:6
T * make(ARGS...args) const
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void geom(int which=0)
Definition: geom.C:163
assert(nhit_max >=nhit_nbins)
Float_t e
Definition: plot.C:35
RemoveBeamSpillsPrecise(const fhicl::ParameterSet &pset)
Encapsulate the geometry of one entire detector (near, far, ndos)
enum BeamMode string