SSPTriggerAna_module.cc
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 2; -*-
2 ////////////////////////////////////////////////////////////////////////
3 // Class: SSPTriggerAna
4 // Module Type: producer
5 // File: SSPTriggerAna_module.cc
6 //
7 // Quickly analyze raw data trigger rate in the SSP
8 //
9 // Alex Himmel ahimmel@fnal.gov (modified by Celio Moura camj@fnal.gov)
10 //
11 ////////////////////////////////////////////////////////////////////////
12 
20 #include "fhiclcpp/ParameterSet.h"
22 
23 #include <memory>
24 #include <iostream>
25 #include <map>
26 #include <set>
27 #include <iomanip>
28 
29 //lbne-artdaq includes
30 #include "lbne-raw-data/Overlays/SSPFragment.hh"
31 #include "lbne-raw-data/Overlays/anlTypes.hh"
32 #include "artdaq-core/Data/Fragment.hh"
33 
34 //larsoft includes
35 #include "lardataobj/RawData/raw.h"
38 
39 //daqinput35t includes
40 
42 #include "SSPReformatterAlgs.h"
43 
44 /*camj_includes*/
45 #include "TH1.h"
47 #include "art_root_io/TFileService.h"
48 #include "art_root_io/TFileDirectory.h"
49 
50 namespace DAQToOffline {
51  class SSPTriggerAna;
52 }
53 
55 public:
56  explicit SSPTriggerAna(fhicl::ParameterSet const & pset);
57  // The destructor generated by the compiler is fine for classes
58  // without bare pointers or other resource use.
59 
60  void analyze(art::Event const & evt) override;
61  void reconfigure(fhicl::ParameterSet const& pset);
62  void printParameterSet();
63 
64 
65  // Plugins should not be copied or assigned.
66  SSPTriggerAna(SSPTriggerAna const &) = delete;
67  SSPTriggerAna(SSPTriggerAna &&) = delete;
68  SSPTriggerAna & operator = (SSPTriggerAna const &) = delete;
70 
71 private:
72  void beginJob() override;
73  void endJob () override;
74  //void beginEvent(art::EventNumber_t eventNumber);
75  //void endEvent (art::EventNumber_t eventNumber);
76  void beginRun(art::Run const& run) override;
77  void endRun (art::Run const& run) override;
78 
82 
84 
85  unsigned long int firstTime;
86  unsigned long int lastTime;
87  std::map<int, long int> triggerCount;
88  std::map<int, long int> triggerCountHeaders;
89  std::map<int, long int> triggerCountWaveforms;
90 
94 
95 };
96 
97 
99  : art::EDAnalyzer(pset),
100  sspReform(pset.get<fhicl::ParameterSet>("SSPReformatter"))
101 {
102 
103  this->reconfigure(pset);
104 
105  //first_FirstSample = -1;
106  //first_TimeStamp = -1;
107 }
108 
110 
111  fFragType = pset.get<std::string>("FragType");
112  fRawDataLabel = pset.get<std::string>("RawDataLabel");
113 
114  //fDebug = pset.get<bool>("Debug");
115  //fZeroThreshold=0;
116  //fCompression=raw::kNone;
117 
119 
120 }
121 
123 
124  mf::LogDebug("SSPTriggerAna") << "====================================" << "\n"
125  << "Parameter Set" << "\n"
126  << "====================================" << "\n"
127  << "fFragType: " << fFragType << "\n"
128  << "fRawDataLabel: " << fRawDataLabel << "\n"
129  << "====================================" << "\n";
130 }
131 
133 {
134  //art::ServiceHandle<art::TFileService> tfs;
135  //adc_values_ = tfs->make<TH1D>("adc_values","adc_values",4096,-0.5,4095.5);
136 
138  fTriggerRateHist = tfs->make<TH1D>("triggerratehist",";Trigger Rate vs OpChannel;Rate (Hz)",116,-10,106);
139  fTriggerRateHistHeaders = tfs->make<TH1D>("triggerratehist_headers",";Trigger Rate vs OpChannel;Rate (Hz)",116,-10,106);
140  fTriggerRateHistWaveforms = tfs->make<TH1D>("triggerratehist_waveforms",";Trigger Rate vs OpChannel;Rate (Hz)",116,-10,106);
141 
142  firstTime = (((unsigned long int)1)<<63);
143  lastTime = 0;
144 }
145 
146 //void DAQToOffline::SSPTriggerAna::beginEvent(art::EventNumber_t /*eventNumber*/)
147 
149 {
150  fTriggerRateHist->Reset();
151  fTriggerRateHistHeaders->Reset();
152  fTriggerRateHistWaveforms->Reset();
153 }
154 
156 {
157  long int deltaT = lastTime-firstTime;
158  double deltaTus = ((double)deltaT)/sspReform.ClockFrequency();
159 
160  fTriggerRateHist->Scale(1.e6/deltaTus);
161  fTriggerRateHistHeaders->Scale(1.e6/deltaTus);
162  fTriggerRateHistWaveforms->Scale(1.e6/deltaTus);
163 
164  fTriggerRateHist->SetTitle(Form("Run %i Trigger Rate",run.run()));
165  fTriggerRateHistHeaders->SetTitle(Form("Run %i Header Trigger Rate",run.run()));
166  fTriggerRateHistWaveforms->SetTitle(Form("Run %i Wavefrom Trigger Rate",run.run()));
167 
168  fTriggerRateHist->Write(Form("run_%i_trigger_rate",run.run()));
169  fTriggerRateHistHeaders->Write(Form("run_%i_trigger_rate_headers",run.run()));
170  fTriggerRateHistWaveforms->Write(Form("run_%i_trigger_rate_waveforms",run.run()));
171 
172 }
173 
174 //void DAQToOffline::SSPTriggerAna::endEvent(art::EventNumber_t eventNumber)
175 
177 {
178  //delete adc_values_;
179  delete fTriggerRateHist;
182 
183  long int deltaT = lastTime-firstTime;
184  double deltaTus = ((double)deltaT)/sspReform.ClockFrequency();
185 
186 
187  mf::LogVerbatim("SSPTriggerAna") << "!! Trigger Rate Report." << std::endl;
188  mf::LogVerbatim("SSPTriggerAna") << "!! Time: " << deltaTus / 60.e6 << " minutes." << std::endl;
189 
190  for (auto itr = triggerCount.begin(); itr != triggerCount.end(); itr++) {
191  double freq = ((double)itr->second) / deltaTus * 1e6;
192  mf::LogVerbatim("SSPTriggerAna") << "!! Channel " << itr->first << ": " << std::setiosflags(std::ios::fixed) << std::setprecision(2) << std::setw(7) << freq << " Hz" << std::endl;
193  }
194 
195  for (auto itr = triggerCountHeaders.begin(); itr != triggerCountHeaders.end(); itr++) {
196  double freq = ((double)itr->second) / deltaTus * 1e6;
197  mf::LogVerbatim("SSPTriggerAna") << "!! H Channel " << itr->first << ": " << std::setiosflags(std::ios::fixed) << std::setprecision(2) << std::setw(7) << freq << " Hz" << std::endl;
198  }
199 
200  for (auto itr = triggerCountWaveforms.begin(); itr != triggerCountWaveforms.end(); itr++) {
201  double freq = ((double)itr->second) / deltaTus * 1e6;
202  mf::LogVerbatim("SSPTriggerAna") << "!! W Channel " << itr->first << ": " << std::setiosflags(std::ios::fixed) << std::setprecision(2) << std::setw(7) << freq << " Hz" << std::endl;
203  }
204 
205 
206  /*
207  mf::LogInfo("SSPTriggerAna") << "firstSample: " << firstTime << " samples\n"
208  << "lastSample: " << lastTime << " samples\n"
209  << "totalSamples: " << deltaT << " samples\n"
210  << "totalTime: " << deltaTus << " us\n"
211  << "# Channels: " << channels.size() << "\n"
212  << "# Triggers: " << triggerCount << "\n"
213  << "Frequency: " << freq << "kHz\n";
214  */
215 }
216 
217 
218 
220 {
221 
222  // std::cout<<"Event: "<<evt.event()<<"Run: "<<evt.run()<<std::endl;
223 
224  art::Handle<artdaq::Fragments> rawFragments;
225  evt.getByLabel(fRawDataLabel, fFragType, rawFragments);
226 
227  // Check if there is SSP data in this event
228  // Don't crash code if not present, just don't save anything
229  try { rawFragments->size(); }
230  catch(std::exception const&) {
231  mf::LogWarning("SSPTriggerAna") << "WARNING: Raw SSP data not found in event " << evt.event();
232  return;
233  }
234 
235  // Check that the data is valid
236  if(!rawFragments.isValid()){
237  mf::LogError("SSPTriggerAna") << "Run: " << evt.run()
238  << ", SubRun: " << evt.subRun()
239  << ", Event: " << evt.event()
240  << " is NOT VALID";
241  throw cet::exception("raw NOT VALID");
242  return;
243  }
244 
245 
246 
247  unsigned int numFragments = rawFragments->size();
248 
249  for (size_t idx = 0; idx < numFragments; ++idx) {
250  const auto& frag((*rawFragments)[idx]);
251  lbne::SSPFragment sspf(frag);
252 
253  unsigned int nTriggers = sspReform.CheckAndGetNTriggers(frag, sspf);
254 
255  const unsigned int* dataPointer = sspf.dataBegin();
256 
257 
258  for (unsigned int triggersProcessed = 0;
259  (nTriggers==0 || triggersProcessed < nTriggers) && dataPointer < sspf.dataEnd();
260  ++triggersProcessed) {
261 
262  //
263  // The elements of the OpDet Pulse
264  //
265  unsigned short OpChannel = -1; ///< Derived Optical channel
266  unsigned long FirstSample = 0; ///< first sample time in ticks
267 
268 
269  // Load the event header, advance the pointer
270  auto daqHeader = sspReform.GetHeaderAndAdvance(dataPointer);
271  //const SSPDAQ::EventHeader* daqHeader=reinterpret_cast<const SSPDAQ::EventHeader*>(dataPointer);
272  //dataPointer += sizeof(SSPDAQ::EventHeader)/sizeof(unsigned int);
273 
274  // Get ADC Count, create pointer to adcs
275  unsigned int nADC = sspReform.GetWaveformLength(daqHeader);
276 
277  //get the information from the header
278  try {
279  OpChannel = sspReform.GetOpChannel(daqHeader);
280 
281  FirstSample = sspReform.GetGlobalFirstSample(daqHeader);
282  //TimeStamp = ((double)FirstSample)/fNOvAClockFrequency;
283 
284  if (FirstSample < 1e16) {
285  sspReform.PrintHeaderInfo(daqHeader);
286  mf::LogInfo("SSPTriggerAna") << "Problem timestamp at " << FirstSample << std::endl;
287  continue;
288  }
289  }
290  catch (cet::exception const&) {
291  continue;
292  }
293 
294  firstTime = std::min(firstTime, FirstSample);
295  lastTime = std::max(lastTime, FirstSample);
296 
297  triggerCount[OpChannel]++;
298  fTriggerRateHist->Fill(OpChannel);
299 
300  if (nADC == 0) {
301  // Header only
302  triggerCountHeaders[OpChannel]++;
303  fTriggerRateHistHeaders->Fill(OpChannel);
304  }
305  else {
306  // Waveform to skip
307  triggerCountWaveforms[OpChannel]++;
308  fTriggerRateHistWaveforms->Fill(OpChannel);
309 
310  // Advance the dataPointer to the next header
311  dataPointer+=nADC/2;
312  }
313 
314 
315  } // End of loop over triggers
316  } // End of loop over fragments (rawFragments)
317 
318 
319 }
320 
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
EventNumber_t event() const
Definition: DataViewImpl.cc:96
SSPTriggerAna & operator=(SSPTriggerAna const &)=delete
std::map< int, long int > triggerCountWaveforms
void beginRun(art::Run const &run) override
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
unsigned int CheckAndGetNTriggers(const artdaq::Fragment &frag, const lbne::SSPFragment sspf)
Load the milislice.
std::map< int, long int > triggerCountHeaders
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:27
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
Definition: Run.h:21
unsigned int GetWaveformLength(const SSPDAQ::EventHeader *daqHeader)
const SSPDAQ::EventHeader * GetHeaderAndAdvance(const unsigned int *&dataPointer)
Load the header.
bool isValid() const
Definition: Handle.h:183
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:446
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:69
parameter set interface
unsigned long GetGlobalFirstSample(const SSPDAQ::EventHeader *daqHeader)
Collect all the RawData header files together.
T get(std::string const &key) const
Definition: ParameterSet.h:231
SSPTriggerAna(fhicl::ParameterSet const &pset)
std::map< int, long int > triggerCount
void endRun(art::Run const &run) override
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:89
RunNumber_t run() const
Definition: DataViewImpl.cc:82
static int max(int a, int b)
unsigned short GetOpChannel(const SSPDAQ::EventHeader *daqHeader)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
void analyze(art::Event const &evt) override
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void reconfigure(fhicl::ParameterSet const &pset)
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void PrintHeaderInfo(const SSPDAQ::EventHeader *daqHeader)
Print out header information.
double ClockFrequency()
Return the NOvAClockFrequency.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
unsigned int run