CMS 3D CMS Logo

DAQSourceModelsScouting.cc
Go to the documentation of this file.
3 
4 #include <sstream>
5 #include <sys/types.h>
6 #include <vector>
7 #include <regex>
8 
12 
13 using namespace scouting;
14 
17  timeval stv;
18  gettimeofday(&stv, nullptr);
19  time = stv.tv_sec;
20  time = (time << 32) + stv.tv_usec;
21  edm::Timestamp tstamp(time);
22 
23  std::unique_ptr<BXVector<l1t::Muon>> rawData(new BXVector<l1t::Muon>);
24  //allow any bx
25  rawData->setBXRange(0, 4000);
26 
27  unpackOrbit(rawData.get(), (char*)event_->payload(), event_->eventSize());
28 
29  uint32_t hdrEventID = event_->event();
30  edm::EventID eventID = edm::EventID(daqSource_->eventRunNumber(), daqSource_->currentLumiSection(), hdrEventID);
32  eventID, daqSource_->processGUID(), tstamp, event_->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
33 
34  aux.setProcessHistoryID(daqSource_->processHistoryID());
35  daqSource_->makeEventWrapper(eventPrincipal, aux);
36 
37  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawData)));
38  eventPrincipal.put(
39  daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
40 }
41 
43  using namespace scouting;
44  size_t pos = 0;
45  uint32_t o_test = 0;
46  while (pos < len) {
47  assert(pos + 4 <= len);
48  uint32_t header = *((uint32*)(buf + pos));
49  uint32_t mAcount = (header & header_masks::mAcount) >> header_shifts::mAcount;
50  uint32_t mBcount = (header & header_masks::mBcount) >> header_shifts::mBcount;
51 
52  block* bl = (block*)(buf + pos + 4);
53 
54  pos += 12 + (mAcount + mBcount) * 8;
55  assert(pos <= len);
56 
57  uint32_t bx = bl->bx;
58 
59  uint32_t orbit = bl->orbit;
60  o_test = orbit;
61 
62  //should cuts should be applied
63  bool excludeIntermediate = true;
64 
65  for (size_t i = 0; i < (mAcount + mBcount); i++) {
66  //unpack new muon
67  //variables: index, ietaext, ipt, qual, iphiext, iso, chrg, iphi, ieta
68 
69  // remove intermediate if required
70  // index==0 and ietaext==0 are a necessary and sufficient condition
71  uint32_t index = (bl->mu[i].s >> shifts::index) & masks::index;
72  int32_t ietaext = ((bl->mu[i].f >> shifts::etaext) & masks::etaextv);
73  if (((bl->mu[i].f >> shifts::etaext) & masks::etaexts) != 0)
74  ietaext -= 256;
75 
76  if (excludeIntermediate && index == 0 && ietaext == 0)
77  continue;
78 
79  //extract pt and quality and apply cut if required
80  uint32_t ipt = (bl->mu[i].f >> shifts::pt) & masks::pt;
81  //cuts??
82  // if((ipt-1)<ptcut) {discarded++; continue;}
83  uint32_t qual = (bl->mu[i].f >> shifts::qual) & masks::qual;
84  // if(qual < qualcut) {discarded++; continue;}
85 
86  //extract integer value for extrapolated phi
87  int32_t iphiext = ((bl->mu[i].f >> shifts::phiext) & masks::phiext);
88 
89  // extract iso bits and charge
90  uint32_t iso = (bl->mu[i].s >> shifts::iso) & masks::iso;
91  int32_t chrg = 0;
92  if (((bl->mu[i].s >> shifts::chrgv) & masks::chrgv) == 1) {
93  chrg = ((bl->mu[i].s >> shifts::chrg) & masks::chrg) == 1 ? -1 : 1;
94  }
95 
96  // extract eta and phi at muon station
97  int32_t iphi = ((bl->mu[i].s >> shifts::phi) & masks::phi);
98  int32_t ieta = (bl->mu[i].s >> shifts::eta) & masks::etav;
99  if (((bl->mu[i].s >> shifts::eta) & masks::etas) != 0)
100  ieta -= 256;
101 
102  l1t::Muon muon(
103  *dummyLVec_, ipt, ieta, iphi, qual, chrg, chrg != 0, iso, -1, 0, false, 0, 0, 0, 0, ietaext, iphiext);
104  muons->push_back(bx, muon);
105  }
106  }
107  std::cout << "end read ... " << o_test << std::endl << std::flush;
108 } //unpackOrbit
109 
110 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeScoutingRun2Muon::makeDaqProvenanceHelpers() {
111  //set FRD data collection
112  daqProvenanceHelpers_.clear();
113  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
114  edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
115  return daqProvenanceHelpers_;
116 }
117 
119  if (eventCached_)
120  return true;
121  event_ = std::make_unique<FRDEventMsgView>(dataBlockAddr_);
122  if (event_->size() > dataBlockMax_) {
123  throw cms::Exception("DAQSource::getNextEvent")
124  << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
125  << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << dataBlockMax_ << " bytes";
126  }
127  return true;
128 }
129 
131 
133 
134 //
135 //2nd model: read multiple input files with different data type
136 //
137 
138 std::pair<bool, std::vector<std::string>> DataModeScoutingRun2Multi::defineAdditionalFiles(
139  std::string const& primaryName, bool fileListMode) const {
140  std::vector<std::string> additionalFiles;
141 
142  auto fullpath = std::filesystem::path(primaryName);
143  auto fullname = fullpath.filename();
144  std::string stem = fullname.stem().string();
145  std::string ext = fullname.extension().string();
146  std::regex regexz("_");
147  std::vector<std::string> nameTokens = {std::sregex_token_iterator(stem.begin(), stem.end(), regexz, -1),
148  std::sregex_token_iterator()};
149 
150  if (nameTokens.size() < 3) {
151  throw cms::Exception("DAQSource::getNextEvent")
152  << primaryName << " name doesn't start with run#_ls#_index#_*.ext syntax";
153  }
154 
155  //Can also filter out non-matching primary files (if detected by DaqDirector). false will tell source to skip the primary file.
156  if (nameTokens.size() > 3 && nameTokens[3].rfind("secondary", 0) == 0)
157  return std::make_pair(false, additionalFiles);
158 
159  //TODO: provisional, name syntax should be better defined
160 
161  additionalFiles.push_back(fullpath.parent_path().string() + "/" + nameTokens[0] + "_" + nameTokens[1] + "_" +
162  nameTokens[2] + "_secondary" + ext);
163  //additionalFiles.push_back(fullpath.parent_path.string() + "/" + nameTokens[0] + "_" + nameTokens[1] + "_" + nameTokens[2] + "_tertiary" + ".raw");
164 
165  return std::make_pair(true, additionalFiles);
166 }
167 
170  timeval stv;
171  gettimeofday(&stv, nullptr);
172  time = stv.tv_sec;
173  time = (time << 32) + stv.tv_usec;
174  edm::Timestamp tstamp(time);
175 
176  std::unique_ptr<BXVector<l1t::Muon>> rawData(new BXVector<l1t::Muon>);
177  //allow any bx
178  rawData->setBXRange(0, 4000);
179 
180  unpackMuonOrbit(rawData.get(), (char*)events_[0]->payload(), events_[0]->eventSize());
181 
182  //TODO: implement here other object type (e.g. unpackCaloOrbit)
183  //
184  std::unique_ptr<BXVector<l1t::Muon>> rawDataSec(new BXVector<l1t::Muon>);
185  //allow any bx
186  rawDataSec->setBXRange(0, 4000);
187 
188  unpackMuonOrbit(rawDataSec.get(), (char*)events_[1]->payload(), events_[1]->eventSize());
189 
190  uint32_t hdrEventID = events_[0]->event(); //take from 1st file
191  edm::EventID eventID = edm::EventID(daqSource_->eventRunNumber(), daqSource_->currentLumiSection(), hdrEventID);
193  eventID, daqSource_->processGUID(), tstamp, events_[0]->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
194 
195  aux.setProcessHistoryID(daqSource_->processHistoryID());
196  daqSource_->makeEventWrapper(eventPrincipal, aux);
197 
198  std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawData)));
199  eventPrincipal.put(
200  daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
201 
202  //TODO: use other object and provenance helper (duplicate is just for demonstration)
203  // std::unique_ptr<edm::WrapperBase> edpSec(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawDataSec)));
204  // eventPrincipal.put(daqProvenanceHelpers_[1]->branchDescription(), std::move(edpSec), daqProvenanceHelpers_[1]->dummyProvenance());
205 
206  eventCached_ = false;
207 }
208 
210  using namespace scouting;
211  size_t pos = 0;
212  //uint32_t o_test = 0;
213  while (pos < len) {
214  assert(pos + 4 <= len);
215  uint32_t header = *((uint32*)(buf + pos));
216  uint32_t mAcount = (header & header_masks::mAcount) >> header_shifts::mAcount;
217  uint32_t mBcount = (header & header_masks::mBcount) >> header_shifts::mBcount;
218 
219  block* bl = (block*)(buf + pos + 4);
220 
221  pos += 12 + (mAcount + mBcount) * 8;
222  assert(pos <= len);
223 
224  uint32_t bx = bl->bx;
225 
226  //uint32_t orbit = bl->orbit;
227  //o_test = orbit;
228 
229  //should cuts should be applied
230  bool excludeIntermediate = true;
231 
232  for (size_t i = 0; i < (mAcount + mBcount); i++) {
233  //unpack new muon
234  //variables: index, ietaext, ipt, qual, iphiext, iso, chrg, iphi, ieta
235 
236  // remove intermediate if required
237  // index==0 and ietaext==0 are a necessary and sufficient condition
238  uint32_t index = (bl->mu[i].s >> shifts::index) & masks::index;
239  int32_t ietaext = ((bl->mu[i].f >> shifts::etaext) & masks::etaextv);
240  if (((bl->mu[i].f >> shifts::etaext) & masks::etaexts) != 0)
241  ietaext -= 256;
242 
243  if (excludeIntermediate && index == 0 && ietaext == 0)
244  continue;
245 
246  //extract pt and quality and apply cut if required
247  uint32_t ipt = (bl->mu[i].f >> shifts::pt) & masks::pt;
248  //cuts??
249  // if((ipt-1)<ptcut) {discarded++; continue;}
250  uint32_t qual = (bl->mu[i].f >> shifts::qual) & masks::qual;
251  // if(qual < qualcut) {discarded++; continue;}
252 
253  //extract integer value for extrapolated phi
254  int32_t iphiext = ((bl->mu[i].f >> shifts::phiext) & masks::phiext);
255 
256  // extract iso bits and charge
257  uint32_t iso = (bl->mu[i].s >> shifts::iso) & masks::iso;
258  int32_t chrg = 0;
259  if (((bl->mu[i].s >> shifts::chrgv) & masks::chrgv) == 1) {
260  chrg = ((bl->mu[i].s >> shifts::chrg) & masks::chrg) == 1 ? -1 : 1;
261  }
262 
263  // extract eta and phi at muon station
264  int32_t iphi = ((bl->mu[i].s >> shifts::phi) & masks::phi);
265  int32_t ieta = (bl->mu[i].s >> shifts::eta) & masks::etav;
266  if (((bl->mu[i].s >> shifts::eta) & masks::etas) != 0)
267  ieta -= 256;
268 
269  l1t::Muon muon(
270  *dummyLVec_, ipt, ieta, iphi, qual, chrg, chrg != 0, iso, -1, 0, false, 0, 0, 0, 0, ietaext, iphiext);
271  muons->push_back(bx, muon);
272  }
273  }
274 } //unpackOrbit
275 
276 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeScoutingRun2Multi::makeDaqProvenanceHelpers() {
277  //set FRD data collection
278  daqProvenanceHelpers_.clear();
279  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
280  edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
281  //Note: two same kind of objects can not be put in the event from the source, so this example will be changed
282  daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
283  edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
284  return daqProvenanceHelpers_;
285 }
286 
288  blockCompleted_ = false;
289  if (eventCached_)
290  return true;
291  for (unsigned int i = 0; i < events_.size(); i++) {
292  //add last event length..
293  dataBlockAddrs_[i] += events_[i]->size();
294  }
295  return makeEvents();
296 }
297 
299  events_.clear();
300  for (int i = 0; i < numFiles_; i++) {
301  if (dataBlockAddrs_[i] >= dataBlockMaxAddrs_[i]) {
302  blockCompleted_ = true;
303  return false;
304  }
305  events_.emplace_back(std::make_unique<FRDEventMsgView>(dataBlockAddrs_[i]));
306  }
307  return true;
308 }
309 
311 
static constexpr uint32_t chrg
void readEvent(edm::EventPrincipal &eventPrincipal) override
static constexpr uint32_t index
static constexpr uint32_t etaexts
void unpackOrbit(BXVector< l1t::Muon > *muons, char *buf, size_t len)
static constexpr uint32_t etav
static constexpr uint32_t mBcount
void readEvent(edm::EventPrincipal &eventPrincipal) override
assert(be >=bs)
static constexpr uint32_t iso
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > & makeDaqProvenanceHelpers() override
static constexpr uint32_t qual
static constexpr uint32_t iso
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
static constexpr uint32_t chrgv
void unpackMuonOrbit(BXVector< l1t::Muon > *muons, char *buf, size_t len)
static constexpr uint32_t mAcount
static constexpr uint32_t index
static constexpr uint32_t phi
static constexpr uint32_t pt
std::pair< bool, std::vector< std::string > > defineAdditionalFiles(std::string const &primaryName, bool fileListMode) const override
static constexpr uint32_t chrgv
static constexpr uint32_t etaextv
static constexpr uint32_t phiext
unsigned long long TimeValue_t
Definition: Timestamp.h:21
unsigned int uint32
Definition: MsgTools.h:13
Definition: Muon.h:21
static constexpr uint32_t eta
static constexpr uint32_t pt
static constexpr uint32_t chrg
static constexpr uint32_t etaext
std::string getChecksumError() const override
static constexpr uint32_t phiext
static constexpr uint32_t mAcount
std::vector< std::shared_ptr< const edm::DaqProvenanceHelper > > & makeDaqProvenanceHelpers() override
static constexpr uint32_t mBcount
static constexpr uint32_t etas
static constexpr uint32_t phi
Definition: memstream.h:15
def move(src, dest)
Definition: eostools.py:511
std::string getChecksumError() const override
static constexpr uint32_t qual