CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AMCDumpToRaw.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EventFilter/L1TRawToDigi
4 // Class: AMCDumpToRaw
5 //
13 //
14 // Original Author: James Brooke
15 // Created: Tue, 11 Mar 2014 14:55:45 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
31 
36 
38 
39 #include <fstream>
40 #include <iostream>
41 #include <sstream>
42 #include <string>
43 #include <iomanip>
44 #include <boost/algorithm/string.hpp>
45 
47 
48 
49 namespace l1t {
50 
51 class AMCDumpToRaw : public edm::EDProducer {
52 public:
53  explicit AMCDumpToRaw(const edm::ParameterSet&);
54  ~AMCDumpToRaw();
55 
56  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
57 
58 
59 private:
60  virtual void beginJob() override;
61  virtual void produce(edm::Event&, const edm::EventSetup&) override;
62  virtual void endJob() override;
63 
64  void readEvent(std::vector<uint32_t>& load32);
65 
66  void formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32);
67 
68  void formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data);
69 
70  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
71  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
72  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
73  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
74 
75  // ----------member data ---------------------------
76  std::ifstream file_;
78 
79  // DAQ params
80  int fedId_;
81  int iAmc_;
82  int boardId_;
83  int evType_;
84  int fwVer_;
85  int slinkHeaderSize_; // in 8-bit words
87 
88 };
89 
90 //
91 // constants, enums and typedefs
92 //
93 
94 //
95 // static data member definitions
96 //
97 
98 //
99 // constructors and destructor
100 //
102  filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
103  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
104  iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
105  boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
106  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
107  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
108  slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
109  slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8))
110 {
111 
112  produces<FEDRawDataCollection>();
113 
114 }
115 
116 
118 {
119 
120  // do anything here that needs to be done at desctruction time
121  // (e.g. close files, deallocate resources etc.)
122 
123 }
124 
125 
126 //
127 // member functions
128 //
129 
130 // ------------ method called for each event ------------
131 void
133 {
134  using namespace edm;
135 
136  // create AMC 13 packet
137  amc13::Packet amc13;
138 
139  std::vector<uint32_t> load32;
140 
141  readEvent(load32);
142 
143  formatAMC(amc13, load32);
144 
145  LogDebug("L1T") << "AMC13 size " << amc13.size();
146 
147  // prepare the raw data collection
148  std::auto_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
149  FEDRawData& fed_data = raw_coll->FEDData(fedId_);
150 
151  formatRaw(iEvent, amc13, fed_data);
152 
153  LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
154 
155  // put the collection in the event
156  iEvent.put(raw_coll);
157 
158 }
159 
160 
161 void
162 AMCDumpToRaw::readEvent(std::vector<uint32_t>& load32) {
163 
164  // read file
166 
167  // while not encountering dumb errors
168  while (getline(file_, line) && !line.empty() ) {
169 
170  std::istringstream iss(line);
171  unsigned long d;
172  iss >> std::hex >> d;
173 
174  load32.push_back( d ) ;
175 
176  }
177 
178 }
179 
180 
181 void
182 AMCDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
183 
184  // TODO this is an empty word to be replaced with a proper MP7
185  // header containing at least the firmware version
186 
187  std::vector<uint64_t> load64;
188  for (unsigned int i = 0; i < load32.size(); i += 2) {
189  uint64_t word = load32[i];
190  if (i + 1 < load32.size())
191  word |= static_cast<uint64_t>(load32[i + 1]) << 32;
192  load64.push_back(word);
193  }
194 
195  LogDebug("L1T") << "Creating AMC packet " << iAmc_;
196 
197  amc13.add(iAmc_, boardId_, 0, 0, 0, load64);
198 
199 }
200 
201 
202 
203 void
205 {
206 
207  unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
208  fed_data.resize(size);
209  unsigned char * payload = fed_data.data();
210  unsigned char * payload_start = payload;
211 
212  auto bxId = iEvent.bunchCrossing();
213  auto evtId = iEvent.id().event();
214 
215  LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
216 
217  FEDHeader header(payload);
218  header.set(payload, evType_, evtId, bxId, fedId_);
219 
220  amc13.write(iEvent, payload, slinkHeaderSize_, size - slinkHeaderSize_ - slinkTrailerSize_);
221 
222  payload += slinkHeaderSize_;
223  payload += amc13.size() * 8;
224 
225  FEDTrailer trailer(payload);
226  trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
227 
228 }
229 
230 
231 // ------------ method called once each job just before starting event loop ------------
232 void
234 {
235 
236  // open VME file
237  file_.open(filename_.c_str(), std::ios::in);
238  if(!file_.good()) { edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl; }
239 
240 
241 }
242 
243 
244 // ------------ method called once each job just after ending the event loop ------------
245 void
247 {
248 
249  file_.close();
250 
251 }
252 
253 // ------------ method called when starting to processes a run ------------
254 /*
255 void
256 AMCDumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
257 {
258 }
259 */
260 
261 // ------------ method called when ending the processing of a run ------------
262 /*
263 void
264 AMCDumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
265 {
266 }
267 */
268 
269 // ------------ method called when starting to processes a luminosity block ------------
270 /*
271 vvoid
272 AMCDumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
273 {
274 }
275 */
276 
277 // ------------ method called when ending the processing of a luminosity block ------------
278 /*
279 void
280 AMCDumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
281 {
282 }
283 */
284 
285 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
286 void
288  //The following says we do not know what parameters are allowed so do no validation
289  // Please change this to state exactly what you do use, even if it is no parameters
291  desc.setUnknown();
292  descriptions.addDefault(desc);
293 }
294 
295 }
296 
297 using namespace l1t;
298 //define this as a plug-in
#define LogDebug(id)
virtual void beginJob() override
EventNumber_t event() const
Definition: EventID.h:41
int i
Definition: DBlmapReader.cc:9
virtual void produce(edm::Event &, const edm::EventSetup &) override
AMCDumpToRaw(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
unsigned int size() const
Definition: AMC13Spec.cc:200
int bunchCrossing() const
Definition: EventBase.h:66
bool write(const edm::Event &ev, unsigned char *ptr, unsigned int skip, unsigned int size) const
Definition: AMC13Spec.cc:218
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
tuple d
Definition: ztail.py:151
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
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
virtual void endJob() override
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
unsigned long long uint64_t
Definition: Time.h:15
void add(unsigned int amc_no, unsigned int board, unsigned int lv1id, unsigned int orbit, unsigned int bx, const std::vector< uint64_t > &load)
Definition: AMC13Spec.cc:61
void readEvent(std::vector< uint32_t > &load32)
std::string filename_
Definition: AMCDumpToRaw.cc:77
edm::EventID id() const
Definition: EventBase.h:60
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
std::ifstream file_
Definition: AMCDumpToRaw.cc:76
void formatAMC(amc13::Packet &amc13, const std::vector< uint32_t > &load32)
void formatRaw(edm::Event &iEvent, amc13::Packet &amc13, FEDRawData &fed_data)
tuple size
Write out results.