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_ > 0) {
165  assert(frdFileVersion_ == 1);
166  //reserve space for file header
167  ftruncate(outfd_, sizeof(FRDFileHeader_v1));
168  lseek(outfd_, sizeof(FRDFileHeader_v1), SEEK_SET);
169  perFileSize_.value() = sizeof(FRDFileHeader_v1);
170  }
171 }
172 
174  std::stringstream ss;
175  ss << destinationDir_ << "/jsd";
176  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
177 
178  std::string rawJSDName = ss.str() + "/rawData.jsd";
179  std::string eolJSDName = ss.str() + "/EoLS.jsd";
180  std::string eorJSDName = ss.str() + "/EoR.jsd";
181 
182  fileMon_->setDefPath(rawJSDName);
183  lumiMon_->setDefPath(eolJSDName);
184  runMon_->setDefPath(eorJSDName);
185 
186  struct stat fstat;
187  if (stat(rawJSDName.c_str(), &fstat) != 0) {
190  FileIO::writeStringToFile(rawJSDName, content);
191  }
192 
193  if (stat(eolJSDName.c_str(), &fstat) != 0) {
196  FileIO::writeStringToFile(eolJSDName, content);
197  }
198 
199  if (stat(eorJSDName.c_str(), &fstat) != 0) {
202  FileIO::writeStringToFile(eorJSDName, content);
203  }
204 }
205 
207  if (frdFileVersion_ > 0) {
208  //rewind
209  lseek(outfd_, 0, SEEK_SET);
210  FRDFileHeader_v1 frdFileHeader(perFileEventCount_.value(), (uint32_t)ls, perFileSize_.value());
211  write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v1));
212  closefd();
213  //move raw file from open to run directory
214  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
215 
216  edm::LogInfo("RawEventFileWriterForBU")
217  << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
218  << " and size " << perFileSize_.value();
219  } else {
220  closefd();
221  //move raw file from open to run directory
222  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind('/'))).c_str());
223  //create equivalent JSON file
224  //TODO:fix this to use DaqDirector convention and better extension replace
226  std::string path = source.replace_extension(".jsn").string();
227 
228  fileMon_->snap(ls);
231 
232  //move the json file from open
233  rename(path.c_str(), (destinationDir_ + path.substr(path.rfind('/'))).c_str());
234 
235  edm::LogInfo("RawEventFileWriterForBU")
236  << "Wrote JSON input file: " << path << " with perFileEventCount = " << perFileEventCount_.value()
237  << " and size " << perFileSize_.value();
238  }
239  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
244  //update open lumi value when first file is completed
245  lumiOpen_ = ls;
246 }
247 
249  if (outfd_ != -1) {
251  closefd();
252  }
253  lumiMon_->snap(ls);
254 
255  std::ostringstream ostr;
256 
257  if (run_ == -1)
259 
260  ostr << destinationDir_ << "/" << runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS"
261  << ".jsn";
262  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
263  //closefd();
264 
265  std::string path = ostr.str();
268 
271  perRunLumiCount_.value() += 1;
273 
274  perLumiEventCount_ = 0;
275  perLumiFileCount_ = 0;
277  perLumiSize_ = 0;
278  lumiClosed_ = ls;
279 }
280 
282  if (lumiOpen_ > lumiClosed_)
284  edm::LogInfo("RawEventFileWriterForBU") << "Writing EOR file!";
285  if (!destinationDir_.empty()) {
286  // create EoR file
287  if (run_ == -1)
289  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
290  runMon_->snap(0);
292  }
293 }
294 
295 //TODO:get from DaqDirector !
297  //dirty hack: extract run number from destination directory
298  std::string::size_type pos = destinationDir.find("run");
299  std::string run = destinationDir.substr(pos + 3);
300  run_ = atoi(run.c_str());
301  std::stringstream ss;
302  ss << "run" << std::setfill('0') << std::setw(6) << run_;
303  runPrefix_ = ss.str();
304 }
305 
307  desc.add<int>("microSleep", 0);
308  desc.add<unsigned int>("frdFileVersion", 0);
309 }
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:76
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:53
RawEventFileWriterForBU::perFileEventCount_
jsoncollector::IntJ perFileEventCount_
Definition: RawEventFileWriterForBU.h:66
MessageLogger.h
RawEventFileWriterForBU::eolJsonDef_
jsoncollector::DataPointDefinition eolJsonDef_
Definition: RawEventFileWriterForBU.h:74
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:104
RawEventFileWriterForBU::perRunLastLumi_
jsoncollector::IntJ perRunLastLumi_
Definition: RawEventFileWriterForBU.h:58
FRDEventMsgView
Definition: FRDEventMessage.h:137
RawEventFileWriterForBU::adlera_
uint32 adlera_
Definition: RawEventFileWriterForBU.h:85
RawEventFileWriterForBU::microSleep_
int microSleep_
Definition: RawEventFileWriterForBU.h:82
RawEventFileWriterForBU::eorJsonDef_
jsoncollector::DataPointDefinition eorJsonDef_
Definition: RawEventFileWriterForBU.h:75
RawEventFileWriterForBU::fileName_
std::string fileName_
Definition: RawEventFileWriterForBU.h:79
RawEventFileWriterForBU::perLumiLostEventCount_
jsoncollector::IntJ perLumiLostEventCount_
Definition: RawEventFileWriterForBU.h:63
jsoncollector::FastMonitor::commit
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:114
jsoncollector::FastMonitor::outputFullJSON
bool outputFullJSON(std::string const &path, unsigned int lumi, bool output=true)
Definition: FastMonitor.cc:266
pos
Definition: PixelAliasList.h:18
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
RawEventFileWriterForBU::perRunLumiCount_
jsoncollector::IntJ perRunLumiCount_
Definition: RawEventFileWriterForBU.h:57
cms::cuda::assert
assert(be >=bs)
RawEventFileWriterForBU::perLumiFileCount_
jsoncollector::IntJ perLumiFileCount_
Definition: RawEventFileWriterForBU.h:61
mps_check.msg
tuple msg
Definition: mps_check.py:285
RawEventFileWriterForBU::perRunEventCount_
jsoncollector::IntJ perRunEventCount_
Definition: RawEventFileWriterForBU.h:55
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:65
RawEventFileWriterForBU::lumiOpen_
unsigned int lumiOpen_
Definition: RawEventFileWriterForBU.h:88
RawEventFileWriterForBU::~RawEventFileWriterForBU
~RawEventFileWriterForBU()
Definition: RawEventFileWriterForBU.cc:81
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:281
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
RawEventFileWriterForBU::adlerb_
uint32 adlerb_
Definition: RawEventFileWriterForBU.h:86
jsoncollector::FastMonitor::setDefPath
void setDefPath(std::string const &dpath)
Definition: FastMonitor.h:32
RawEventFileWriterForBU::perLumiEventCount_
jsoncollector::IntJ perLumiEventCount_
Definition: RawEventFileWriterForBU.h:60
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:64
RawEventFileWriterForBU::frdFileVersion_
unsigned int frdFileVersion_
Definition: RawEventFileWriterForBU.h:83
RawEventFileWriterForBU::lumiClosed_
unsigned int lumiClosed_
Definition: RawEventFileWriterForBU.h:89
RawEventFileWriterForBU::runMon_
jsoncollector::FastMonitor * runMon_
Definition: RawEventFileWriterForBU.h:71
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
RawEventFileWriterForBU::closefd
bool closefd()
Definition: RawEventFileWriterForBU.h:40
RawEventFileWriterForBU::perRunFileCount_
jsoncollector::IntJ perRunFileCount_
Definition: RawEventFileWriterForBU.h:56
RawEventFileWriterForBU::writeJsds
void writeJsds()
Definition: RawEventFileWriterForBU.cc:173
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:206
RawEventFileWriterForBU::outfd_
int outfd_
Definition: RawEventFileWriterForBU.h:50
RawEventFileWriterForBU::rawJsonDef_
jsoncollector::DataPointDefinition rawJsonDef_
Definition: RawEventFileWriterForBU.h:73
createfilelist.int
int
Definition: createfilelist.py:10
EvFDaqDirector.h
RawEventFileWriterForBU::extendDescription
static void extendDescription(edm::ParameterSetDescription &desc)
Definition: RawEventFileWriterForBU.cc:306
RawEventFileWriterForBU::RawEventFileWriterForBU
RawEventFileWriterForBU(edm::ParameterSet const &ps)
Definition: RawEventFileWriterForBU.cc:23
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:52
FRDFileHeader_v1
Definition: FRDFileHeader.h:22
RawEventFileWriterForBU::perLumiTotalEventCount_
jsoncollector::IntJ perLumiTotalEventCount_
Definition: RawEventFileWriterForBU.h:62
RawEventFileWriterForBU::perFileSize_
jsoncollector::IntJ perFileSize_
Definition: RawEventFileWriterForBU.h:67
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
writedatasetfile.run
run
Definition: writedatasetfile.py:27
Exception
Definition: hltDiff.cc:245
RawEventFileWriterForBU::destinationDir_
std::string destinationDir_
Definition: RawEventFileWriterForBU.h:80
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:188
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:284
edm_modernize_messagelogger.stat
stat
Definition: edm_modernize_messagelogger.py:27
RawEventFileWriterForBU::makeRunPrefix
void makeRunPrefix(std::string const &destinationDir)
Definition: RawEventFileWriterForBU.cc:296
RawEventFileWriterForBU::fileMon_
jsoncollector::FastMonitor * fileMon_
Definition: RawEventFileWriterForBU.h:69
RawEventFileWriterForBU::endOfLS
void endOfLS(int ls)
Definition: RawEventFileWriterForBU.cc:248
RawEventFileWriterForBU::doOutputEvent
void doOutputEvent(FRDEventMsgView const &msg)
Definition: RawEventFileWriterForBU.cc:87
RawEventFileWriterForBU::lumiMon_
jsoncollector::FastMonitor * lumiMon_
Definition: RawEventFileWriterForBU.h:70