TBSpillInfo_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief This module reads IFDB and then stores MCenter spill info
3 /// \author lackey32@fnal.gov
4 ////////////////////////////////////////////////////////////////////////
5 
6 //C++ includes
7 #include <iostream>
8 #include <fstream>
9 #include <math.h>
10 #include <string>
11 #include <time.h>
12 #include <fstream>
13 #include <sstream>
14 #include <iomanip>
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include "ifbeam.h"
21 
22 // Framework includes
29 #include "fhiclcpp/ParameterSet.h"
31 
32 // NOvASoft includes
33 #include "NovaTimingUtilities/TimingUtilities.h"
34 #include "RawData/RawTrigger.h"
35 #include "RawData/RawBeamline.h"
38 
39 //IFDB includes
40 #include "IFBeam_service.h"
41 #include "Munits.h"
42 #include "ifbeam.h"
43 
44 
45 /// Information about the IFDB
46 namespace ifdb
47 {
48  class TBSpillInfo : public art::EDProducer
49  {
50  public:
51  explicit TBSpillInfo(fhicl::ParameterSet const& pset); // Required! explicit tag tells the compiler this is not a copy constructor
52  virtual ~TBSpillInfo();
53 
54  void produce(art::Event& evt);
55 
56  void reconfigure(const fhicl::ParameterSet& pset);
57 
58 
59  private:
60  double fOffset;
63 
65  std::unique_ptr<BeamFolder> bfp;
66  bool isMC;
67 
68  };
69 }
70 
71 
72 ////////////////////////////////////////////////////////////////////////
73 namespace ifdb
74 {
75  //..............................................................
77  fOffset (pset.get< double >("Offset") ),
78  pRawDataLabel (pset.get< std::string >("RawDataLabel") ),
79  pRawBLTrigLabel (pset.get< std::string >("RawBLTrigLabel") ),
80  bfp( ifbeam_handle->getBeamFolder(pset.get< std::string >("Bundle"), pset.get< std::string >("URL"), pset.get< double >("TimeWindow"))),
81  isMC(false)
82  {
83  // how close in time does the spill time have to be from the DAQ tims [seconds]
84  // TB has one 4.2 second spill every minute.
85  bfp->set_epsilon(5);
86  // tell the module what it is making as this is a EDProducer
87  produces< sumdata::MCenterData >();
88  // Be noisy to demonstrate what's happening
89  mf::LogInfo("TBSpillInfo") << "In TBSpillInfo Constructor\n";
90 
91  }
92 
93  //......................................................................
95  {
96  }
97 
98  //......................................................................
100  {
101  fOffset = pset.get< double >("Offset");
102  }
103 
104  //......................................................................
106  {
107  std::unique_ptr<sumdata::MCenterData> mcenterdata(new sumdata::MCenterData());
108 
109  if(!evt.isRealData()){
110  // remove empty spill info write from here, move to GENIEGen and fill
111  isMC = true;
112  // don't have any beam sim to put in here
113  return;
114  }
115 
116  //this will be the CORRECT UTC time we want ultimately
117  struct timespec unixTime;
118 
121 
122  // Case: use NOvA DAQ trigger
123  if (evt.getByLabel<std::vector<rawdata::RawTrigger> >(pRawDataLabel, trigv)) {
124 
125  const rawdata::RawTrigger& trig = (*trigv)[0];
126 
127  if( !(trig.fTriggerMask_TriggerType == 1 || trig.fTriggerMask_TriggerType == 43) ){
128  // This isn't a Beamline or TBSpill trigger. Don't try to get beam info
129  evt.put(std::move(mcenterdata));
130  return;
131  }
132 
134 
135  }
136 
137  // Case: use Beamline DAQ trigger
138  else if (evt.getByLabel<std::vector<rawdata::RawBeamlineTrigger> >(pRawBLTrigLabel, bltrigv)) {
139 
140  const rawdata::RawBeamlineTrigger& trig = (*bltrigv)[0];
142 
143  }
144 
145  else
146  throw cet::exception("TBSpillInfo") << "failed to get valid trigger handle";
147 
148  unsigned long int uevt_sec = unixTime.tv_sec; //utval >> 32 & 0xFFFFFFFFF;
149  unsigned long int uevt_nsec = unixTime.tv_nsec; //utval & 0xFFFFFFFFF;
150 
151  //DAQ event time
152  double DAQtime = (double)(uevt_sec + (0.000000001)*uevt_nsec);
153 
154  // beam parameters
155  double mcnrg=0.;
156  double mc6ic=0.;
157  double mc6ictime=0.;
158  double mc6cv=0.;
159  double mc7sc1=0.;
160  double mc7an1=0.;
161 
162  // NOvA scintillator counters
163  double novtsc01=0.;
164  double novtsc07=0.;
165 
166  // start with what was originally in bundle - split up the beam devices due to
167  // some issues querying around March 4th. Looks like devices that were added to the bundle
168  // later don't have data for some time on the 4th.
169  try{
170 
171  // Query the database to get post-spill sample device info. We are querying $36, so an offset
172  // is added to the event time to match the db time. Epsilon for the beam folder is large enough
173  // to encompass full spill
174 
175  bfp->GetNamedData(DAQtime+fOffset,"F:MC6IC@,F:MC7SC1", &mc6ic, &mc6ictime, &mc7sc1);
176 
177  //Check time close to DB time later
178 
179  // Epsilon provided on beam folder should already guarantee matching spill time (if one exists)
180 
181  unsigned long int spill_time_sec = (int) mc6ictime;
182  unsigned long int spill_time_nsec = (mc6ictime-spill_time_sec)*1e9;
183  double diff = DAQtime+fOffset-mc6ictime;
184 
185  mcenterdata->spilltimesec = spill_time_sec;
186  mcenterdata->spilltimensec = spill_time_nsec;
187  mcenterdata->deltaspilltimensec = diff*1e9;
188  mcenterdata->mc6int = mc6ic;
189  mcenterdata->mc7int = mc7sc1;
190 
191  }
192  catch (WebAPIException we){
193  std::string err(we.what());
194  // If the varialbe can't be found, it means there was no spill within epsilon of DAQtime+fOffset
195  // In this case, just enter zeros for all spill data.
196  // If it's a different error, force and abort and print out error
197  if (!err.find("variable not found")) {
198  mf::LogError("TBSpillInfo") << "Exception encountered: " << we.what();
199  std::abort();
200  }
201  } // end try/catch
202 
203  // Get other beam devices
204  try{
205 
206  bfp->GetNamedData(DAQtime+fOffset,"F:MCENRG,F:MC6CV,F:MC7AN1", &mcnrg, &mc6cv, &mc7an1);
207 
208  mcenterdata->mcenrg = mcnrg;
209  mcenterdata->mc6col = mc6cv;
210  mcenterdata->mc7magnet = mc7an1;
211 
212  }
213  catch (WebAPIException we){
214  std::string err(we.what());
215  // If the varialbe can't be found, it means there was no spill within epsilon of DAQtime+fOffset
216  // or there is no reading for the scintillator counters at this time
217  // In this case, just enter zeros for the counters
218  // If it's a different error, force and abort and print out error
219  if (!err.find("variable not found")) {
220  mf::LogError("TBSpillInfo") << "Exception encountered: " << we.what();
221  std::abort();
222  }
223  } // end try/catch
224 
225 
226  // Get NOvA scintillator counters
227  try{
228 
229  bfp->GetNamedData(DAQtime+fOffset,"Z:NOVTSC01,Z:NOVTSC07", &novtsc01, &novtsc07);
230 
231  mcenterdata->targetsc = novtsc01;
232  mcenterdata->daqtrigger = novtsc07;
233 
234  }
235  catch (WebAPIException we){
236  std::string err(we.what());
237  // If the varialbe can't be found, it means there was no spill within epsilon of DAQtime+fOffset
238  // or there is no reading for the scintillator counters at this time
239  // In this case, just enter zeros for the counters
240  // If it's a different error, force and abort and print out error
241  if (!err.find("variable not found")) {
242  mf::LogError("TBSpillInfo") << "Exception encountered: " << we.what();
243  std::abort();
244  }
245  } // end try/catch
246 
247 
248  evt.put(std::move(mcenterdata));
249 
250  } //end event loop
251 
252 
253 
255 
256 } // end namespace ifdb
257 ////////////////////////////////////////////////////////////////////////
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void reconfigure(const fhicl::ParameterSet &pset)
art::ServiceHandle< ifbeam_ns::IFBeam > ifbeam_handle
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
This module reads IFDB DB and then stores BNB spill info.
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)
TBSpillInfo(fhicl::ParameterSet const &pset)
uint8_t fTriggerMask_TriggerType
Definition: RawTrigger.h:43
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
std::string pRawBLTrigLabel
T get(std::string const &key) const
Definition: ParameterSet.h:231
uint64_t TDUTime() const
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
unsigned long long fTriggerTimingMarker_TimeStart
Definition: RawTrigger.h:38
Raw data definitions for beamline data used in NOvA test beam experiment.
void produce(art::Event &evt)
std::unique_ptr< BeamFolder > bfp