CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GctDigiToRaw.cc
Go to the documentation of this file.
2 
3 // system
4 #include <vector>
5 #include <sstream>
6 #include <iostream>
7 #include <iomanip>
8 
9 // framework
13 
14 // Raw data collection
19 
20 // Header needed to computer CRCs
22 
23 // GCT input data formats
28 
29 // GCT output data formats
31 
32 // Raw data collection
34 
35 using std::cout;
36 using std::endl;
37 using std::vector;
38 
39 
41  rctInputLabel_(iConfig.getParameter<edm::InputTag>("rctInputLabel")),
42  gctInputLabel_(iConfig.getParameter<edm::InputTag>("gctInputLabel")),
43  packRctEm_(iConfig.getUntrackedParameter<bool>("packRctEm", true)),
44  packRctCalo_(iConfig.getUntrackedParameter<bool>("packRctCalo", true)),
45  fedId_(iConfig.getParameter<int>("gctFedId")),
46  verbose_(iConfig.getUntrackedParameter<bool>("verbose",false)),
47  counter_(0),
48  formatTranslator_()
49 {
50  LogDebug("GCT") << "GctDigiToRaw will pack FED Id " << fedId_;
51 
52  //register the products
53  produces<FEDRawDataCollection>();
54 }
55 
56 
58 {
59  // do anything here that needs to be done at destruction time
60  // (e.g. close files, deallocate resources etc.)
61 }
62 
63 
64 //
65 // member functions
66 //
67 
68 // ------------ method called to produce the data ------------
69 void
71 {
72  using namespace edm;
73 
74  counter_++; // To "simulate" bunch crossings for now...
75  unsigned int bx = counter_ % 3564; // What's the proper way of doing this?
76  EventNumber_t eventNumber = iEvent.id().event();
77 
78  // Supply bx and EvID to the packer so it can make internal capture block headers.
81 
82  // The GCT and RCT input label strings
83  const std::string gctInputLabelStr = gctInputLabel_.label();
84  const std::string rctInputLabelStr = rctInputLabel_.label();
85 
86  // get GCT digis
88  iEvent.getByLabel(gctInputLabelStr, "isoEm", isoEm);
90  iEvent.getByLabel(gctInputLabelStr, "nonIsoEm", nonIsoEm);
92  iEvent.getByLabel(gctInputLabelStr, "cenJets", cenJets);
94  iEvent.getByLabel(gctInputLabelStr, "forJets", forJets);
96  iEvent.getByLabel(gctInputLabelStr, "tauJets", tauJets);
98  iEvent.getByLabel(gctInputLabelStr, "", etTotal);
100  iEvent.getByLabel(gctInputLabelStr, "", etHad);
102  iEvent.getByLabel(gctInputLabelStr, "", etMiss);
104  iEvent.getByLabel(gctInputLabelStr, "", hfRingSums);
106  iEvent.getByLabel(gctInputLabelStr, "", hfBitCounts);
108  iEvent.getByLabel(gctInputLabelStr, "", htMiss);
110  iEvent.getByLabel(gctInputLabelStr, "", jetCounts);
111 
112  // get RCT EM Cand digi
113  bool packRctEmThisEvent = packRctEm_;
115  if(packRctEmThisEvent)
116  {
117  iEvent.getByLabel(rctInputLabelStr, rctEm);
118  if(rctEm.failedToGet())
119  {
120  packRctEmThisEvent = false;
121  LogDebug("GCT") << "RCT EM Candidate packing requested, but failed to get them from event!";
122  }
123  }
124 
125  // get RCT Calo region digi
126  bool packRctCaloThisEvent = packRctCalo_;
128  if(packRctCaloThisEvent)
129  {
130  iEvent.getByLabel(rctInputLabelStr, rctCalo);
131  if(rctCalo.failedToGet())
132  {
133  packRctCaloThisEvent = false;
134  LogDebug("GCT") << "RCT Calo Region packing requested, but failed to get them from event!";
135  }
136  }
137 
138  // create the raw data collection
139  std::auto_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
140 
141  // get the GCT buffer
142  FEDRawData& fedRawData=rawColl->FEDData(fedId_);
143 
144  // set the size & make pointers to the header, beginning of payload, and footer.
145  unsigned int rawSize = 88; // MUST BE MULTIPLE OF 8! (slink packets are 64 bit, but using 8-bit data struct).
146  if(packRctEmThisEvent) { rawSize += 232; } // Space for RCT EM Cands.
147  if(packRctCaloThisEvent) { rawSize += 800; } // Space for RCT Calo Regions (plus a 32-bit word of padding to make divisible by 8)
148  fedRawData.resize(rawSize);
149  unsigned char * pHeader = fedRawData.data();
150  unsigned char * pPayload = pHeader + 16; // 16 = 8 for slink header + 8 for Greg's versioning header.
151  unsigned char * pFooter = pHeader + rawSize - 8;
152 
153  // Write CDF header (exactly as told by Marco Zanetti)
154  FEDHeader fedHeader(pHeader);
155  fedHeader.set(pHeader, 1, eventNumber, bx, fedId_); // what should the bx_ID be?
156 
157  // Pack GCT jet output digis
159  cenJets.product(),
160  forJets.product(),
161  tauJets.product(),
162  hfRingSums.product(),
163  hfBitCounts.product(),
164  htMiss.product());
165 
166  pPayload += 36; //advance payload pointer
167 
168  // Pack GCT EM and energy sums digis.
170  isoEm.product(),
171  nonIsoEm.product(),
172  etTotal.product(),
173  etHad.product(),
174  etMiss.product());
175 
176  pPayload += 28; //advance payload pointer
177 
178  // Pack RCT EM Cands
179  if(packRctEmThisEvent)
180  {
181  formatTranslator_.writeRctEmCandBlocks(pPayload, rctEm.product());
182  pPayload+=232; //advance payload pointer
183  }
184 
185  // Pack RCT Calo Regions
186  if(packRctCaloThisEvent)
187  {
188  formatTranslator_.writeAllRctCaloRegionBlock(pPayload, rctCalo.product());
189  }
190 
191  // Write CDF footer (exactly as told by Marco Zanetti)
192  FEDTrailer fedTrailer(pFooter);
193  fedTrailer.set(pFooter, rawSize/8, evf::compute_crc(pHeader, rawSize), 0, 0);
194 
195  // Debug output.
196  if (verbose_) { print(fedRawData); }
197 
198  // Put the collection in the event.
199  iEvent.put(rawColl);
200 }
201 
202 
204 
205  const unsigned char * d = data.data();
206 
207  for (unsigned int i=0; i<data.size(); i=i+4) {
208  uint32_t w = (uint32_t)d[i] + (uint32_t)(d[i+1]<<8) + (uint32_t)(d[i+2]<<16) + (uint32_t)(d[i+3]<<24);
209  cout << std::hex << std::setw(4) << i/4 << " " << std::setw(8) << w << endl;
210  }
211 
212 }
213 
214 
215 // ------------ method called once each job just before starting event loop ------------
216 void
218 {
219 }
220 
221 // ------------ method called once each job just after ending the event loop ------------
222 void
224 }
225 
228 
#define LogDebug(id)
void setPackingEventId(uint32_t eventId)
void writeGctOutEmAndEnergyBlock(unsigned char *d, const L1GctEmCandCollection *iso, const L1GctEmCandCollection *nonIso, const L1GctEtTotalCollection *etTotal, const L1GctEtHadCollection *etHad, const L1GctEtMissCollection *etMiss)
Writes GCT output EM and energy sums block into an unsigned char array, starting at the position poin...
EventNumber_t event() const
Definition: EventID.h:41
int i
Definition: DBlmapReader.cc:9
void writeAllRctCaloRegionBlock(unsigned char *d, const L1CaloRegionCollection *rctCalo)
Writes the giant hack that is the RCT Calo Regions block.
const double w
Definition: UKUtility.cc:23
bool packRctCalo_
Definition: GctDigiToRaw.h:61
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void endJob()
void writeRctEmCandBlocks(unsigned char *d, const L1CaloEmCollection *rctEm)
Writes the 4 RCT EM Candidate blocks.
unsigned long long EventNumber_t
static void set(unsigned char *trailer, int evt_lgth, int crc, int evt_stat, int tts, bool T=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:42
virtual void produce(edm::Event &, const edm::EventSetup &)
Definition: GctDigiToRaw.cc:70
void writeGctOutJetBlock(unsigned char *d, const L1GctJetCandCollection *cenJets, const L1GctJetCandCollection *forJets, const L1GctJetCandCollection *tauJets, const L1GctHFRingEtSumsCollection *hfRingSums, const L1GctHFBitCountsCollection *hfBitCounts, const L1GctHtMissCollection *htMiss)
Writes GCT output jet cands and counts into an unsigned char array, starting at the position pointed ...
GctDigiToRaw(const edm::ParameterSet &)
Definition: GctDigiToRaw.cc:40
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
tuple d
Definition: ztail.py:151
edm::InputTag rctInputLabel_
Definition: GctDigiToRaw.h:56
void print(FEDRawData &data)
int iEvent
Definition: GenABIO.cc:230
void resize(size_t newsize)
Definition: FEDRawData.cc:32
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
static void set(unsigned char *header, int evt_ty, int lvl1_ID, int bx_ID, int source_ID, int version=0, bool H=false)
Set all fields in the header.
Definition: FEDHeader.cc:40
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
edm::InputTag gctInputLabel_
Definition: GctDigiToRaw.h:57
virtual void beginJob()
void setPackingBxId(uint32_t bxId)
std::string const & label() const
Definition: InputTag.h:42
edm::EventID id() const
Definition: EventBase.h:60
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
GctFormatTranslateMCLegacy formatTranslator_
Definition: GctDigiToRaw.h:73
tuple cout
Definition: gather_cfg.py:121
volatile std::atomic< bool > shutdown_flag false