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 
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  // ----------member data ---------------------------
68  std::ifstream file_;
70 
71  // DAQ params
72  int fedId_;
73  int iAmc_;
74  int boardId_;
75  int evType_;
76  int fwVer_;
77  int slinkHeaderSize_; // in 8-bit words
79  };
80 
81  //
82  // constants, enums and typedefs
83  //
84 
85  //
86  // static data member definitions
87  //
88 
89  //
90  // constructors and destructor
91  //
93  : filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
94  fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
95  iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
96  boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
97  evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
98  fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
99  slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
100  slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
101  produces<FEDRawDataCollection>();
102  }
103 
105  // do anything here that needs to be done at desctruction time
106  // (e.g. close files, deallocate resources etc.)
107  }
108 
109  //
110  // member functions
111  //
112 
113  // ------------ method called for each event ------------
115  using namespace edm;
116 
117  // create AMC 13 packet
119 
120  std::vector<uint32_t> load32;
121 
122  readEvent(load32);
123 
124  formatAMC(amc13, load32);
125 
126  LogDebug("L1T") << "AMC13 size " << amc13.size();
127 
128  // prepare the raw data collection
129  std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
130  FEDRawData& fed_data = raw_coll->FEDData(fedId_);
131 
132  formatRaw(iEvent, amc13, fed_data);
133 
134  LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
135 
136  // put the collection in the event
137  iEvent.put(std::move(raw_coll));
138  }
139 
140  void AMCDumpToRaw::readEvent(std::vector<uint32_t>& load32) {
141  // read file
143 
144  // while not encountering dumb errors
145  while (getline(file_, line) && !line.empty()) {
146  std::istringstream iss(line);
147  unsigned long d;
148  iss >> std::hex >> d;
149 
150  load32.push_back(d);
151  }
152  }
153 
154  void AMCDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
155  // TODO this is an empty word to be replaced with a proper MP7
156  // header containing at least the firmware version
157 
158  std::vector<uint64_t> load64;
159  for (unsigned int i = 0; i < load32.size(); i += 2) {
160  uint64_t word = load32[i];
161  if (i + 1 < load32.size())
162  word |= static_cast<uint64_t>(load32[i + 1]) << 32;
163  load64.push_back(word);
164  }
165 
166  LogDebug("L1T") << "Creating AMC packet " << iAmc_;
167 
168  amc13.add(iAmc_, boardId_, 0, 0, 0, load64);
169  }
170 
172  unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
173  fed_data.resize(size);
174  unsigned char* payload = fed_data.data();
175  unsigned char* payload_start = payload;
176 
177  auto bxId = iEvent.bunchCrossing();
178  auto evtId = iEvent.id().event();
179 
180  LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
181 
183  header.set(payload, evType_, evtId, bxId, fedId_);
184 
186 
188  payload += amc13.size() * 8;
189 
190  FEDTrailer trailer(payload);
191  trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
192  }
193 
194  // ------------ method called once each job just before starting event loop ------------
196  // open VME file
197  file_.open(filename_.c_str(), std::ios::in);
198  if (!file_.good()) {
199  edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
200  }
201  }
202 
203  // ------------ method called once each job just after ending the event loop ------------
204  void AMCDumpToRaw::endJob() { file_.close(); }
205 
206  // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
208  //The following says we do not know what parameters are allowed so do no validation
209  // Please change this to state exactly what you do use, even if it is no parameters
211  desc.setUnknown();
212  descriptions.addDefault(desc);
213  }
214 
215 } // namespace l1t
216 
217 using namespace l1t;
218 //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:92
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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:69
HLT enums.
std::ifstream file_
Definition: AMCDumpToRaw.cc:68
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)