CMS 3D CMS Logo

TextToRaw.cc
Go to the documentation of this file.
1 
2 
4 
5 // system
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9 #include <vector>
10 
11 // framework
13 
15 
16 // Raw data collection
18 
19 using std::cerr;
20 using std::cout;
21 using std::endl;
22 using std::ios;
23 using std::string;
24 using std::vector;
25 
26 const unsigned TextToRaw::EVT_MAX_SIZE;
27 
29  : fedId_(iConfig.getUntrackedParameter<int>("fedId", 745)),
30  filename_(iConfig.getUntrackedParameter<std::string>("filename", "slinkOutput.txt")),
31  fileEventOffset_(iConfig.getUntrackedParameter<int>("FileEventOffset", 0)),
32  nevt_(0) {
33  edm::LogInfo("TextToDigi") << "Reading ASCII dump from " << filename_ << std::endl;
34 
35  // register the products
36  produces<FEDRawDataCollection>();
37 }
38 
40  // do anything here that needs to be done at desctruction time
41  // (e.g. close files, deallocate resources etc.)
42 }
43 
46  std::unique_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
47  // FEDRawData& feddata=rawColl->FEDData(fedId_);
48  // feddata.data()[0] = 0;
49  iEvent.put(std::move(rawColl));
50 }
51 
52 // ------------ method called to produce the data ------------
54  using namespace edm;
55 
56  // Skip event if required
57  if (nevt_ < fileEventOffset_) {
59  nevt_++;
60  return;
61  } else if (nevt_ == 0 && fileEventOffset_ < 0) {
63  // skip first fileEventOffset input crossings
64  for (unsigned i = 0; i < (unsigned)abs(fileEventOffset_); i++) {
65  unsigned iline = 0;
66  while (getline(file_, line) && !line.empty()) {
67  iline++;
68  if (iline * 4 >= EVT_MAX_SIZE)
69  throw cms::Exception("TextToRawEventSizeOverflow") << "TextToRaw::produce() : "
70  << " read too many lines (" << iline << ": " << line << ")"
71  << ", maximum event size is " << EVT_MAX_SIZE << std::endl;
72  }
73  }
74  }
75 
76  nevt_++;
77 
78  // read file
80  unsigned i = 0; // count 32-bit words
81 
82  // while not encountering dumb errors
83  while (getline(file_, line) && !line.empty()) {
84  // bail if we reached the EVT_MAX_SIZE
85  if (i * 4 >= EVT_MAX_SIZE) {
86  throw cms::Exception("TextToRaw") << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE
87  << " lines" << std::endl;
88  }
89 
90  // convert string to int
91  std::istringstream iss(line);
92  unsigned long d;
93  iss >> std::hex >> d;
94 
95  // copy data
96  for (int j = 0; j < 4; j++) {
97  if ((i * 4 + j) < EVT_MAX_SIZE) {
98  char c = (d >> (8 * j)) & 0xff;
99  data_[i * 4 + j] = c;
100  }
101  }
102 
103  ++i;
104 
105  // bail if we reached the EVT_MAX_SIZE
106  if (i >= EVT_MAX_SIZE) {
107  throw cms::Exception("TextToRaw") << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE
108  << " lines" << std::endl;
109  }
110  }
111 
112  unsigned evtSize = i * 4;
113 
114  // create the collection
115  std::unique_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
116  // retrieve the target buffer
117  FEDRawData &feddata = rawColl->FEDData(fedId_);
118  // Allocate space for header+trailer+payload
119  feddata.resize(evtSize);
120 
121  // fill FEDRawData object
122  for (unsigned i = 0; i < evtSize; ++i) {
123  feddata.data()[i] = data_[i];
124  }
125 
126  // put the collection in the event
127  iEvent.put(std::move(rawColl));
128 }
129 
130 // ------------ method called once each job just before starting event loop
131 // ------------
133  // open VME file
134  file_.open(filename_.c_str(), std::ios::in);
135  if (!file_.good()) {
136  edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
137  }
138 }
139 
140 // ------------ method called once each job just after ending the event loop
141 // ------------
142 void TextToRaw::endJob() { file_.close(); }
std::ifstream file_
Definition: TextToRaw.h:57
void putEmptyDigi(edm::Event &)
Append empty digi collection.
Definition: TextToRaw.cc:45
int fileEventOffset_
Definition: TextToRaw.h:63
void produce(edm::Event &, const edm::EventSetup &) override
Definition: TextToRaw.cc:53
void beginJob() override
Definition: TextToRaw.cc:132
int nevt_
Definition: TextToRaw.h:64
std::string filename_
Definition: TextToRaw.h:56
char data_[EVT_MAX_SIZE]
Definition: TextToRaw.h:61
static constexpr unsigned EVT_MAX_SIZE
Definition: TextToRaw.h:60
int iEvent
Definition: GenABIO.cc:224
TextToRaw(const edm::ParameterSet &)
Definition: TextToRaw.cc:28
~TextToRaw() override
Definition: TextToRaw.cc:39
void endJob() override
Definition: TextToRaw.cc:142
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
d
Definition: ztail.py:151
int fedId_
Definition: TextToRaw.h:53
Log< level::Info, false > LogInfo
void resize(size_t newsize, size_t wordsize=8)
Definition: FEDRawData.cc:28
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
def move(src, dest)
Definition: eostools.py:511