CMS 3D CMS Logo

GEMDigiToRawModule.cc
Go to the documentation of this file.
1 
11 
18 
20 
21 using namespace gem;
22 
24  event_type_(pset.getParameter<int>("eventType")),
25  digi_token(consumes<GEMDigiCollection>( pset.getParameter<edm::InputTag>("gemDigi") )),
26  useDBEMap_(pset.getParameter<bool>("useDBEMap"))
27 {
28  produces<FEDRawDataCollection>();
29 }
30 
32 {
34  desc.add<edm::InputTag>("gemDigi", edm::InputTag("simMuonGEMDigis"));
35  desc.add<int>("eventType", 0);
36  desc.add<bool>("useDBEMap", false);
37  descriptions.add("gemPackerDefault", desc);
38 }
39 
40 std::shared_ptr<GEMROmap> GEMDigiToRawModule::globalBeginRun(edm::Run const&, edm::EventSetup const& iSetup) const
41 {
42  auto gemORmap = std::make_shared<GEMROmap>();
43  if (useDBEMap_){
44  edm::ESHandle<GEMELMap> gemEMapRcd;
45  iSetup.get<GEMELMapRcd>().get(gemEMapRcd);
46  auto gemEMap = std::make_unique<GEMELMap>(*(gemEMapRcd.product()));
47  gemEMap->convert(*gemORmap);
48  gemEMap.reset();
49  }
50  else {
51  // no EMap in DB, using dummy
52  auto gemEMap = std::make_unique<GEMELMap>();
53  gemEMap->convertDummy(*gemORmap);
54  gemEMap.reset();
55  }
56  return gemORmap;
57 }
58 
60 {
61  auto fedRawDataCol = std::make_unique<FEDRawDataCollection>();
62 
63  // Take digis from the event
65  iEvent.getByToken( digi_token, gemDigis );
66 
67  auto gemROMap = runCache(iEvent.getRun().index());
68 
69  std::vector<std::unique_ptr<AMC13Event>> amc13Events;
70  // currently only one FEDRaw
71  amc13Events.reserve(1);
72  {
73  auto amc13Event = std::make_unique<AMC13Event>();
74 
75  uint16_t amcId = 0, gebId = 0;
76  std::unique_ptr<AMCdata> amcData;
77  std::unique_ptr<GEBdata> gebData;
78 
79  const std::map<GEMROmap::eCoord,GEMROmap::dCoord> *roMapED = gemROMap->getRoMap();
80  for (auto ro=roMapED->begin(); ro!=roMapED->end(); ++ro){
81  GEMROmap::eCoord ec = ro->first;
82  GEMROmap::dCoord dc = ro->second;
83 
84  if (amcId != ec.amcId || !amcData){
85  amcId = ec.amcId;
86  amcData = std::make_unique<AMCdata>();
87  amcData->setBID(amcId);
88  amcData->setBX(GEMELMap::amcBX_);
89  }
90 
91  if (gebId != ec.gebId || !gebData){
92  gebId = ec.gebId;
93  gebData = std::make_unique<GEBdata>();
94  gebData->setInputID(gebId);
95  }
96 
97  uint16_t vfatId = ec.vfatId;
98  GEMDetId gemId = dc.gemDetId;
99 
100  for (uint16_t bc = 0; bc < 2*GEMELMap::amcBX_; ++bc){
101  bool hasDigi = false;
102 
103  uint8_t b1010 =0xA;
104  uint16_t BC =bc;
105  uint8_t b1100 =0xC;
106  uint8_t EC =0;
107  uint8_t Flag =0;
108  uint8_t b1110 =0xE;
109  int SlotNumber =0;
110  bool isBlockGood=false;
111  uint64_t lsData =0;
112  uint64_t msData =0;
113  uint16_t crc =0;
114  uint16_t crc_calc =0;
115 
116  GEMDigiCollection::Range range = gemDigis->get(gemId);
117  for (GEMDigiCollection::const_iterator digiIt = range.first; digiIt!=range.second; ++digiIt){
118 
119  const GEMDigi & digi = (*digiIt);
120  if (digi.bx() != bc-GEMELMap::amcBX_) continue;
121 
122  int maxVFat = GEMELMap::maxVFatGE11_;
123  if (gemId.station() == 2) maxVFat = GEMELMap::maxVFatGE21_;
124 
125  int localStrip = digi.strip() - ((dc.iPhi-1)%maxVFat)*GEMELMap::maxChan_;
126  // skip strips not in current vFat
127  if (localStrip < 1 || localStrip > GEMELMap::maxChan_) continue;
128 
129  hasDigi = true;
130 
131  GEMROmap::stripNum stMap = {dc.vfatType, localStrip};
132  GEMROmap::channelNum chMap = gemROMap->hitPosition(stMap);
133 
134  int chan = chMap.chNum;
135  uint64_t oneBit = 0x1;
136  if (chan < 64) lsData = lsData | (oneBit << chan);
137  else msData = msData | (oneBit << (chan-64));
138 
139  LogDebug("GEMDigiToRawModule") <<" vfatId "<<ec.vfatId
140  <<" gemDetId "<< gemId
141  <<" chan "<< chMap.chNum
142  <<" strip "<< stMap.stNum
143  <<" bx "<< digi.bx();
144 
145  }
146 
147  if (!hasDigi) continue;
148  // only make vfat with hits
149  auto vfatData = std::make_unique<VFATdata>(b1010, BC, b1100, EC, Flag, b1110, vfatId, lsData, msData,
150  crc, crc_calc, SlotNumber, isBlockGood);
151  gebData->addVFAT(*vfatData);
152  }
153 
154  bool saveGeb = false;
155  bool saveAMC = false;
156  auto nx = std::next(ro);
157  // last vfat, save
158  if (nx == roMapED->end()){
159  saveGeb = true;
160  saveAMC = true;
161  }
162  else {
163  // check if next vfat is in new geb or amc
164  GEMROmap::eCoord ecNext = nx->first;
165  if (ecNext.gebId != gebId) saveGeb = true;
166  if (ecNext.amcId != amcId) saveAMC = true;
167  }
168 
169  if (!gebData->vFATs()->empty() && saveGeb){
170  gebData->setVwh(gebData->vFATs()->size()*3);
171  amcData->addGEB(*gebData);
172  }
173  if (!amcData->gebs()->empty() && saveAMC){
174  amcData->setGDcount(amcData->gebs()->size());
175  amc13Event->addAMCpayload(*amcData);
176  }
177  }
178 
179  // CDFHeader
180  uint8_t cb5 = 0x5;// control bit, should be 0x5 bits 60-63
181  uint8_t Evt_ty = event_type_;
182  uint32_t LV1_id = iEvent.id().event();
183  uint16_t BX_id = iEvent.bunchCrossing();
184  uint16_t Source_id = FEDNumbering::MINGEMFEDID;
185  amc13Event->setCDFHeader(cb5, Evt_ty, LV1_id, BX_id, Source_id);
186 
187  // AMC13header
188  uint8_t CalTyp = 1;
189  uint8_t nAMC = amc13Event->getAMCpayloads()->size(); // currently only one AMC13Event
190  uint32_t OrN = 2;
191  uint8_t cb0 = 0b0000;// control bit, should be 0b0000
192  amc13Event->setAMC13header(CalTyp, nAMC, OrN, cb0);
193 
194  for (unsigned short i = 0; i < amc13Event->nAMC(); ++i){
195  uint32_t AMC_size = 0;
196  uint8_t Blk_No = 0;
197  uint8_t AMC_No = 0;
198  uint16_t BoardID = 0;
199  amc13Event->addAMCheader(AMC_size, Blk_No, AMC_No, BoardID);
200  }
201 
202  //AMC13 trailer
203  uint32_t CRC_amc13 = 0;
204  uint8_t Blk_NoT = 0;
205  uint8_t LV1_idT = 0;
206  uint16_t BX_idT = BX_id;
207  amc13Event->setAMC13trailer(CRC_amc13, Blk_NoT, LV1_idT, BX_idT);
208  //CDF trailer
209  uint8_t cbA = 0xA; // control bit, should be 0xA bits 60-63
210  uint32_t EvtLength = 0;
211  uint16_t CRC_cdf = 0;
212  amc13Event->setCDFTrailer(cbA, EvtLength, CRC_cdf);
213  amc13Events.emplace_back(std::move(amc13Event));
214  }// finished making amc13Event data
215 
216  // read out amc13Events into fedRawData
217  for (const auto & amc13e : amc13Events){
218  std::vector<uint64_t> words;
219  words.emplace_back(amc13e->getCDFHeader());
220  words.emplace_back(amc13e->getAMC13header());
221 
222  for (const auto & w: *amc13e->getAMCheaders())
223  words.emplace_back(w);
224 
225  for (const auto & amc : *amc13e->getAMCpayloads()){
226  words.emplace_back(amc.getAMCheader1());
227  words.emplace_back(amc.getAMCheader2());
228  words.emplace_back(amc.getGEMeventHeader());
229 
230  for (const auto & geb: *amc.gebs()){
231  words.emplace_back(geb.getChamberHeader());
232 
233  for (const auto & vfat: *geb.vFATs()){
234  words.emplace_back(vfat.get_fw());
235  words.emplace_back(vfat.get_sw());
236  words.emplace_back(vfat.get_tw());
237  }
238 
239  words.emplace_back(geb.getChamberTrailer());
240  }
241 
242  words.emplace_back(amc.getGEMeventTrailer());
243  words.emplace_back(amc.getAMCTrailer());
244  }
245 
246  words.emplace_back(amc13e->getAMC13trailer());
247  words.emplace_back(amc13e->getCDFTrailer());
248 
249  FEDRawData & fedRawData = fedRawDataCol->FEDData(amc13e->source_id());
250 
251  int dataSize = (words.size()) * sizeof(uint64_t);
252  fedRawData.resize(dataSize);
253 
254  uint64_t * w = reinterpret_cast<uint64_t* >(fedRawData.data());
255  for (const auto & word: words) *(w++) = word;
256 
257  LogDebug("GEMDigiToRawModule") <<" words " << words.size();
258  }
259 
260  iEvent.put(std::move(fedRawDataCol));
261 }
#define LogDebug(id)
EventNumber_t event() const
Definition: EventID.h:41
edm::EDGetTokenT< GEMDigiCollection > digi_token
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
const double w
Definition: UKUtility.cc:23
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
int bunchCrossing() const
Definition: EventBase.h:66
Run const & getRun() const
Definition: Event.cc:114
static const int maxVFatGE21_
Definition: GEMELMap.h:60
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
uint16_t gebId
Definition: GEMROmap.h:11
A class for AMC data.
Definition: AMC13Event.h:6
static const int maxChan_
Definition: GEMELMap.h:61
int iEvent
Definition: GenABIO.cc:230
void resize(size_t newsize)
Definition: FEDRawData.cc:32
GEMDetId gemDetId
Definition: GEMROmap.h:30
int station() const
Station id : the station is the pair of chambers at same disk.
Definition: GEMDetId.h:64
RunIndex index() const
Definition: Run.cc:24
static const int amcBX_
Definition: GEMELMap.h:62
GEMDigiToRawModule(const edm::ParameterSet &pset)
Constructor.
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::shared_ptr< GEMROmap > globalBeginRun(edm::Run const &, edm::EventSetup const &) const override
int bx() const
Definition: GEMDigi.h:27
unsigned long long uint64_t
Definition: Time.h:15
static const int maxVFatGE11_
Definition: GEMELMap.h:59
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override
uint16_t vfatId
Definition: GEMROmap.h:12
const T & get() const
Definition: EventSetup.h:59
std::vector< GEMDigi >::const_iterator const_iterator
void add(std::string const &label, ParameterSetDescription const &psetDescription)
chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, "NDC") lumi.SetBorderSize( 0 ) lumi...
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
uint16_t amcId
Definition: GEMROmap.h:10
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
std::pair< const_iterator, const_iterator > Range
int strip() const
Definition: GEMDigi.h:26
Definition: AMCSpec.h:8
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510
Definition: Run.h:43