CMS 3D CMS Logo

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 // system include files
20 #include <memory>
21 
22 // user include files
30 
35 
37 
38 #include <fstream>
39 #include <iostream>
40 #include <sstream>
41 #include <string>
42 #include <iomanip>
43 #include <boost/algorithm/string.hpp>
44 
46 
47 namespace l1t {
48 
49  class AMCDumpToRaw : public edm::EDProducer {
50  public:
51  explicit AMCDumpToRaw(const edm::ParameterSet&);
52  ~AMCDumpToRaw() override;
53 
54  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
55 
56  private:
57  void beginJob() override;
58  void produce(edm::Event&, const edm::EventSetup&) override;
59  void endJob() override;
60 
61  void readEvent(std::vector<uint32_t>& load32);
62 
63  void formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32);
64 
66 
67  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
68  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
69  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
70  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
71 
72  // ----------member data ---------------------------
73  std::ifstream file_;
75 
76  // DAQ params
77  int fedId_;
78  int iAmc_;
79  int boardId_;
80  int evType_;
81  int fwVer_;
82  int slinkHeaderSize_; // in 8-bit words
84  };
85 
86  //
87  // constants, enums and typedefs
88  //
89 
90  //
91  // static data member definitions
92  //
93 
94  //
95  // constructors and destructor
96  //
98  : filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
99  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
100  iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
101  boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
102  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
103  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
104  slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
105  slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
106  produces<FEDRawDataCollection>();
107  }
108 
110  // do anything here that needs to be done at desctruction time
111  // (e.g. close files, deallocate resources etc.)
112  }
113 
114  //
115  // member functions
116  //
117 
118  // ------------ method called for each event ------------
120  using namespace edm;
121 
122  // create AMC 13 packet
124 
125  std::vector<uint32_t> load32;
126 
127  readEvent(load32);
128 
129  formatAMC(amc13, load32);
130 
131  LogDebug("L1T") << "AMC13 size " << amc13.size();
132 
133  // prepare the raw data collection
134  std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
135  FEDRawData& fed_data = raw_coll->FEDData(fedId_);
136 
137  formatRaw(iEvent, amc13, fed_data);
138 
139  LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
140 
141  // put the collection in the event
142  iEvent.put(std::move(raw_coll));
143  }
144 
145  void AMCDumpToRaw::readEvent(std::vector<uint32_t>& load32) {
146  // read file
148 
149  // while not encountering dumb errors
150  while (getline(file_, line) && !line.empty()) {
151  std::istringstream iss(line);
152  unsigned long d;
153  iss >> std::hex >> d;
154 
155  load32.push_back(d);
156  }
157  }
158 
159  void AMCDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
160  // TODO this is an empty word to be replaced with a proper MP7
161  // header containing at least the firmware version
162 
163  std::vector<uint64_t> load64;
164  for (unsigned int i = 0; i < load32.size(); i += 2) {
165  uint64_t word = load32[i];
166  if (i + 1 < load32.size())
167  word |= static_cast<uint64_t>(load32[i + 1]) << 32;
168  load64.push_back(word);
169  }
170 
171  LogDebug("L1T") << "Creating AMC packet " << iAmc_;
172 
173  amc13.add(iAmc_, boardId_, 0, 0, 0, load64);
174  }
175 
177  unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
178  fed_data.resize(size);
179  unsigned char* payload = fed_data.data();
180  unsigned char* payload_start = payload;
181 
182  auto bxId = iEvent.bunchCrossing();
183  auto evtId = iEvent.id().event();
184 
185  LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
186 
188  header.set(payload, evType_, evtId, bxId, fedId_);
189 
191 
193  payload += amc13.size() * 8;
194 
195  FEDTrailer trailer(payload);
196  trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
197  }
198 
199  // ------------ method called once each job just before starting event loop ------------
201  // open VME file
202  file_.open(filename_.c_str(), std::ios::in);
203  if (!file_.good()) {
204  edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
205  }
206  }
207 
208  // ------------ method called once each job just after ending the event loop ------------
209  void AMCDumpToRaw::endJob() { file_.close(); }
210 
211  // ------------ method called when starting to processes a run ------------
212  /*
213 void
214 AMCDumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
215 {
216 }
217 */
218 
219  // ------------ method called when ending the processing of a run ------------
220  /*
221 void
222 AMCDumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
223 {
224 }
225 */
226 
227  // ------------ method called when starting to processes a luminosity block ------------
228  /*
229 vvoid
230 AMCDumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
231 {
232 }
233 */
234 
235  // ------------ method called when ending the processing of a luminosity block ------------
236  /*
237 void
238 AMCDumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
239 {
240 }
241 */
242 
243  // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
245  //The following says we do not know what parameters are allowed so do no validation
246  // Please change this to state exactly what you do use, even if it is no parameters
248  desc.setUnknown();
249  descriptions.addDefault(desc);
250  }
251 
252 } // namespace l1t
253 
254 using namespace l1t;
255 //define this as a plug-in
size
Write out results.
void beginJob() override
void produce(edm::Event &, const edm::EventSetup &) override
AMCDumpToRaw(const edm::ParameterSet &)
Definition: AMCDumpToRaw.cc:97
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
delete x;
Definition: CaloConfig.h:22
uint64_t word
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:31
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void resize(size_t newsize)
Definition: FEDRawData.cc:28
~AMCDumpToRaw() override
void endJob() override
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:46
d
Definition: ztail.py:151
Log< level::Info, false > LogInfo
unsigned long long uint64_t
Definition: Time.h:13
void readEvent(std::vector< uint32_t > &load32)
std::string filename_
Definition: AMCDumpToRaw.cc:74
HLT enums.
std::ifstream file_
Definition: AMCDumpToRaw.cc:73
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
void formatAMC(amc13::Packet &amc13, const std::vector< uint32_t > &load32)
void formatRaw(edm::Event &iEvent, amc13::Packet &amc13, FEDRawData &fed_data)
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)