CMS 3D CMS Logo

RawEventFileWriterForBU.cc
Go to the documentation of this file.
1 #include <cerrno>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <iostream>
6 #include <sstream>
7 
8 // CMSSW headers
18 
19 using namespace jsoncollector;
20 
21 //TODO:get run directory information from DaqDirector
22 
24  : microSleep_(ps.getParameter<int>("microSleep")),
25  frdFileVersion_(ps.getParameter<unsigned int>("frdFileVersion")) {
26  //per-file JSD and FastMonitor
27  rawJsonDef_.setDefaultGroup("legend");
28  rawJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
29 
30  perFileEventCount_.setName("NEvents");
31  perFileSize_.setName("NBytes");
32 
33  fileMon_ = new FastMonitor(&rawJsonDef_, false);
36  fileMon_->commit(nullptr);
37 
38  //per-lumi JSD and FastMonitor
39  eolJsonDef_.setDefaultGroup("legend");
40  eolJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
42  eolJsonDef_.addLegendItem("TotalEvents", "integer", DataPointDefinition::SUM);
43  eolJsonDef_.addLegendItem("NLostEvents", "integer", DataPointDefinition::SUM);
44 
45  perLumiEventCount_.setName("NEvents");
46  perLumiFileCount_.setName("NFiles");
47  perLumiTotalEventCount_.setName("TotalEvents");
48  perLumiLostEventCount_.setName("NLostEvents");
49  perLumiSize_.setName("NBytes");
50 
51  lumiMon_ = new FastMonitor(&eolJsonDef_, false);
57  lumiMon_->commit(nullptr);
58 
59  //per-run JSD and FastMonitor
60  eorJsonDef_.setDefaultGroup("legend");
61  eorJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
64  eorJsonDef_.addLegendItem("LastLumi", "integer", DataPointDefinition::SUM);
65 
66  perRunEventCount_.setName("NEvents");
67  perRunFileCount_.setName("NFiles");
68  perRunLumiCount_.setName("NLumis");
69  perRunLastLumi_.setName("LastLumi");
70 
71  runMon_ = new FastMonitor(&eorJsonDef_, false);
76  runMon_->commit(nullptr);
77 }
78 
80 
82  delete fileMon_;
83  delete lumiMon_;
84  delete runMon_;
85 }
86 
88  ssize_t retval = write(outfd_, (void*)msg.startAddress(), msg.size());
89 
90  if ((unsigned)retval != msg.size()) {
91  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
92  << "Error writing FED Raw Data event data to " << fileName_ << ". Possibly the output disk "
93  << "is full?" << std::endl;
94  }
95 
96  // throttle event output
97  usleep(microSleep_);
99  perFileSize_.value() += msg.size();
100 
101  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
102 }
103 
104 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls) {
105  destinationDir_ = destinationDir;
106 
107  if (outfd_ != -1) {
109  closefd();
110  }
111 
112  fileName_ = name;
113 
114  if (!writtenJSDs_) {
115  writeJsds();
116  /* std::stringstream ss;
117  ss << destinationDir_ << "/jsd";
118  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
119 
120  std::string rawJSDName = ss.str()+"/rawData.jsd";
121  std::string eolJSDName = ss.str()+"/EoLS.jsd";
122  std::string eorJSDName = ss.str()+"/EoR.jsd";
123 
124  fileMon_->setDefPath(rawJSDName);
125  lumiMon_->setDefPath(eolJSDName);
126  runMon_->setDefPath(eorJSDName);
127 
128  struct stat fstat;
129  if (stat (rawJSDName.c_str(), &fstat) != 0) {
130  std::string content;
131  JSONSerializer::serialize(&rawJsonDef_,content);
132  FileIO::writeStringToFile(rawJSDName, content);
133  }
134 
135  if (stat (eolJSDName.c_str(), &fstat) != 0) {
136  std::string content;
137  JSONSerializer::serialize(&eolJsonDef_,content);
138  FileIO::writeStringToFile(eolJSDName, content);
139  }
140 
141  if (stat (eorJSDName.c_str(), &fstat) != 0) {
142  std::string content;
143  JSONSerializer::serialize(&eorJsonDef_,content);
144  FileIO::writeStringToFile(eorJSDName, content);
145  }
146 */
147  writtenJSDs_ = true;
148  }
149 
150  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
151  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
152 
153  if (outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
154  throw cms::Exception("RawEventFileWriterForBU", "initialize")
155  << "Error opening FED Raw Data event output file: " << name << ": " << strerror(errno) << "\n";
156  }
157 
159  perFileSize_.value() = 0;
160 
161  adlera_ = 1;
162  adlerb_ = 0;
163 
164  if (frdFileVersion_ == 1) {
165  //reserve space for file header
166  ftruncate(outfd_, sizeof(FRDFileHeader_v1));
167  lseek(outfd_, sizeof(FRDFileHeader_v1), SEEK_SET);
168  perFileSize_.value() = sizeof(FRDFileHeader_v1);
169  } else if (frdFileVersion_ == 2) {
170  ftruncate(outfd_, sizeof(FRDFileHeader_v2));
171  lseek(outfd_, sizeof(FRDFileHeader_v2), SEEK_SET);
172  perFileSize_.value() = sizeof(FRDFileHeader_v2);
173  }
174  assert(frdFileVersion_ <= 2);
175 }
176 
178  std::stringstream ss;
179  ss << destinationDir_ << "/jsd";
180  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
181 
182  std::string rawJSDName = ss.str() + "/rawData.jsd";
183  std::string eolJSDName = ss.str() + "/EoLS.jsd";
184  std::string eorJSDName = ss.str() + "/EoR.jsd";
185 
186  fileMon_->setDefPath(rawJSDName);
187  lumiMon_->setDefPath(eolJSDName);
188  runMon_->setDefPath(eorJSDName);
189 
190  struct stat fstat;
191  if (stat(rawJSDName.c_str(), &fstat) != 0) {
194  FileIO::writeStringToFile(rawJSDName, content);
195  }
196 
197  if (stat(eolJSDName.c_str(), &fstat) != 0) {
200  FileIO::writeStringToFile(eolJSDName, content);
201  }
202 
203  if (stat(eorJSDName.c_str(), &fstat) != 0) {
206  FileIO::writeStringToFile(eorJSDName, content);
207  }
208 }
209 
211  if (frdFileVersion_ == 1) {
212  //rewind
213  lseek(outfd_, 0, SEEK_SET);
214  FRDFileHeader_v1 frdFileHeader(perFileEventCount_.value(), (uint32_t)ls, perFileSize_.value());
215  write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v1));
216  closefd();
217  //move raw file from open to run directory
218  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
219 
220  edm::LogInfo("RawEventFileWriterForBU")
221  << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
222  << " and size " << perFileSize_.value();
223  } else if (frdFileVersion_ == 2) {
224  lseek(outfd_, 0, SEEK_SET);
225  FRDFileHeader_v2 frdFileHeader(0, perFileEventCount_.value(), (uint32_t)run_, (uint32_t)ls, perFileSize_.value());
226  write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v2));
227  closefd();
228  //move raw file from open to run directory
229  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
230  edm::LogInfo("RawEventFileWriterForBU")
231  << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
232  << " and size " << perFileSize_.value();
233  } else {
234  closefd();
235  //move raw file from open to run directory
236  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
237  //create equivalent JSON file
238  //TODO:fix this to use DaqDirector convention and better extension replace
240  std::string path = source.replace_extension(".jsn").string();
241 
242  fileMon_->snap(ls);
245 
246  //move the json file from open
247  rename(path.c_str(), (destinationDir_ + path.substr(path.rfind('/'))).c_str());
248 
249  edm::LogInfo("RawEventFileWriterForBU")
250  << "Wrote JSON input file: " << path << " with perFileEventCount = " << perFileEventCount_.value()
251  << " and size " << perFileSize_.value();
252  }
253  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
258  //update open lumi value when first file is completed
259  lumiOpen_ = ls;
260 }
261 
263  if (outfd_ != -1) {
265  closefd();
266  }
267  lumiMon_->snap(ls);
268 
269  std::ostringstream ostr;
270 
271  if (run_ == -1)
273 
274  ostr << destinationDir_ << "/" << runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS"
275  << ".jsn";
276  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
277  //closefd();
278 
279  std::string path = ostr.str();
282 
285  perRunLumiCount_.value() += 1;
287 
288  perLumiEventCount_ = 0;
289  perLumiFileCount_ = 0;
291  perLumiSize_ = 0;
292  lumiClosed_ = ls;
293 }
294 
296  if (lumiOpen_ > lumiClosed_)
298  edm::LogInfo("RawEventFileWriterForBU") << "Writing EOR file!";
299  if (!destinationDir_.empty()) {
300  // create EoR file
301  if (run_ == -1)
303  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
304  runMon_->snap(0);
306  }
307 }
308 
309 //TODO:get from DaqDirector !
311  //dirty hack: extract run number from destination directory
312  std::string::size_type pos = destinationDir.find("run");
313  std::string run = destinationDir.substr(pos + 3);
314  run_ = atoi(run.c_str());
315  std::stringstream ss;
316  ss << "run" << std::setfill('0') << std::setw(6) << run_;
317  runPrefix_ = ss.str();
318 }
319 
321  desc.add<int>("microSleep", 0);
322  desc.add<unsigned int>("frdFileVersion", 0);
323 }
void addLegendItem(std::string const &name, std::string const &type, std::string const &operation)
jsoncollector::FastMonitor * fileMon_
void initialize(std::string const &destinationDir, std::string const &name, int ls)
void setDefPath(std::string const &dpath)
Definition: FastMonitor.h:32
jsoncollector::IntJ perLumiLostEventCount_
std::pair< Binary, Binary > serialize(const T &payload)
Definition: Serialization.h:66
Definition: rename.py:1
jsoncollector::IntJ perFileEventCount_
jsoncollector::DataPointDefinition eolJsonDef_
jsoncollector::IntJ perRunLumiCount_
jsoncollector::IntJ perRunLastLumi_
jsoncollector::DataPointDefinition eorJsonDef_
jsoncollector::IntJ perLumiFileCount_
jsoncollector::IntJ perRunEventCount_
assert(be >=bs)
uint16_t size_type
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:67
jsoncollector::IntJ perLumiEventCount_
virtual void setName(std::string name)
jsoncollector::IntJ perLumiSize_
jsoncollector::FastMonitor * runMon_
jsoncollector::IntJ perRunFileCount_
static void extendDescription(edm::ParameterSetDescription &desc)
RawEventFileWriterForBU(edm::ParameterSet const &ps)
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:116
Log< level::Info, false > LogInfo
jsoncollector::DataPointDefinition rawJsonDef_
def ls(path, rec=False)
Definition: eostools.py:349
#define SUM(A, B)
bool outputFullJSON(std::string const &path, unsigned int lumi, bool output=true)
Definition: FastMonitor.cc:244
tuple msg
Definition: mps_check.py:286
def mkdir(path)
Definition: eostools.py:251
jsoncollector::IntJ perLumiTotalEventCount_
void snap(unsigned int ls)
Definition: FastMonitor.cc:190
jsoncollector::IntJ perFileSize_
void setDefaultGroup(std::string const &group)
void makeRunPrefix(std::string const &destinationDir)
JSON (JavaScript Object Notation).
Definition: DataPoint.h:26
static std::string const source
Definition: EdmProvDump.cc:49
void doOutputEvent(FRDEventMsgView const &msg)
jsoncollector::FastMonitor * lumiMon_
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:262