CMS 3D CMS Logo

GEMDigiToRawModule.cc
Go to the documentation of this file.
1 
27 
28 class GEMDigiToRawModule : public edm::global::EDProducer<edm::RunCache<GEMChMap>> {
29 public:
32 
33  // global::EDProducer
34  std::shared_ptr<GEMChMap> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
35  void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
36  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}
37 
38  // Fill parameters descriptions
39  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
40 
41 private:
42  const int event_type_;
43  const int minBunch_;
44  const int maxBunch_;
47  const bool useDBEMap_;
49 };
50 
53 
55  : event_type_(pset.getParameter<int>("eventType")),
56  minBunch_(pset.getParameter<int>("minBunch")),
57  maxBunch_(pset.getParameter<int>("maxBunch")),
58  digiToken_(consumes<GEMDigiCollection>(pset.getParameter<edm::InputTag>("gemDigi"))),
59  useDBEMap_(pset.getParameter<bool>("useDBEMap")),
60  simulatePulseStretching_(pset.getParameter<bool>("simulatePulseStretching")) {
61  produces<FEDRawDataCollection>();
62  if (useDBEMap_) {
63  gemChMapToken_ = esConsumes<GEMChMap, GEMChMapRcd, edm::Transition::BeginRun>();
64  }
65 }
66 
69  desc.add<edm::InputTag>("gemDigi", edm::InputTag("simMuonGEMDigis"));
70  desc.add<int>("eventType", 0);
71 
72  // time window for pulse stretching simulation
73  desc.add<int>("minBunch", -3);
74  desc.add<int>("maxBunch", 4);
75 
76  desc.add<bool>("useDBEMap", false);
77  desc.add<bool>("simulatePulseStretching", false);
78  descriptions.add("gemPackerDefault", desc);
79 }
80 
81 std::shared_ptr<GEMChMap> GEMDigiToRawModule::globalBeginRun(edm::Run const&, edm::EventSetup const& iSetup) const {
82  if (useDBEMap_) {
83  const auto& eMap = iSetup.getData(gemChMapToken_);
84  auto gemChMap = std::make_shared<GEMChMap>(eMap);
85  return gemChMap;
86  } else {
87  // no EMap in DB, using dummy
88  auto gemChMap = std::make_shared<GEMChMap>();
89  gemChMap->setDummy();
90  return gemChMap;
91  }
92 }
93 
95  auto fedRawDataCol = std::make_unique<FEDRawDataCollection>();
96 
98  iEvent.getByToken(digiToken_, gemDigis);
99  if (!gemDigis.isValid()) {
100  iEvent.put(std::move(fedRawDataCol));
101  return;
102  }
103 
104  auto gemChMap = runCache(iEvent.getRun().index());
105 
106  std::vector<std::unique_ptr<GEMAMC13>> amc13s;
108 
109  int LV1_id = iEvent.id().event();
110  uint8_t BX_id(iEvent.bunchCrossing());
111  int OrN = iEvent.orbitNumber();
112 
113  // making map of bx GEMDigiCollection
114  // each bx will be saved as new GEMAMC13, so GEMDigiCollection needs to be split into bx
115  std::map<int, GEMDigiCollection> gemBxMap;
116  for (auto const& etaPart : *gemDigis) {
117  GEMDetId gemId = etaPart.first;
118  const GEMDigiCollection::Range& digis = etaPart.second;
119  for (auto digi = digis.first; digi != digis.second; ++digi) {
120  int bx = digi->bx();
122  if (bx < minBunch_ or bx > maxBunch_)
123  continue;
124  else
125  bx = 0;
126  }
127  auto search = gemBxMap.find(bx);
128  if (search != gemBxMap.end()) {
129  search->second.insertDigi(gemId, *digi);
130  } else {
131  GEMDigiCollection newGDC;
132  newGDC.insertDigi(gemId, *digi);
133  gemBxMap.insert(std::pair<int, GEMDigiCollection>(bx, newGDC));
134  }
135  }
136  }
137 
139  uint32_t amc13EvtLength = 0;
140  std::unique_ptr<GEMAMC13> amc13 = std::make_unique<GEMAMC13>();
141 
142  for (uint8_t amcNum = 0; amcNum <= GEMChMap::maxAMCs_; ++amcNum) {
143  uint32_t amcSize = 0;
144  std::unique_ptr<GEMAMC> amc = std::make_unique<GEMAMC>();
145 
146  for (uint8_t gebId = 0; gebId <= GEMChMap::maxGEBs_; ++gebId) {
147  std::unique_ptr<GEMOptoHybrid> optoH = std::make_unique<GEMOptoHybrid>();
148 
149  if (!gemChMap->isValidChamber(fedId, amcNum, gebId))
150  continue;
151 
152  auto geb_dc = gemChMap->chamberPos(fedId, amcNum, gebId);
153  GEMDetId cid = geb_dc.detId;
154  int chamberType = geb_dc.chamberType;
155 
156  auto vfats = gemChMap->getVfats(chamberType);
157 
158  for (auto vfatId : vfats) {
159  auto iEtas = gemChMap->getIEtas(chamberType, vfatId);
160  for (auto iEta : iEtas) {
161  GEMDetId gemId(cid.region(), cid.ring(), cid.station(), cid.layer(), cid.chamber(), iEta);
162 
163  for (auto const& gemBx : gemBxMap) {
164  int bc = BX_id + gemBx.first;
165 
166  bool hasDigi = false;
167  uint64_t lsData = 0;
168  uint64_t msData = 0;
169 
170  GEMDigiCollection inBxGemDigis = gemBx.second;
171  const GEMDigiCollection::Range& range = inBxGemDigis.get(gemId);
172 
173  for (GEMDigiCollection::const_iterator digiIt = range.first; digiIt != range.second; ++digiIt) {
174  const GEMDigi& digi = (*digiIt);
175 
176  int strip = digi.strip();
177 
178  hasDigi = true;
179 
180  if (!gemChMap->isValidStrip(chamberType, iEta, strip))
181  continue;
182  auto chMap = gemChMap->getChannel(chamberType, iEta, strip);
183 
184  if (chMap.vfatAdd != vfatId)
185  continue;
186 
187  if (chMap.chNum < 64)
188  lsData |= 1UL << chMap.chNum;
189  else
190  msData |= 1UL << (chMap.chNum - 64);
191 
192  LogDebug("GEMDigiToRawModule")
193  << "fed: " << fedId << " amc:" << int(amcNum) << " geb:" << int(gebId) << " vfat id:" << int(vfatId)
194  << ",type:" << chamberType << " id:" << gemId << " ch:" << chMap.chNum << " st:" << digi.strip()
195  << " bx:" << digi.bx();
196  }
197 
198  if (!hasDigi)
199  continue;
200  // only make vfat with hits
201  amcSize += 3;
202  int vfatVersion = (chamberType < 10) ? 2 : 3;
203  auto vfat = std::make_unique<GEMVFAT>(vfatVersion, bc, LV1_id, vfatId, lsData, msData);
204  optoH->addVFAT(*vfat);
205  }
206  }
207  } // end of vfats in GEB
208 
209  if (!optoH->vFATs()->empty()) {
210  amcSize += 2;
211  optoH->setChamberHeader(optoH->vFATs()->size() * 3, gebId);
212  optoH->setChamberTrailer(LV1_id, BX_id, optoH->vFATs()->size() * 3);
213  amc->addGEB(*optoH);
214  }
215  } // end of GEB loop
216 
217  if (!amc->gebs()->empty()) {
218  amcSize += 5;
219  amc->setAMCheader1(amcSize, BX_id, LV1_id, amcNum);
220  amc->setAMCheader2(amcNum, OrN, 1);
221  amc->setGEMeventHeader(amc->gebs()->size(), 0);
222  amc13->addAMCpayload(*amc);
223  // AMC header in GEMAMC13
224  amc13->addAMCheader(amcSize, 0, amcNum, 0);
225  amc13EvtLength += amcSize + 1; // AMC data size + AMC header size
226  }
227  } // end of AMC loop
228 
229  if (!amc13->getAMCpayloads()->empty()) {
230  // CDFHeader
231  amc13->setCDFHeader(event_type_, LV1_id, BX_id, fedId);
232  // AMC13header
233  uint8_t nAMC = amc13->getAMCpayloads()->size();
234  amc13->setAMC13Header(1, nAMC, OrN);
235  amc13->setAMC13Trailer(BX_id, LV1_id, BX_id);
236  //CDF trailer
237  uint32_t EvtLength = amc13EvtLength + 4; // 2 header and 2 trailer
238  amc13->setCDFTrailer(EvtLength);
239  amc13s.emplace_back(std::move(amc13));
240  } // finished making amc13 data
241  } // end of FED loop
242 
243  // read out amc13s into fedRawData
244  for (const auto& amc13e : amc13s) {
245  std::vector<uint64_t> words;
246  words.emplace_back(amc13e->getCDFHeader());
247  words.emplace_back(amc13e->getAMC13Header());
248 
249  for (const auto& w : *amc13e->getAMCheaders())
250  words.emplace_back(w);
251 
252  for (const auto& amc : *amc13e->getAMCpayloads()) {
253  words.emplace_back(amc.getAMCheader1());
254  words.emplace_back(amc.getAMCheader2());
255  words.emplace_back(amc.getGEMeventHeader());
256 
257  for (const auto& geb : *amc.gebs()) {
258  words.emplace_back(geb.getChamberHeader());
259 
260  for (const auto& vfat : *geb.vFATs()) {
261  words.emplace_back(vfat.get_fw());
262  words.emplace_back(vfat.get_sw());
263  words.emplace_back(vfat.get_tw());
264  }
265 
266  words.emplace_back(geb.getChamberTrailer());
267  }
268 
269  words.emplace_back(amc.getGEMeventTrailer());
270  words.emplace_back(amc.getAMCTrailer());
271  }
272 
273  words.emplace_back(amc13e->getAMC13Trailer());
274  words.emplace_back(amc13e->getCDFTrailer());
275 
276  FEDRawData& fedRawData = fedRawDataCol->FEDData(amc13e->sourceId());
277 
278  int dataSize = (words.size()) * sizeof(uint64_t);
279  fedRawData.resize(dataSize);
280 
281  uint64_t* w = reinterpret_cast<uint64_t*>(fedRawData.data());
282  for (const auto& word : words) {
283  *(w++) = word;
284  }
285  LogDebug("GEMDigiToRawModule") << "fedId:" << amc13e->sourceId() << " words:" << words.size();
286  }
287 
288  iEvent.put(std::move(fedRawDataCol));
289 }
constexpr int station() const
Definition: GEMDetId.h:179
constexpr int region() const
Definition: GEMDetId.h:171
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
T w() const
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:22
edm::ESGetToken< GEMChMap, GEMChMapRcd > gemChMapToken_
constexpr int layer() const
Definition: GEMDetId.h:190
constexpr int chamber() const
Definition: GEMDetId.h:183
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::shared_ptr< GEMChMap > globalBeginRun(edm::Run const &, edm::EventSetup const &) const override
uint64_t word
int iEvent
Definition: GenABIO.cc:224
constexpr int ring() const
Definition: GEMDetId.h:176
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GEMDigiToRawModule(const edm::ParameterSet &pset)
Constructor.
static const int maxGEBs_
Definition: GEMChMap.h:189
const bool simulatePulseStretching_
const edm::EDGetTokenT< GEMDigiCollection > digiToken_
uint16_t strip() const
Definition: GEMDigi.h:26
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
HLT enums.
int16_t bx() const
Definition: GEMDigi.h:27
static const int maxAMCs_
Definition: GEMChMap.h:192
Definition: AMCSpec.h:8
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
#define LogDebug(id)