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