TimingRawDecoder_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: TimingRawDecoder
3 // Module Type: producer
4 // File: TimingRawDecoder_module.cc
5 //
6 // Generated at Thu Jul 6 18:31:48 2017 by Antonino Sergi,32 1-A14,+41227678738, using artmod
7 // from cetpkgsupport v1_11_00.
8 ////////////////////////////////////////////////////////////////////////
9 
10 // art includes
17 #include "fhiclcpp/ParameterSet.h"
20 #include "art_root_io/TFileService.h"
21 
22 // artdaq and dune-raw-data includes
24 
26 
27 // larsoft includes
29 
30 // ROOT includes
31 #include "TH1.h"
32 
33 // C++ Includes
34 #include <memory>
35 #include <iostream>
36 #include <fstream>
37 
38 namespace dune {
39  class TimingRawDecoder;
40 }
41 
42 using std::string;
43 using std::cout;
44 using std::endl;
45 using std::ofstream;
46 
48 public:
49  explicit TimingRawDecoder(fhicl::ParameterSet const & p);
50  // The destructor generated by the compiler is fine for classes
51  // without bare pointers or other resource use.
52 
53  // Plugins should not be copied or assigned.
54  TimingRawDecoder(TimingRawDecoder const &) = delete;
56  TimingRawDecoder & operator = (TimingRawDecoder const &) = delete;
58 
59  // Required functions.
60  void produce(art::Event & e) override;
61  void reconfigure(const fhicl::ParameterSet &pset);
62  void printParameterSet();
63  void beginJob() override;
64 
65  void setRootObjects();
66 
67 private:
68 
69  // Declare member data here.
73  bool fDebug;
74  bool fMakeTree;
75  bool fMakeEventTimeFile = false;
76 
77  TH1I * fHTimestamp;
78  TH1I * fHTrigType;
80 
81  ULong64_t fPrevTimestamp;
82 };
83 
84 
86  : EDProducer{pset}
87 // Initialize member data here.
88 {
90  //fs->registerFileSwitchCallback(this, &TimingRawDecoder::setRootObjects);
92 
93  reconfigure(pset);
94  // Call appropriate produces<>() functions here.
95  produces< std::vector<raw::RDTimeStamp> > (fOutputDataLabel);
96  produces< std::vector<dune::ProtoDUNETimeStamp> > (fOutputDataLabel);
97 }
98 
100 
101  fRawDataLabel = pset.get<std::string>("RawDataLabel");
102  fOutputDataLabel = pset.get<std::string>("OutputDataLabel");
103  fUseChannelMap = pset.get<bool>("UseChannelMap");
104  fDebug = pset.get<bool>("Debug");
105  fMakeTree = pset.get<bool>("MakeTree");
106  pset.get_if_present<bool>("MakeEventTimeFile", fMakeEventTimeFile);
107  fPrevTimestamp=0;
108  if(fDebug) printParameterSet();
109 
110 }
111 
113 
114  for(int i=0;i<20;i++) std::cout << "=";
115  std::cout << std::endl;
116  std::cout << "Parameter Set" << std::endl;
117  for(int i=0;i<20;i++) std::cout << "=";
118  std::cout << std::endl;
119 
120  std::cout << "fRawDataLabel: " << fRawDataLabel << std::endl;
121  std::cout << "fOutputDataLabel: " << fOutputDataLabel << std::endl;
122  std::cout << "fDebug: ";
123  if(fDebug) std::cout << "true" << std::endl;
124  else std::cout << "false" << std::endl;
125 
126  for(int i=0;i<20;i++) std::cout << "=";
127  std::cout << std::endl;
128 }
129 
132 
133  fHTimestamp = tFileService->make<TH1I>("Timestamp","Timing Timestamp", 1e3, 0, 1e9);
134  fHTimestamp->GetXaxis()->SetTitle("Timestamp (ms)");
135 
136  fHTimestampDelta = tFileService->make<TH1I>("TimestampDelta","Timing Timestamp Delta", 100, 0, 1e3);
137  fHTimestampDelta->GetXaxis()->SetTitle("Delta Timestamp (ms)");
138 
139  fHTrigType = tFileService->make<TH1I>("TrigType","Timing trigger type", 20, -0.5, 19.5);
140  fHTrigType->GetXaxis()->SetTitle("Trigger type");
141 
142 
143 }
145 }
146 
148  //std::cout<<"-------------------- Timing RawDecoder -------------------"<<std::endl;
149  // Implementation of required member function here.
150  art::InputTag itag1(fRawDataLabel, "TIMING");
151  auto rawFragments = evt.getHandle<artdaq::Fragments>(itag1);
152 
153  art::EventNumber_t eventNumber = evt.event();
154  art::RunNumber_t runNumber = evt.run();
155 
156  std::vector<raw::RDTimeStamp> rdtimestamps;
157  std::vector<dune::ProtoDUNETimeStamp> pdtimestamps;
158 
159  if(rawFragments.isValid()){
160 
161  ULong64_t evtTimestamp = 0;
162  for(auto const& rawFrag : *rawFragments){
163  dune::TimingFragment frag(rawFrag);
164  // Try to determine the version of the fragment. This is
165  // complicated because early versions of the fragment didn't
166  // have a metadata object with the version, so for those, we
167  // have to look at the size of the fragment (actually the size
168  // of the payload, which excludes the header and metadata)
169  int fragmentVersion=0;
170  if(rawFrag.hasMetadata()){
172  fragmentVersion=metadata->fragment_version;
173  }
174  else{
175  size_t dataSizeBytes=rawFrag.dataSizeBytes();
176  // The first version of the fragment had 6 uint32_t words
177  if(dataSizeBytes==6*sizeof(uint32_t)){
178  fragmentVersion=1;
179  }
180  // The second version of the fragment had 12 uint32_t words,
181  // as the last spill start/end and last run start timestamps
182  // were added
183  else if(dataSizeBytes==12*sizeof(uint32_t)){
184  fragmentVersion=2;
185  }
186  else{
187  throw cet::exception("TimingRawDecoder::produce") << "Fragment had no metadata and unexpected size " << dataSizeBytes << " bytes. Can't determined timing fragment version";
188  }
189  }
190 
191  //std::cout << " Run " << runNumber << ", event " << eventNumber << ": ArtDaq Fragment Timestamp: " << std::dec << rawFrag.timestamp() << std::endl;
192  ULong64_t currentTimestamp=frag.get_tstamp();
193  uint16_t scmd = (frag.get_scmd() & 0xFFFF); // mask this just to make sure. Though scmd only has four relevant bits, the method is declared uint32_t.
194  rdtimestamps.emplace_back(currentTimestamp,scmd);
195 
197  pdts.setCookie(frag.get_cookie());
199  pdts.setReservedBits(frag.get_tcmd());
200  pdts.setTimeStamp(frag.get_tstamp());
201  pdts.setEventCounter(frag.get_evtctr());
202  // TODO: Checksum isn't set by the board reader yet
203  pdts.setChecksumGood(true);
204  pdts.setVersion(fragmentVersion);
205  // The additional timestamps were not added until version 2 of the timing fragment
206  if(fragmentVersion>=2){
210  }
211  else{
212  // Set to 0xfff... if not present
213  pdts.setLastRunStart(~0ul);
214  pdts.setLastSpillStart(~0ul);
215  pdts.setLastSpillEnd(~0ul);
216  }
217 
218  pdtimestamps.push_back(pdts);
219 
220  fHTimestamp->Fill(currentTimestamp/1e6);
221  fHTrigType->Fill(frag.get_scmd());
222 
223  if(fPrevTimestamp!=0) fHTimestampDelta->Fill((currentTimestamp-fPrevTimestamp)/1e6);
224 
225  fPrevTimestamp=currentTimestamp;
226  // fHTimestamp->Fill(rawFrag.timestamp());
227 
228  if ( fMakeEventTimeFile ) {
229  if ( evtTimestamp == 0 ) {
230  evtTimestamp = rawFrag.timestamp();
231  string foutName = "artdaqTimestamp-Run" + std::to_string(runNumber);
232  int subrun = evt.subRun();
233  if ( subrun != 1 ) {
234  cout << "TimingRawDecoder::produce: WARNING: Unexpected subrun number: " << subrun << endl;
235  foutName += "-Sub" + std::to_string(subrun);
236  }
237  foutName += "-Event" + std::to_string(eventNumber) + ".dat";
238  ofstream fout(foutName);
239  fout << evtTimestamp << endl;
240  }
241  else
242  {
243  if ( rawFrag.timestamp() != evtTimestamp ) {
244  cout << "TimingRawDecoder::produce: WARNING: Fragments have inconsistent timestamps." << endl;
245  }
246  }
247  }
248  }
249  }
250  evt.put(std::make_unique<decltype(rdtimestamps)>(std::move(rdtimestamps)), fOutputDataLabel);
251  evt.put(std::make_unique<decltype(pdtimestamps)>(std::move(pdtimestamps)), fOutputDataLabel);
252 }
253 
uint64_t get_last_spillend_timestamp() const
EventNumber_t event() const
Definition: DataViewImpl.cc:85
void setLastSpillEnd(ULong64_t arg)
void produce(art::Event &e) override
void setLastSpillStart(ULong64_t arg)
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
void setTimeStamp(ULong64_t arg)
uint64_t get_last_runstart_timestamp() const
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
uint32_t get_tcmd() const
void setEventCounter(UInt_t arg)
static constexpr double fs
Definition: Units.h:100
void reconfigure(const fhicl::ParameterSet &pset)
void setTriggerType(dune::ProtoDUNETimingCommand arg)
void setLastRunStart(ULong64_t arg)
const double e
void setReservedBits(UInt_t arg)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
uint32_t get_cookie() const
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
uint64_t get_last_spillstart_timestamp() const
p
Definition: test.py:223
TimingRawDecoder & operator=(TimingRawDecoder const &)=delete
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
RunNumber_t run() const
Definition: DataViewImpl.cc:71
uint64_t get_tstamp() const
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
uint32_t get_scmd() const
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:118
TimingRawDecoder(fhicl::ParameterSet const &p)
uint32_t get_evtctr() const
TCEvent evt
Definition: DataStructs.cxx:7
ProtoDUNETimingCommand
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
std::vector< Fragment > Fragments
Definition: HDF5Utils.h:57
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:120