All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRTArtdaqFragmentProducer_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTArtdaqFragmentProducer
3 // Module Type: producer
4 // File: CRTArtdaqFragmentProducer_module.cc
5 //
6 // Producer module for creating CRT DAQ-readable fragment from simulation
7 //
8 //
9 // Erin Yandel (eyandel@fnal.gov)
10 ////////////////////////////////////////////////////////////////////////
11 
12 // sbndcode includes
23 
24 
25 // LArSoft includes
32 #include "nusimdata/SimulationBase/MCParticle.h"
33 #include "nusimdata/SimulationBase/MCTruth.h"
34 
35 // artdaq includes
36 #include "sbndaq-artdaq-core/Overlays/Common/BernCRTFragmentV2.hh"
37 #include "sbndaq-artdaq-core/Overlays/FragmentType.hh"
38 #include "artdaq-core/Data/Fragment.hh"
39 
40 // Framework includes
41 #include "art/Framework/Core/EDProducer.h"
42 #include "art/Framework/Principal/Event.h"
43 #include "art/Framework/Principal/Handle.h"
44 #include "art/Framework/Services/Registry/ServiceHandle.h"
45 #include "art_root_io/TFileService.h"
46 #include "art/Framework/Core/ModuleMacros.h"
47 #include "canvas/Persistency/Common/FindManyP.h"
48 #include "canvas/Utilities/Exception.h"
51 
52 // Utility libraries
53 #include "messagefacility/MessageLogger/MessageLogger.h"
54 #include "fhiclcpp/ParameterSet.h"
55 #include "fhiclcpp/types/Table.h"
56 #include "fhiclcpp/types/Atom.h"
57 #include "cetlib/pow.h"
58 
59 // ROOT includes.
60 #include "TH1.h"
61 #include "TH2.h"
62 #include "TVector3.h"
63 #include "TString.h"
64 
65 // C++ includes
66 #include <map>
67 #include <vector>
68 #include <string>
69 #include <random>
70 #include <iomanip>
71 
72 namespace sbnd {
73  namespace trigger {
74  class CRTArtdaqFragmentProducer;
75  }
76 }
77 
78 class sbnd::trigger::CRTArtdaqFragmentProducer : public art::EDProducer {
79 public:
80  explicit CRTArtdaqFragmentProducer(fhicl::ParameterSet const& p);
81 
86 
87  // Required functions.
88  void produce(art::Event& e) override;
89 
90 
91 private:
92 
93  int fRun;
94  int fSubrun;
95  int fEvent;
96 
97  // fcl file parameters
98  art::InputTag fSimModuleLabel; ///< name of detsim producer
99  art::InputTag fCRTSimLabel; ///< name of CRT producer
100  std::string fFEBDataLabel; ///< name of FEBData producer
101  bool fVerbose; ///< print information about what's going on
103  size_t fFirstFEBMac5; ///< lowest mac5 address for CRT FEBs
104 
105  // Other variables shared between different methods.
108 
109  //limits for array sizes
110  enum LIMITS{
112  num_febs = 255,
114  };
115 
116  //arrays to store FEBinfo by module (want one fragment per module per event)
117  std::string taggers[num_febs];
123 
124 
125 
126 
127 }; // class CRTArtdaqFragmentProducer
128 
129 
131  : EDProducer{p},
132  fSimModuleLabel(p.get<std::string>("SimModuleLabel", "largeant")),
133  fCRTSimLabel(p.get<std::string>("CRTSimLabel", "crt")),
134  fFEBDataLabel(p.get<std::string>("FEBDataLabel", "crtsim")),
135  fVerbose(p.get<bool>("Verbose", false)),
136  fClockSpeedCRT(p.get<double>("ClockSpeedCRT")),
137  fFirstFEBMac5(p.get<size_t>("FirstFEBMac5", 0))
138  // More initializers here.
139 {
140  // Call appropriate produces<>() functions here.
141  produces< std::vector<artdaq::Fragment> >();
142 
143  // Get a pointer to the fGeometryServiceetry service provider
144  fGeometryService = lar::providerFrom<geo::Geometry>();
145 
146 }
147 
148 
150 {
151 
152  // Fetch basic event info
153  // event information
154  fRun = e.run();
155  fSubrun = e.subRun();
156  fEvent = e.id().event();
157  if(fVerbose){
158  std::cout<<"============================================"<<std::endl
159  <<"Run = "<<fRun<<", SubRun = "<<fSubrun<<", Event = "<<fEvent<<std::endl
160  <<"============================================"<<std::endl;
161  }
162 
163 
164  for(int i=0; i<num_febs; i++){empty_fragment[i]=true;}
165 
166  int num_module = fCrtGeo.NumModules();
167 
168  //----------------------------------------------------------------------------------------------------------
169  // GETTING PRODUCTS
170  //----------------------------------------------------------------------------------------------------------
171 
172  // Get FEB data from the event
173  art::Handle<std::vector<sbnd::crt::FEBData>> feb_data_h;
174  e.getByLabel(fFEBDataLabel, feb_data_h);
175 
176  // make sure hits look good
177  if (!feb_data_h.isValid()) {
178  throw art::Exception(art::errors::Configuration) << "could not locate FEBData." << std::endl;;
179  }
180 
181  std::vector<art::Ptr<sbnd::crt::FEBData>> feb_data_v;
182  art::fill_ptr_vector(feb_data_v, feb_data_h);
183 
184  art::FindManyP<sim::AuxDetIDE, sbnd::crt::FEBTruthInfo> febdata_to_ides (feb_data_h, e, fFEBDataLabel);
185 
186  //Fill arrays with default values
187  std::fill(feb_hits_in_fragments, feb_hits_in_fragments + sizeof(feb_hits_in_fragments), 0);
188 
189  // fragments vector
190  std::unique_ptr<std::vector<artdaq::Fragment>> vecFrag = std::make_unique<std::vector<artdaq::Fragment>>();
191 
192  // set properties of fragment that are common to event
193  //quantities in fragment
194  uint16_t lostcpu = 0;
195  uint16_t lostfpga = 0;
196  uint32_t coinc = 0;
197  uint16_t lost_hits = 0; //number of lost hits from the previous one
198 
199 
200  //metadata
201  uint64_t run_start_time = 0;
202  uint64_t this_poll_start = 0;
203  uint64_t this_poll_end = 0;
204  int32_t system_clock_deviation = 0;
205  uint32_t feb_hits_in_poll = 0;
206 
207  //information from fragment header
208  uint64_t sequence_id = fEvent;
209 
210  uint64_t temp_last_time = 0;
211 
212  //random number generator to be used to simulate pedestal
213  std::default_random_engine generator;
214  std::normal_distribution<double> distribution(175.0,23.0);
215 
216  //list all fragment ids - uncomment if needed for daq running
217  /*if (fVerbose){std::cout<<"Num Modules: "<<fCrtGeo.NumModules()<<std::endl;}
218  for (size_t mod_i = 0; mod_i<fCrtGeo.NumModules(); mod_i++){
219  int plane = sbnd::CRTCommonUtils::GetPlaneIndex(fCrtGeo.GetTaggerName(fCrtGeo.ChannelToStripName(mod_i * 32)));
220  if (fVerbose){std::cout << std::hex << (32768 + 12288 + (plane * 256) + (int)mod_i) << std::endl;}
221  } */
222 
223  //loop through FEBData product and sort by module/mac5
224  for (size_t feb_i = 0; feb_i < feb_data_v.size(); feb_i++) {
225 
226  auto const feb_data = feb_data_v[feb_i];
227 
228  if(fVerbose){std::cout << "FEB " << feb_i << " with mac " << feb_data->Mac5() << std::endl;}
229 
230  empty_fragment[feb_data->Mac5()] = false;
231 
232  int channel = feb_data->Mac5() * 32;
233  std::string stripName = fCrtGeo.ChannelToStripName(channel);
234  std::string tagger = fCrtGeo.GetTaggerName(stripName);
235  taggers[feb_data->Mac5()] = tagger;
236 
237  T0s[feb_data->Mac5()][feb_hits_in_fragments[feb_data->Mac5()]] = feb_data->Ts0();
238  T1s[feb_data->Mac5()][feb_hits_in_fragments[feb_data->Mac5()]] = feb_data->Ts1();
239  for (int i_adc = 0; i_adc<32; i_adc++){
240  uint16_t adc = 0;
241  if (feb_data->ADC()[i_adc] > 4089){
242  adc = 4089;
243  }else if (feb_data->ADC()[i_adc] == 0){
244  //pull from a normal distribution to simulate pedestal
245  adc = distribution(generator);
246  }else{
247  adc = feb_data->ADC()[i_adc];
248  }
249  ADCs[feb_data->Mac5()][feb_hits_in_fragments[feb_data->Mac5()]][i_adc] = adc;
250  }
251 
252  feb_hits_in_fragments[feb_data->Mac5()]++;
253 
254  }//FEBData loop
255 
256  //make one fragment for every module
257  for (size_t feb_i = fFirstFEBMac5; feb_i < num_module+fFirstFEBMac5; feb_i++){
258  //quantities in fragment
259  if (empty_fragment[feb_i]){
260  //if no hits for a module, make a simulated "T1 reset" event to avoid missing fragments
261  feb_hits_in_fragments[feb_i] = 1;
262  int channel = feb_i * 32;
263  std::string stripName = fCrtGeo.ChannelToStripName(channel);
264  std::string tagger = fCrtGeo.GetTaggerName(stripName);
265  taggers[feb_i] = tagger;
266 
267  T0s[feb_i][0] = 0;
268  T1s[feb_i][0] = 0;
269  for (int i_adc = 0; i_adc<32; i_adc++){
270  uint16_t adc = distribution(generator);
271  ADCs[feb_i][0][i_adc] = adc;
272  }
273  }
274 
275  if (feb_hits_in_fragments[feb_i]==0) continue;
276 
277  //metadata
278  uint8_t mac5 = (uint8_t)feb_i; //last 8 bits of FEB mac5 address
279  uint16_t feb_hits_in_fragment = feb_hits_in_fragments[feb_i];
280 
281  sbndaq::BernCRTFragmentMetadataV2 metadata;
282 
283  metadata.set_mac5(mac5);
284  metadata.set_clock_deviation(system_clock_deviation);
285  metadata.set_hits_in_poll(feb_hits_in_poll);
286  metadata.set_hits_in_fragment(feb_hits_in_fragment);
287  metadata.set_run_start_time(run_start_time);
288  metadata.update_poll_time(this_poll_start, this_poll_end);
289 
290  uint64_t timestamp = (uint64_t)(T0s[feb_i][feb_hits_in_fragments[feb_i]]/fClockSpeedCRT); //absolute timestamp
291 
292  // create fragment
293  sbnd::CRTPlane plane_num = sbnd::CRTCommonUtils::GetPlaneIndex(taggers[feb_i]);
294  uint16_t fragmentIDVal = 32768 + 12288 + (plane_num * 256) + (uint16_t)mac5;
295  if(fVerbose){std::cout<<"fragmentID: "<<std::bitset<16>{fragmentIDVal}<<std::endl;}
296  auto fragment_uptr = artdaq::Fragment::FragmentBytes(sizeof(sbndaq::BernCRTHitV2)*metadata.hits_in_fragment(), //payload_size
297  sequence_id,
298  fragmentIDVal,
299  sbndaq::detail::FragmentType::BERNCRTV2,
300  metadata,
301  timestamp
302  ); // unique pointer
303 
304  // populate fragment
305  std::vector<sbndaq::BernCRTHitV2> data_vec;
306  for (int i_frag = 0; i_frag<feb_hits_in_fragments[feb_i]; i_frag++){
307  sbndaq::BernCRTHitV2 hit;
308 
309  uint8_t flags = 3;
310  uint32_t ts0 = T0s[feb_i][i_frag];
311  uint32_t ts1 = T1s[feb_i][i_frag];
312 
313  if (ts1==0){flags=11;}else if (ts0==0){flags=7;}
314 
315  uint64_t feb_hit_number = feb_hits_in_fragments[feb_i]; //hit counter for individual FEB, including hits lost in FEB or fragment generator
316  timestamp = (uint64_t)ts0/fClockSpeedCRT; //absolute timestamp
317  uint64_t last_accepted_timestamp = temp_last_time; //timestamp of previous accepted hit
318  temp_last_time = timestamp;
319 
320  hit.flags = (uint16_t)flags;
321  hit.lostcpu = (uint16_t)lostcpu;
322  hit.lostfpga = (uint16_t)lostfpga;
323  hit.ts0 = (uint32_t)ts0;
324  hit.ts1 = (uint32_t)ts1;
325  for (int i_adc = 0; i_adc<32; i_adc++){
326  hit.adc[i_adc] = ADCs[feb_i][i_frag][i_adc];
327  }
328  hit.coinc = (uint32_t)coinc;
329  hit.feb_hit_number = (uint64_t)feb_hit_number;
330  hit.timestamp = (uint64_t)timestamp;
331  hit.last_accepted_timestamp = (uint64_t)last_accepted_timestamp;
332  hit.lost_hits = (uint16_t)lost_hits;
333 
334  data_vec.emplace_back(hit);
335  }//bern crt hit vector
336 
337  //copy data vector of hits into the fragment we made
338  std::memcpy(fragment_uptr->dataBeginBytes(), data_vec.data(), sizeof(sbndaq::BernCRTHitV2)*metadata.hits_in_fragment());
339 
340  // add fragment to vector
341  vecFrag->push_back(*fragment_uptr);
342 
343  }//module (mac5) loop
344 
345  if(fVerbose) std::cout << "Fragments written: " << vecFrag->size() << std::endl;
346 
347  e.put(std::move(vecFrag));
348 
349 
350 } // CRTArtdaqFragmentProducer::produce()
351 
352 
353 
bool fVerbose
print information about what&#39;s going on
size_t fFirstFEBMac5
lowest mac5 address for CRT FEBs
art::InputTag fSimModuleLabel
name of detsim producer
pdgs p
Definition: selectors.fcl:22
process_name hit
Definition: cheaterreco.fcl:51
Access the description of detector geometry.
object containing MC truth information necessary for making RawDigits and doing back tracking ...
art framework interface to geometry description for auxiliary detectors
enum::sbnd::CRTPlane GetPlaneIndex(std::string tagger)
CRTArtdaqFragmentProducer & operator=(CRTArtdaqFragmentProducer const &)=delete
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
uint32_t T1s[num_febs][max_hits_in_fragment]
Description of geometry of one entire detector.
Definition of data types for geometry description.
uint16_t ADCs[num_febs][max_hits_in_fragment][num_channels]
do i e
uint32_t T0s[num_febs][max_hits_in_fragment]
stream1 can override from command line with o or output services user sbnd
art framework interface to geometry description
BEGIN_PROLOG could also be cout