CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GEMDigiToRawModule.cc
Go to the documentation of this file.
1 
28 
29 class GEMDigiToRawModule : public edm::global::EDProducer<edm::RunCache<GEMROMapping>> {
30 public:
33 
34  // global::EDProducer
35  std::shared_ptr<GEMROMapping> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
36  void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
37  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override{};
38 
39  // Fill parameters descriptions
40  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
41 
42 private:
46  bool useDBEMap_;
47 };
48 
51 
53  : event_type_(pset.getParameter<int>("eventType")),
54  digi_token(consumes<GEMDigiCollection>(pset.getParameter<edm::InputTag>("gemDigi"))),
55  useDBEMap_(pset.getParameter<bool>("useDBEMap")) {
56  produces<FEDRawDataCollection>();
57  if (useDBEMap_) {
58  gemEMapToken_ = esConsumes<GEMeMap, GEMeMapRcd, edm::Transition::BeginRun>();
59  }
60 }
61 
64  desc.add<edm::InputTag>("gemDigi", edm::InputTag("simMuonGEMDigis"));
65  desc.add<int>("eventType", 0);
66  desc.add<bool>("useDBEMap", false);
67  descriptions.add("gemPackerDefault", desc);
68 }
69 
70 std::shared_ptr<GEMROMapping> GEMDigiToRawModule::globalBeginRun(edm::Run const&, edm::EventSetup const& iSetup) const {
71  auto gemROmap = std::make_shared<GEMROMapping>();
72  if (useDBEMap_) {
73  const auto& eMap = iSetup.getData(gemEMapToken_);
74  auto gemEMap = std::make_unique<GEMeMap>(eMap);
75  gemEMap->convert(*gemROmap);
76  gemEMap.reset();
77  } else {
78  // no EMap in DB, using dummy
79  auto gemEMap = std::make_unique<GEMeMap>();
80  gemEMap->convertDummy(*gemROmap);
81  gemEMap.reset();
82  }
83  return gemROmap;
84 }
85 
87  auto fedRawDataCol = std::make_unique<FEDRawDataCollection>();
88 
90  iEvent.getByToken(digi_token, gemDigis);
91  if (!gemDigis.isValid()) {
92  iEvent.put(std::move(fedRawDataCol));
93  return;
94  }
95 
96  auto gemROMap = runCache(iEvent.getRun().index());
97 
98  std::vector<std::unique_ptr<GEMAMC13>> amc13s;
100 
101  int LV1_id = iEvent.id().event();
102  uint8_t BX_id(iEvent.bunchCrossing());
103  int OrN = iEvent.orbitNumber();
104 
105  // making map of bx GEMDigiCollection
106  // each bx will be saved as new GEMAMC13, so GEMDigiCollection needs to be split into bx
107  std::map<int, GEMDigiCollection> gemBxMap;
108  for (auto const& etaPart : *gemDigis) {
109  GEMDetId gemId = etaPart.first;
110  const GEMDigiCollection::Range& digis = etaPart.second;
111  for (auto digi = digis.first; digi != digis.second; ++digi) {
112  int bx = digi->bx();
113  auto search = gemBxMap.find(bx);
114  if (search != gemBxMap.end()) {
115  search->second.insertDigi(gemId, *digi);
116  } else {
117  GEMDigiCollection newGDC;
118  newGDC.insertDigi(gemId, *digi);
119  gemBxMap.insert(std::pair<int, GEMDigiCollection>(bx, newGDC));
120  }
121  }
122  }
123 
125  uint32_t amc13EvtLength = 0;
126  std::unique_ptr<GEMAMC13> amc13 = std::make_unique<GEMAMC13>();
127 
128  for (uint8_t amcNum = 0; amcNum <= GEMeMap::maxAMCs_; ++amcNum) {
129  uint32_t amcSize = 0;
130  std::unique_ptr<GEMAMC> amc = std::make_unique<GEMAMC>();
131 
132  for (uint8_t gebId = 0; gebId <= GEMeMap::maxGEBs_; ++gebId) {
133  std::unique_ptr<GEMOptoHybrid> optoH = std::make_unique<GEMOptoHybrid>();
134  GEMROMapping::chamEC geb_ec{fedId, amcNum, gebId};
135 
136  if (!gemROMap->isValidChamber(geb_ec))
137  continue;
138  GEMROMapping::chamDC geb_dc = gemROMap->chamberPos(geb_ec);
139 
140  auto vfats = gemROMap->getVfats(geb_dc.detId);
141  for (auto const& vfat_ec : vfats) {
142  GEMROMapping::vfatDC vfat_dc = gemROMap->vfatPos(vfat_ec);
143  GEMDetId gemId = vfat_dc.detId;
144  uint16_t vfatId = vfat_ec.vfatAdd;
145 
146  for (auto const& gemBx : gemBxMap) {
147  int bc = BX_id + gemBx.first;
148 
149  bool hasDigi = false;
150  uint64_t lsData = 0;
151  uint64_t msData = 0;
152 
153  GEMDigiCollection inBxGemDigis = gemBx.second;
154  const GEMDigiCollection::Range& range = inBxGemDigis.get(gemId);
155  for (GEMDigiCollection::const_iterator digiIt = range.first; digiIt != range.second; ++digiIt) {
156  const GEMDigi& digi = (*digiIt);
157 
158  int localStrip = digi.strip() - vfat_dc.localPhi * GEMeMap::maxChan_;
159 
160  // skip strips not in current vFat
161  if (localStrip < 0 || localStrip > GEMeMap::maxChan_ - 1)
162  continue;
163 
164  hasDigi = true;
165  GEMROMapping::stripNum stMap = {vfat_dc.vfatType, localStrip};
166  GEMROMapping::channelNum chMap = gemROMap->hitPos(stMap);
167 
168  if (chMap.chNum < 64)
169  lsData |= 1UL << chMap.chNum;
170  else
171  msData |= 1UL << (chMap.chNum - 64);
172 
173  LogDebug("GEMDigiToRawModule")
174  << "fed: " << fedId << " amc:" << int(amcNum) << " geb:" << int(gebId) << " vfat id:" << int(vfatId)
175  << ",type:" << vfat_dc.vfatType << " id:" << gemId << " ch:" << chMap.chNum << " st:" << digi.strip()
176  << " bx:" << digi.bx();
177  }
178 
179  if (!hasDigi)
180  continue;
181  // only make vfat with hits
182  amcSize += 3;
183  auto vfat = std::make_unique<GEMVFAT>(geb_dc.vfatVer, bc, LV1_id, vfatId, lsData, msData);
184  optoH->addVFAT(*vfat);
185  }
186  } // end of vfats in GEB
187 
188  if (!optoH->vFATs()->empty()) {
189  amcSize += 2;
190  optoH->setChamberHeader(optoH->vFATs()->size() * 3, gebId);
191  optoH->setChamberTrailer(LV1_id, BX_id, optoH->vFATs()->size() * 3);
192  amc->addGEB(*optoH);
193  }
194  } // end of GEB loop
195 
196  if (!amc->gebs()->empty()) {
197  amcSize += 5;
198  amc->setAMCheader1(amcSize, BX_id, LV1_id, amcNum);
199  amc->setAMCheader2(amcNum, OrN, 1);
200  amc->setGEMeventHeader(amc->gebs()->size(), 0);
201  amc13->addAMCpayload(*amc);
202  // AMC header in GEMAMC13
203  amc13->addAMCheader(amcSize, 0, amcNum, 0);
204  amc13EvtLength += amcSize + 1; // AMC data size + AMC header size
205  }
206  } // end of AMC loop
207 
208  if (!amc13->getAMCpayloads()->empty()) {
209  // CDFHeader
210  amc13->setCDFHeader(event_type_, LV1_id, BX_id, fedId);
211  // AMC13header
212  uint8_t nAMC = amc13->getAMCpayloads()->size();
213  amc13->setAMC13Header(1, nAMC, OrN);
214  amc13->setAMC13Trailer(BX_id, LV1_id, BX_id);
215  //CDF trailer
216  uint32_t EvtLength = amc13EvtLength + 4; // 2 header and 2 trailer
217  amc13->setCDFTrailer(EvtLength);
218  amc13s.emplace_back(std::move(amc13));
219  } // finished making amc13 data
220  } // end of FED loop
221 
222  // read out amc13s into fedRawData
223  for (const auto& amc13e : amc13s) {
224  std::vector<uint64_t> words;
225  words.emplace_back(amc13e->getCDFHeader());
226  words.emplace_back(amc13e->getAMC13Header());
227 
228  for (const auto& w : *amc13e->getAMCheaders())
229  words.emplace_back(w);
230 
231  for (const auto& amc : *amc13e->getAMCpayloads()) {
232  words.emplace_back(amc.getAMCheader1());
233  words.emplace_back(amc.getAMCheader2());
234  words.emplace_back(amc.getGEMeventHeader());
235 
236  for (const auto& geb : *amc.gebs()) {
237  words.emplace_back(geb.getChamberHeader());
238 
239  for (const auto& vfat : *geb.vFATs()) {
240  words.emplace_back(vfat.get_fw());
241  words.emplace_back(vfat.get_sw());
242  words.emplace_back(vfat.get_tw());
243  }
244 
245  words.emplace_back(geb.getChamberTrailer());
246  }
247 
248  words.emplace_back(amc.getGEMeventTrailer());
249  words.emplace_back(amc.getAMCTrailer());
250  }
251 
252  words.emplace_back(amc13e->getAMC13Trailer());
253  words.emplace_back(amc13e->getCDFTrailer());
254 
255  FEDRawData& fedRawData = fedRawDataCol->FEDData(amc13e->sourceId());
256 
257  int dataSize = (words.size()) * sizeof(uint64_t);
258  fedRawData.resize(dataSize);
259 
260  uint64_t* w = reinterpret_cast<uint64_t*>(fedRawData.data());
261  for (const auto& word : words) {
262  *(w++) = word;
263  }
264  LogDebug("GEMDigiToRawModule") << "fedId:" << amc13e->sourceId() << " words:" << words.size();
265  }
266 
267  iEvent.put(std::move(fedRawDataCol));
268 }
edm::ESGetToken< GEMeMap, GEMeMapRcd > gemEMapToken_
EventNumber_t event() const
Definition: EventID.h:40
edm::EDGetTokenT< GEMDigiCollection > digi_token
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
const double w
Definition: UKUtility.cc:23
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:21
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int16_t bx() const
Definition: GEMDigi.h:27
int bunchCrossing() const
Definition: EventBase.h:64
Run const & getRun() const
Definition: Event.cc:112
static const int maxChan_
Definition: GEMeMap.h:72
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const uint16_t range(const Frame &aFrame)
bool getData(T &iHolder) const
Definition: EventSetup.h:128
uint64_t word
int iEvent
Definition: GenABIO.cc:224
double amc
Definition: hdecay.h:20
uint16_t strip() const
Definition: GEMDigi.h:26
void resize(size_t newsize)
Definition: FEDRawData.cc:28
def move
Definition: eostools.py:511
RunIndex index() const
Definition: Run.cc:26
int orbitNumber() const
Definition: EventBase.h:65
GEMDigiToRawModule(const edm::ParameterSet &pset)
Constructor.
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static const int maxAMCs_
Definition: GEMeMap.h:65
static const int maxGEBs_
Definition: GEMeMap.h:62
unsigned long long uint64_t
Definition: Time.h:13
std::pair< const_iterator, const_iterator > Range
std::vector< DigiType >::const_iterator const_iterator
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override
edm::EventID id() const
Definition: EventBase.h:59
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
std::shared_ptr< GEMROMapping > globalBeginRun(edm::Run const &, edm::EventSetup const &) const override
Definition: Run.h:45
#define LogDebug(id)