Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:00:31

0001 #include "EventFilter/Utilities/interface/DAQSource.h"
0002 #include "EventFilter/Utilities/interface/DAQSourceModelsScouting.h"
0003 
0004 #include <sstream>
0005 #include <sys/types.h>
0006 #include <vector>
0007 #include <regex>
0008 
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0011 #include "DataFormats/Provenance/interface/EventID.h"
0012 
0013 using namespace scouting;
0014 
0015 void DataModeScoutingRun2Muon::readEvent(edm::EventPrincipal& eventPrincipal) {
0016   edm::TimeValue_t time;
0017   timeval stv;
0018   gettimeofday(&stv, nullptr);
0019   time = stv.tv_sec;
0020   time = (time << 32) + stv.tv_usec;
0021   edm::Timestamp tstamp(time);
0022 
0023   std::unique_ptr<BXVector<l1t::Muon>> rawData(new BXVector<l1t::Muon>);
0024   //allow any bx
0025   rawData->setBXRange(0, 4000);
0026 
0027   unpackOrbit(rawData.get(), (char*)event_->payload(), event_->eventSize());
0028 
0029   uint32_t hdrEventID = event_->event();
0030   edm::EventID eventID = edm::EventID(daqSource_->eventRunNumber(), daqSource_->currentLumiSection(), hdrEventID);
0031   edm::EventAuxiliary aux(
0032       eventID, daqSource_->processGUID(), tstamp, event_->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
0033 
0034   aux.setProcessHistoryID(daqSource_->processHistoryID());
0035   daqSource_->makeEventWrapper(eventPrincipal, aux);
0036 
0037   std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawData)));
0038   eventPrincipal.put(
0039       daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
0040 }
0041 
0042 void DataModeScoutingRun2Muon::unpackOrbit(BXVector<l1t::Muon>* muons, char* buf, size_t len) {
0043   using namespace scouting;
0044   size_t pos = 0;
0045   uint32_t o_test = 0;
0046   while (pos < len) {
0047     assert(pos + 4 <= len);
0048     uint32_t header = *((uint32*)(buf + pos));
0049     uint32_t mAcount = (header & header_masks::mAcount) >> header_shifts::mAcount;
0050     uint32_t mBcount = (header & header_masks::mBcount) >> header_shifts::mBcount;
0051 
0052     block* bl = (block*)(buf + pos + 4);
0053 
0054     pos += 12 + (mAcount + mBcount) * 8;
0055     assert(pos <= len);
0056 
0057     uint32_t bx = bl->bx;
0058 
0059     uint32_t orbit = bl->orbit;
0060     o_test = orbit;
0061 
0062     //should cuts should be applied
0063     bool excludeIntermediate = true;
0064 
0065     for (size_t i = 0; i < (mAcount + mBcount); i++) {
0066       //unpack new muon
0067       //variables: index, ietaext, ipt, qual, iphiext, iso, chrg, iphi, ieta
0068 
0069       // remove intermediate if required
0070       // index==0 and ietaext==0 are a necessary and sufficient condition
0071       uint32_t index = (bl->mu[i].s >> shifts::index) & masks::index;
0072       int32_t ietaext = ((bl->mu[i].f >> shifts::etaext) & masks::etaextv);
0073       if (((bl->mu[i].f >> shifts::etaext) & masks::etaexts) != 0)
0074         ietaext -= 256;
0075 
0076       if (excludeIntermediate && index == 0 && ietaext == 0)
0077         continue;
0078 
0079       //extract pt and quality and apply cut if required
0080       uint32_t ipt = (bl->mu[i].f >> shifts::pt) & masks::pt;
0081       //cuts??
0082       //        if((ipt-1)<ptcut) {discarded++; continue;}
0083       uint32_t qual = (bl->mu[i].f >> shifts::qual) & masks::qual;
0084       //        if(qual < qualcut) {discarded++; continue;}
0085 
0086       //extract integer value for extrapolated phi
0087       int32_t iphiext = ((bl->mu[i].f >> shifts::phiext) & masks::phiext);
0088 
0089       // extract iso bits and charge
0090       uint32_t iso = (bl->mu[i].s >> shifts::iso) & masks::iso;
0091       int32_t chrg = 0;
0092       if (((bl->mu[i].s >> shifts::chrgv) & masks::chrgv) == 1) {
0093         chrg = ((bl->mu[i].s >> shifts::chrg) & masks::chrg) == 1 ? -1 : 1;
0094       }
0095 
0096       // extract eta and phi at muon station
0097       int32_t iphi = ((bl->mu[i].s >> shifts::phi) & masks::phi);
0098       int32_t ieta = (bl->mu[i].s >> shifts::eta) & masks::etav;
0099       if (((bl->mu[i].s >> shifts::eta) & masks::etas) != 0)
0100         ieta -= 256;
0101 
0102       l1t::Muon muon(
0103           *dummyLVec_, ipt, ieta, iphi, qual, chrg, chrg != 0, iso, -1, 0, false, 0, 0, 0, 0, ietaext, iphiext);
0104       muons->push_back(bx, muon);
0105     }
0106   }
0107   std::cout << "end read ... " << o_test << std::endl << std::flush;
0108 }  //unpackOrbit
0109 
0110 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeScoutingRun2Muon::makeDaqProvenanceHelpers() {
0111   //set FRD data collection
0112   daqProvenanceHelpers_.clear();
0113   daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
0114       edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
0115   return daqProvenanceHelpers_;
0116 }
0117 
0118 bool DataModeScoutingRun2Muon::nextEventView() {
0119   if (eventCached_)
0120     return true;
0121   event_ = std::make_unique<FRDEventMsgView>(dataBlockAddr_);
0122   if (event_->size() > dataBlockMax_) {
0123     throw cms::Exception("DAQSource::getNextEvent")
0124         << " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
0125         << " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << dataBlockMax_ << " bytes";
0126   }
0127   return true;
0128 }
0129 
0130 bool DataModeScoutingRun2Muon::checksumValid() { return true; }
0131 
0132 std::string DataModeScoutingRun2Muon::getChecksumError() const { return std::string(); }
0133 
0134 //
0135 //2nd model: read multiple input files with different data type
0136 //
0137 
0138 std::pair<bool, std::vector<std::string>> DataModeScoutingRun2Multi::defineAdditionalFiles(
0139     std::string const& primaryName, bool fileListMode) const {
0140   std::vector<std::string> additionalFiles;
0141 
0142   auto fullpath = std::filesystem::path(primaryName);
0143   auto fullname = fullpath.filename();
0144   std::string stem = fullname.stem().string();
0145   std::string ext = fullname.extension().string();
0146   std::regex regexz("_");
0147   std::vector<std::string> nameTokens = {std::sregex_token_iterator(stem.begin(), stem.end(), regexz, -1),
0148                                          std::sregex_token_iterator()};
0149 
0150   if (nameTokens.size() < 3) {
0151     throw cms::Exception("DAQSource::getNextEvent")
0152         << primaryName << " name doesn't start with run#_ls#_index#_*.ext syntax";
0153   }
0154 
0155   //Can also filter out non-matching primary files (if detected by DaqDirector). false will tell source to skip the primary file.
0156   if (nameTokens.size() > 3 && nameTokens[3].rfind("secondary", 0) == 0)
0157     return std::make_pair(false, additionalFiles);
0158 
0159   //TODO: provisional, name syntax should be better defined
0160 
0161   additionalFiles.push_back(fullpath.parent_path().string() + "/" + nameTokens[0] + "_" + nameTokens[1] + "_" +
0162                             nameTokens[2] + "_secondary" + ext);
0163   //additionalFiles.push_back(fullpath.parent_path.string() + "/" + nameTokens[0] + "_" + nameTokens[1] + "_" + nameTokens[2] + "_tertiary" + ".raw");
0164 
0165   return std::make_pair(true, additionalFiles);
0166 }
0167 
0168 void DataModeScoutingRun2Multi::readEvent(edm::EventPrincipal& eventPrincipal) {
0169   edm::TimeValue_t time;
0170   timeval stv;
0171   gettimeofday(&stv, nullptr);
0172   time = stv.tv_sec;
0173   time = (time << 32) + stv.tv_usec;
0174   edm::Timestamp tstamp(time);
0175 
0176   std::unique_ptr<BXVector<l1t::Muon>> rawData(new BXVector<l1t::Muon>);
0177   //allow any bx
0178   rawData->setBXRange(0, 4000);
0179 
0180   unpackMuonOrbit(rawData.get(), (char*)events_[0]->payload(), events_[0]->eventSize());
0181 
0182   //TODO: implement here other object type (e.g. unpackCaloOrbit)
0183   //
0184   std::unique_ptr<BXVector<l1t::Muon>> rawDataSec(new BXVector<l1t::Muon>);
0185   //allow any bx
0186   rawDataSec->setBXRange(0, 4000);
0187 
0188   unpackMuonOrbit(rawDataSec.get(), (char*)events_[1]->payload(), events_[1]->eventSize());
0189 
0190   uint32_t hdrEventID = events_[0]->event();  //take from 1st file
0191   edm::EventID eventID = edm::EventID(daqSource_->eventRunNumber(), daqSource_->currentLumiSection(), hdrEventID);
0192   edm::EventAuxiliary aux(
0193       eventID, daqSource_->processGUID(), tstamp, events_[0]->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
0194 
0195   aux.setProcessHistoryID(daqSource_->processHistoryID());
0196   daqSource_->makeEventWrapper(eventPrincipal, aux);
0197 
0198   std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawData)));
0199   eventPrincipal.put(
0200       daqProvenanceHelpers_[0]->branchDescription(), std::move(edp), daqProvenanceHelpers_[0]->dummyProvenance());
0201 
0202   //TODO: use other object and provenance helper (duplicate is just for demonstration)
0203   //  std::unique_ptr<edm::WrapperBase> edpSec(new edm::Wrapper<BXVector<l1t::Muon>>(std::move(rawDataSec)));
0204   //  eventPrincipal.put(daqProvenanceHelpers_[1]->branchDescription(), std::move(edpSec), daqProvenanceHelpers_[1]->dummyProvenance());
0205 
0206   eventCached_ = false;
0207 }
0208 
0209 void DataModeScoutingRun2Multi::unpackMuonOrbit(BXVector<l1t::Muon>* muons, char* buf, size_t len) {
0210   using namespace scouting;
0211   size_t pos = 0;
0212   //uint32_t o_test = 0;
0213   while (pos < len) {
0214     assert(pos + 4 <= len);
0215     uint32_t header = *((uint32*)(buf + pos));
0216     uint32_t mAcount = (header & header_masks::mAcount) >> header_shifts::mAcount;
0217     uint32_t mBcount = (header & header_masks::mBcount) >> header_shifts::mBcount;
0218 
0219     block* bl = (block*)(buf + pos + 4);
0220 
0221     pos += 12 + (mAcount + mBcount) * 8;
0222     assert(pos <= len);
0223 
0224     uint32_t bx = bl->bx;
0225 
0226     //uint32_t orbit = bl->orbit;
0227     //o_test = orbit;
0228 
0229     //should cuts should be applied
0230     bool excludeIntermediate = true;
0231 
0232     for (size_t i = 0; i < (mAcount + mBcount); i++) {
0233       //unpack new muon
0234       //variables: index, ietaext, ipt, qual, iphiext, iso, chrg, iphi, ieta
0235 
0236       // remove intermediate if required
0237       // index==0 and ietaext==0 are a necessary and sufficient condition
0238       uint32_t index = (bl->mu[i].s >> shifts::index) & masks::index;
0239       int32_t ietaext = ((bl->mu[i].f >> shifts::etaext) & masks::etaextv);
0240       if (((bl->mu[i].f >> shifts::etaext) & masks::etaexts) != 0)
0241         ietaext -= 256;
0242 
0243       if (excludeIntermediate && index == 0 && ietaext == 0)
0244         continue;
0245 
0246       //extract pt and quality and apply cut if required
0247       uint32_t ipt = (bl->mu[i].f >> shifts::pt) & masks::pt;
0248       //cuts??
0249       //        if((ipt-1)<ptcut) {discarded++; continue;}
0250       uint32_t qual = (bl->mu[i].f >> shifts::qual) & masks::qual;
0251       //        if(qual < qualcut) {discarded++; continue;}
0252 
0253       //extract integer value for extrapolated phi
0254       int32_t iphiext = ((bl->mu[i].f >> shifts::phiext) & masks::phiext);
0255 
0256       // extract iso bits and charge
0257       uint32_t iso = (bl->mu[i].s >> shifts::iso) & masks::iso;
0258       int32_t chrg = 0;
0259       if (((bl->mu[i].s >> shifts::chrgv) & masks::chrgv) == 1) {
0260         chrg = ((bl->mu[i].s >> shifts::chrg) & masks::chrg) == 1 ? -1 : 1;
0261       }
0262 
0263       // extract eta and phi at muon station
0264       int32_t iphi = ((bl->mu[i].s >> shifts::phi) & masks::phi);
0265       int32_t ieta = (bl->mu[i].s >> shifts::eta) & masks::etav;
0266       if (((bl->mu[i].s >> shifts::eta) & masks::etas) != 0)
0267         ieta -= 256;
0268 
0269       l1t::Muon muon(
0270           *dummyLVec_, ipt, ieta, iphi, qual, chrg, chrg != 0, iso, -1, 0, false, 0, 0, 0, 0, ietaext, iphiext);
0271       muons->push_back(bx, muon);
0272     }
0273   }
0274 }  //unpackOrbit
0275 
0276 std::vector<std::shared_ptr<const edm::DaqProvenanceHelper>>& DataModeScoutingRun2Multi::makeDaqProvenanceHelpers() {
0277   //set FRD data collection
0278   daqProvenanceHelpers_.clear();
0279   daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
0280       edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
0281   //Note: two same kind of objects can not be put in the event from the source, so this example will be changed
0282   daqProvenanceHelpers_.emplace_back(std::make_shared<const edm::DaqProvenanceHelper>(
0283       edm::TypeID(typeid(l1t::MuonBxCollection)), "l1t::MuonBxCollection", "l1tMuonBxCollection", "DAQSource"));
0284   return daqProvenanceHelpers_;
0285 }
0286 
0287 bool DataModeScoutingRun2Multi::nextEventView() {
0288   blockCompleted_ = false;
0289   if (eventCached_)
0290     return true;
0291   for (unsigned int i = 0; i < events_.size(); i++) {
0292     //add last event length..
0293     dataBlockAddrs_[i] += events_[i]->size();
0294   }
0295   return makeEvents();
0296 }
0297 
0298 bool DataModeScoutingRun2Multi::makeEvents() {
0299   events_.clear();
0300   for (int i = 0; i < numFiles_; i++) {
0301     if (dataBlockAddrs_[i] >= dataBlockMaxAddrs_[i]) {
0302       blockCompleted_ = true;
0303       return false;
0304     }
0305     events_.emplace_back(std::make_unique<FRDEventMsgView>(dataBlockAddrs_[i]));
0306   }
0307   return true;
0308 }
0309 
0310 bool DataModeScoutingRun2Multi::checksumValid() { return true; }
0311 
0312 std::string DataModeScoutingRun2Multi::getChecksumError() const { return std::string(); }