LongReadoutAnalyzer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: LongReadoutAnalyzer
3 // Module Type: analyzer
4 // File: LongReadoutAnalyzer_module.cc
5 //
6 // Generated at Thu Mar 16 11:30:28 2017 by Justin Vasel using artmod
7 // from cetpkgsupport v1_10_02.
8 ////////////////////////////////////////////////////////////////////////
9 
10 // Framework includes
22 #include "fhiclcpp/ParameterSet.h"
24 
25 // NOvASoft includes
27 #include "DAQDataFormats/RawEvent.h"
28 #include "DAQDataFormats/RawEventHeader.h"
29 #include "NovaDAQConventions/DAQConventions.h"
30 #include "NovaTimingUtilities/TimingUtilities.h"
31 #include "RawData/FlatDAQData.h"
32 #include "RawData/DAQHeader.h"
33 #include "RawData/DAQUnpack.h"
34 #include "RawData/RawDigit.h"
35 #include "RawData/RawSumDropMB.h"
36 #include "RawData/RawTrigger.h"
38 
39 // ROOT includes
40 #include "TFile.h"
41 #include "TTree.h"
42 
43 
44 namespace trigmon {
45  class LongReadoutAnalyzer;
46 }
47 
49 public:
50  explicit LongReadoutAnalyzer(fhicl::ParameterSet const & p);
51 
52  LongReadoutAnalyzer(LongReadoutAnalyzer const &) = delete;
56 
57  // Required functions.
58  void analyze(art::Event const & e) override;
59 
60  // Selected optional functions.
61  void beginJob() override;
62  void endJob() override;
63  void beginRun(art::Run const& r) override;
64  void respondToOpenInputFile(const art::FileBlock& fb) override;
65 
66  void createDBTables();
67  void fillDB();
68 
69 private:
70  std::string fRawDataLabel; ///< label of where to find RawData
71  std::vector<unsigned int> fTriggerTypes; ///< vector of trigger type numbers to check
72 
73  TTree* fEvents;
74  std::string fFileName; ///< Input filename
75  novadaq::cnv::DetId fDetId; ///< Detector ID
76  unsigned int fEvt; ///< Event number
77  unsigned int fEvtRecord; ///< Event number for this file
78  unsigned int fRun; ///< Run number
79  unsigned int fSubRun; ///< Subrun number
80  unsigned long long fTime; ///< Event timestamp
81  unsigned int fNMicroslices; ///< Number of microslices present
82  unsigned long long fTrigNum; ///< Trigger number
83  unsigned long long fTrigMasterNum; ///< Trigger master number
84  unsigned long long fTrigMasterID; ///< Unique identifier for a given master trigger
85  unsigned int fTrigType; ///< Numerical trigger type
86  unsigned long long fTrigTimeStart; ///< NOvA time of trigger
87  unsigned long long fTrigUnixTimeStart; ///< Unix time of trigger (in ns)
88  uint32_t fTrigLength; ///< Length of trigger (units of 500 nsec)
89  int fErrorRegister; ///< Error register
90  unsigned int fNMicroslicesMin; ///< Minimum number of microslices to be OK
91  unsigned int fNMicroclicesMax; ///< Maximum number of microslices to be OK
92  uint32_t fNDroppedMicroBlocks; ///< Number of dropped microblocks
93  bool fWriteToSQLite3File; ///< Write to sqlite3 file?
94  bool fWriteToROOTFile; ///< Write to ROOT histogram file?
95  std::string fSQLite3FileName; ///< Name of sqlite3 file
96  std::string fSchemaFileName; ///< Name of sqlite3 schema file
97 
98  sqlite3* db; ///< SQLite3 database object
99  sqlite3_stmt *stmt = 0; ///< SQLite3 prepared statement container
100 
101 };
102 
103 
104 //...........................................................................
106  :
107  EDAnalyzer(p),
108  fRawDataLabel(p.get<std::string>("RawDataLabel")),
109  fTriggerTypes(p.get<std::vector<unsigned int>>("TriggerTypes")),
110  fFileName(""),
112  fEvt(0),
113  fRun(0),
114  fSubRun(0),
115  fTime(0),
116  fNMicroslices(0),
117  fTrigNum(0),
118  fTrigMasterNum(0),
119  fTrigMasterID(0),
120  fTrigType(0),
121  fTrigTimeStart(0),
123  fTrigLength(0),
124  fErrorRegister(0),
125  fNMicroslicesMin(p.get<int>("NMicroslicesMin")),
126  fNMicroclicesMax(p.get<int>("NMicroslicesMax")),
128  fWriteToSQLite3File(p.get<bool>("WriteToSQLite3File")),
129  fWriteToROOTFile(p.get<bool>("WriteToROOTFile")),
130  fSQLite3FileName(p.get<std::string>("SQLite3FileName")),
131  fSchemaFileName(p.get<std::string>("SchemaFileName"))
132 {
133  if (fWriteToROOTFile) {
135  }
136 }
137 
138 
139 //...........................................................................
141 {
142  // Reset the error register for this event
143  fErrorRegister = 0;
144 
145  /* If this is MC, don't do anything */
146  if (!e.isRealData()) {
147  mf::LogWarning("EventIsMC") << "This event is not real data. Skipping...";
148  return;
149  }
150 
151 
152  /* Get some basic info from the event */
153  fEvt = e.id().event();
154  fRun = e.run();
155  fSubRun = e.subRun();
156  fTime = (unsigned long long)((e.time()).value());
157  fEvtRecord++;
158 
159  /* Get the FlatDAQData data from the event and unpack it */
161  e.getByLabel(fRawDataLabel, flatdaqdata);
162 
163  rawdata::DAQUnpack* daqUnpack(new rawdata::DAQUnpack);
165  daqUnpack->GetRawEvent(*flatdaqdata, rawevt);
166 
167  /* Get DAQHeader from event */
169  e.getByLabel(fRawDataLabel, daqheader);
170  fNMicroslices = daqheader->TotalMicroSlices();
171 
173  e.getByLabel(fRawDataLabel, droppedMBs);
174  fNDroppedMicroBlocks = (*droppedMBs)[0].fSumTrig_NumDroppedMicroblocks;
175 
176 
177  /* Get the trigger information from event */
179  e.getByLabel(fRawDataLabel, trigs);
180  const rawdata::RawTrigger trig = trigs->at(0);
181 
184  fTrigType = (unsigned int)(trig.fTriggerMask_TriggerType);
186 
187  /* If the trigger doesn't match list in fcl, don't do anything */
188  if (std::find(fTriggerTypes.begin(), fTriggerTypes.end(), fTrigType) == fTriggerTypes.end()) {
189  mf::LogWarning("RequestedTriggerNotFound") << "This trigger type (" << fTrigType << ") does not match a requested type. Skipping this trigger.";
190  return;
191  }
192 
193  // FFFF FFFF FF00 0000 - Master trigger number: first 40 bits
194  // 0000 0000 00FF FFFF - Run number: remaining 24 bits
195  fTrigMasterID = ((uint64_t)fTrigMasterNum & 0xffffffffff) << 24 | ((uint64_t)fRun & 0xffffff);
196 
197  std::cout << "Processing record " << fEvtRecord << " (master trigger 0x" << std::hex << fTrigMasterID << std::dec << ", trigger " << fTrigNum << ", type " << fTrigType << ")" << std::endl;
198 
199  /* Convert trigger start time form NOvA time to Unix time */
200  struct timespec unixtime;
202 
204  fTrigUnixTimeStart = unixtime.tv_sec * 1e9 + unixtime.tv_nsec;
205 
206  /* Check for known error modes */
207  if (rawevt.getHeader()->isEventIncomplete()) fErrorRegister |= 1 << trigmon::LongReadoutError::kIncompleteEvent;
208  if (fNMicroslices < fNMicroslicesMin || fNMicroslices > fNMicroclicesMax) fErrorRegister |= 1 << trigmon::LongReadoutError::kNHitsOutOfRange;
209 
210  /* Fill TTree and DB */
211  if (fWriteToROOTFile) fEvents->Fill();
212  if (fWriteToSQLite3File) this->fillDB();
213 
214  return;
215 }
216 
217 
218 //...........................................................................
220 {
221  /* Open database and create tables */
222  if (fWriteToSQLite3File) {
223  sqlite3_open(fSQLite3FileName.c_str(), &db);
224  this->createDBTables();
225  }
226 
227  /* Build TTree */
228  if (fWriteToROOTFile) {
230  fEvents = tfs->make<TTree>("Events", "Events");
231  fEvents->Branch("evt", &fEvt);
232  fEvents->Branch("evtRecord", &fEvtRecord);
233  fEvents->Branch("run", &fRun);
234  fEvents->Branch("srun", &fSubRun);
235  fEvents->Branch("time", &fTime);
236  fEvents->Branch("NMicroSlices", &fNMicroslices);
237  fEvents->Branch("NDroppedMicroBlocks", &fNDroppedMicroBlocks);
238  fEvents->Branch("ErrorRegister", &fErrorRegister);
239  fEvents->Branch("TriggerNo", &fTrigNum);
240  fEvents->Branch("MasterTriggerNo", &fTrigMasterNum);
241  fEvents->Branch("MasterTriggerID", &fTrigMasterID);
242  fEvents->Branch("TriggerType", &fTrigType);
243  fEvents->Branch("TriggerTimeStart", &fTrigTimeStart);
244  fEvents->Branch("TriggerUnixTimeStart", &fTrigUnixTimeStart);
245  fEvents->Branch("TriggerLength", &fTrigLength);
246  }
247 
248  fEvtRecord = 0;
249 
250  return;
251 }
252 
253 
254 //...........................................................................
256 {
257  if (fWriteToSQLite3File) {
258  /* Fill table: "filenames" */
259  sqlite3_prepare_v2(db,
260  "INSERT OR REPLACE INTO files(filename, det, run, subrun) VALUES(?, ?, ?, ?);",
261  -1,
262  &stmt,
263  NULL);
264 
265  sqlite3_bind_text (stmt, 1, fFileName.c_str(), -1, 0);
266  sqlite3_bind_int (stmt, 2, fDetId);
267  sqlite3_bind_int (stmt, 3, fRun);
268  sqlite3_bind_int (stmt, 4, fSubRun);
269 
270  int rc = sqlite3_step(stmt);
271  if( rc != SQLITE_DONE)
272  throw cet::exception("LongReadoutAnalyzer") << "problem writing to \"files\" table (SQLite error " << rc << ").";
273 
274  rc = sqlite3_finalize(stmt);
275 
276 
277  /* close DB connection */
278  sqlite3_close(db);
279  }
280 
281  return;
282 }
283 
284 
285 //...........................................................................
287 {
289  fDetId = ds->DetId();
290 }
291 
292 
293 //...........................................................................
295 {
296  fFileName = basename((char*)fb.fileName().c_str());
297 }
298 
299 
300 //...........................................................................
302 {
303  char* errMsg = 0;
304  std::string schemaFilePath;
305 
306  cet::search_path searchpath("FW_SEARCH_PATH");
307  if (!searchpath.find_file(fSchemaFileName, schemaFilePath)) {
308  std::cout << "Cannot find SQL schema file" << std::endl;
309  throw cet::exception("LongReadoutAnalyzer") << "Cannot find SQL schema file " << fSchemaFileName;
310  }
311 
312  std::ifstream schemaFile(schemaFilePath);
313  std::string sqlschema((std::istreambuf_iterator<char>(schemaFile)), std::istreambuf_iterator<char>());
314 
315  sqlite3_exec(db, sqlschema.c_str(), 0, 0, &errMsg);
316 
317  if (errMsg) {
318  std::cout << "Error message: " << errMsg << std::endl;
319  throw cet::exception("LongReadoutAnalyzer") << "Error writing database schema: " << errMsg << std::endl;
320  }
321  return;
322 }
323 
324 
325 //...........................................................................
327 {
328  /* Fill table: "triggers" */
329  sqlite3_prepare_v2(db,
330  "INSERT OR REPLACE INTO triggers(master_trigger_id, master_trigger_number, trigger_number, type, run, subrun, event, tstart_nova, tstart_unix, tlength, n_uslices, n_dropped_ublocks, error_modes) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
331  -1,
332  &stmt,
333  NULL);
334 
335  sqlite3_bind_int64 (stmt, 1, fTrigMasterID);
336  sqlite3_bind_int (stmt, 2, fTrigMasterNum);
337  sqlite3_bind_int (stmt, 3, fTrigNum);
338  sqlite3_bind_int (stmt, 4, fTrigType);
339  sqlite3_bind_int (stmt, 5, fRun);
340  sqlite3_bind_int (stmt, 6, fSubRun);
341  sqlite3_bind_int (stmt, 7, fEvt);
342  sqlite3_bind_int64 (stmt, 8, fTrigTimeStart);
343  sqlite3_bind_int64 (stmt, 9, fTrigUnixTimeStart);
344  sqlite3_bind_int (stmt, 10, fTrigLength);
345  sqlite3_bind_int (stmt, 11, fNMicroslices);
346  sqlite3_bind_int (stmt, 12, fNDroppedMicroBlocks);
347  sqlite3_bind_int (stmt, 13, fErrorRegister);
348 
349  if( sqlite3_step(stmt) != SQLITE_DONE)
350  throw cet::exception("LongReadoutAnalyzer") << "problem writing to triggers table ";
351 
352  sqlite3_finalize(stmt);
353 
354  return;
355 }
356 
357 
novadaq::cnv::DetId DetId() const
What detector are we in?
SubRunNumber_t subRun() const
Definition: Event.h:72
unsigned int fNMicroslices
Number of microslices present.
bit-1: isEventIncomplete flag set
unsigned int fSubRun
Subrun number.
uint32_t fNDroppedMicroBlocks
Number of dropped microblocks.
std::string fRawDataLabel
label of where to find RawData
std::string fFileName
Input filename.
const char * p
Definition: xmltok.h:285
void analyze(art::Event const &e) override
std::string const & fileName() const
Definition: FileBlock.h:38
unsigned long long fTrigMasterID
Unique identifier for a given master trigger.
LongReadoutAnalyzer(fhicl::ParameterSet const &p)
unsigned long long fTime
Event timestamp.
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
LongReadoutAnalyzer & operator=(LongReadoutAnalyzer const &)=delete
bool fWriteToSQLite3File
Write to sqlite3 file?
bool isRealData() const
Definition: Event.h:83
DEFINE_ART_MODULE(TestTMapFile)
int TotalMicroSlices() const
Definition: DAQHeader.h:27
unsigned int fTrigType
Numerical trigger type.
bool convertNovaTimeToUnixTime(uint64_t const &inputNovaTime, struct timespec &outputUnixTime)
std::string find_file(std::string const &filename) const
uint32_t fTrigLength
Length of trigger (units of 500 nsec)
bit-0: Number of hits out of range
sqlite3_stmt * stmt
SQLite3 prepared statement container.
Definition: Run.h:31
uint8_t fTriggerMask_TriggerType
Definition: RawTrigger.h:43
void respondToOpenInputFile(const art::FileBlock &fb) override
unsigned int fEvtRecord
Event number for this file.
std::string fSQLite3FileName
Name of sqlite3 file.
unsigned long long fTriggerHeader_MasterTriggerNumber
Definition: RawTrigger.h:35
static const char *const errMsg[]
Definition: Error.h:69
bool fWriteToROOTFile
Write to ROOT histogram file?
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::string fSchemaFileName
Name of sqlite3 schema file.
bool GetRawEvent(const std::vector< rawdata::FlatDAQData > &flatdaqdata, daqdataformats::RawEvent &raw)
Definition: DAQUnpack.cxx:57
unsigned long long fTrigUnixTimeStart
Unix time of trigger (in ns)
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
A very simple service to remember what detector we&#39;re working in.
OStream cout
Definition: OStream.cxx:6
unsigned int fNMicroclicesMax
Maximum number of microslices to be OK.
EventNumber_t event() const
Definition: EventID.h:116
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T * make(ARGS...args) const
unsigned long long fTrigMasterNum
Trigger master number.
sqlite3 * db
SQLite3 database object.
void beginRun(art::Run const &r) override
unsigned long long fTriggerHeader_TriggerNumber
Definition: RawTrigger.h:34
std::vector< unsigned int > fTriggerTypes
vector of trigger type numbers to check
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
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
novadaq::cnv::DetId fDetId
Detector ID.
TRandom3 r(0)
Timestamp time() const
Definition: Event.h:61
unsigned long long fTrigTimeStart
NOvA time of trigger.
uint32_t fTriggerRange_TriggerLength
Definition: RawTrigger.h:40
unsigned long long fTrigNum
Trigger number.
Float_t e
Definition: plot.C:35
RunNumber_t run() const
Definition: Event.h:77
unsigned int fNMicroslicesMin
Minimum number of microslices to be OK.
EventID id() const
Definition: Event.h:56