CMS 3D CMS Logo

RawEventFileWriterForBU.cc
Go to the documentation of this file.
1 // $Id: RawEventFileWriterForBU.cc,v 1.1.2.6 2013/03/28 14:56:53 aspataru Exp $
2 
9 
13 
14 #include <iostream>
15 #include <sstream>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cerrno>
19 #include <cstring>
20 #include <boost/filesystem/fstream.hpp>
21 
22 using namespace jsoncollector;
23 
24 //TODO:get run directory information from DaqDirector
25 
27  : // default to .5ms sleep per event
28  microSleep_(ps.getUntrackedParameter<int>("microSleep", 0)),
29  frdFileVersion_(ps.getUntrackedParameter<unsigned int>("frdFileVersion", 0))
30 //debug_(ps.getUntrackedParameter<bool>("debug", False))
31 {
32  //per-file JSD and FastMonitor
33  rawJsonDef_.setDefaultGroup("legend");
34  rawJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
35 
36  perFileEventCount_.setName("NEvents");
37  perFileSize_.setName("NBytes");
38 
39  fileMon_ = new FastMonitor(&rawJsonDef_, false);
42  fileMon_->commit(nullptr);
43 
44  //per-lumi JSD and FastMonitor
45  eolJsonDef_.setDefaultGroup("legend");
46  eolJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
48  eolJsonDef_.addLegendItem("TotalEvents", "integer", DataPointDefinition::SUM);
49  eolJsonDef_.addLegendItem("NLostEvents", "integer", DataPointDefinition::SUM);
50 
51  perLumiEventCount_.setName("NEvents");
52  perLumiFileCount_.setName("NFiles");
53  perLumiTotalEventCount_.setName("TotalEvents");
54  perLumiLostEventCount_.setName("NLostEvents");
55  perLumiSize_.setName("NBytes");
56 
57  lumiMon_ = new FastMonitor(&eolJsonDef_, false);
63  lumiMon_->commit(nullptr);
64 
65  //per-run JSD and FastMonitor
66  eorJsonDef_.setDefaultGroup("legend");
67  eorJsonDef_.addLegendItem("NEvents", "integer", DataPointDefinition::SUM);
70  eorJsonDef_.addLegendItem("LastLumi", "integer", DataPointDefinition::SUM);
71 
72  perRunEventCount_.setName("NEvents");
73  perRunFileCount_.setName("NFiles");
74  perRunLumiCount_.setName("NLumis");
75  perRunLastLumi_.setName("LastLumi");
76 
77  runMon_ = new FastMonitor(&eorJsonDef_, false);
82  runMon_->commit(nullptr);
83 }
84 
86 
88  delete fileMon_;
89  delete lumiMon_;
90  delete runMon_;
91 }
92 
94  ssize_t retval = write(outfd_, (void*)msg.startAddress(), msg.size());
95 
96  if ((unsigned)retval != msg.size()) {
97  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
98  << "Error writing FED Raw Data event data to " << fileName_ << ". Possibly the output disk "
99  << "is full?" << std::endl;
100  }
101 
102  // throttle event output
103  usleep(microSleep_);
105  perFileSize_.value() += msg.size();
106 
107  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
108 }
109 
110 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize) {
111  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment") << "Unsupported output mode ";
112 
113  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
114 }
115 
116 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls) {
117  destinationDir_ = destinationDir;
118 
119  if (outfd_ != -1) {
121  closefd();
122  }
123 
124  fileName_ = name;
125 
126  if (!writtenJSDs_) {
127  writeJsds();
128  /* std::stringstream ss;
129  ss << destinationDir_ << "/jsd";
130  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
131 
132  std::string rawJSDName = ss.str()+"/rawData.jsd";
133  std::string eolJSDName = ss.str()+"/EoLS.jsd";
134  std::string eorJSDName = ss.str()+"/EoR.jsd";
135 
136  fileMon_->setDefPath(rawJSDName);
137  lumiMon_->setDefPath(eolJSDName);
138  runMon_->setDefPath(eorJSDName);
139 
140  struct stat fstat;
141  if (stat (rawJSDName.c_str(), &fstat) != 0) {
142  std::string content;
143  JSONSerializer::serialize(&rawJsonDef_,content);
144  FileIO::writeStringToFile(rawJSDName, content);
145  }
146 
147  if (stat (eolJSDName.c_str(), &fstat) != 0) {
148  std::string content;
149  JSONSerializer::serialize(&eolJsonDef_,content);
150  FileIO::writeStringToFile(eolJSDName, content);
151  }
152 
153  if (stat (eorJSDName.c_str(), &fstat) != 0) {
154  std::string content;
155  JSONSerializer::serialize(&eorJsonDef_,content);
156  FileIO::writeStringToFile(eorJSDName, content);
157  }
158 */
159  writtenJSDs_ = true;
160  }
161 
162  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
163  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
164 
165  if (outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
166  throw cms::Exception("RawEventFileWriterForBU", "initialize")
167  << "Error opening FED Raw Data event output file: " << name << ": " << strerror(errno) << "\n";
168  }
169 
171  perFileSize_.value() = 0;
172 
173  adlera_ = 1;
174  adlerb_ = 0;
175 
176  if (frdFileVersion_ > 0) {
177  assert(frdFileVersion_ == 1);
178  //reserve space for file header
179  ftruncate(outfd_, sizeof(FRDFileHeader_v1));
180  lseek(outfd_, sizeof(FRDFileHeader_v1), SEEK_SET);
181  perFileSize_.value() = sizeof(FRDFileHeader_v1);
182  }
183 }
184 
186  std::stringstream ss;
187  ss << destinationDir_ << "/jsd";
188  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
189 
190  std::string rawJSDName = ss.str() + "/rawData.jsd";
191  std::string eolJSDName = ss.str() + "/EoLS.jsd";
192  std::string eorJSDName = ss.str() + "/EoR.jsd";
193 
194  fileMon_->setDefPath(rawJSDName);
195  lumiMon_->setDefPath(eolJSDName);
196  runMon_->setDefPath(eorJSDName);
197 
198  struct stat fstat;
199  if (stat(rawJSDName.c_str(), &fstat) != 0) {
202  FileIO::writeStringToFile(rawJSDName, content);
203  }
204 
205  if (stat(eolJSDName.c_str(), &fstat) != 0) {
208  FileIO::writeStringToFile(eolJSDName, content);
209  }
210 
211  if (stat(eorJSDName.c_str(), &fstat) != 0) {
214  FileIO::writeStringToFile(eorJSDName, content);
215  }
216 }
217 
219  if (frdFileVersion_ > 0) {
220  //rewind
221  lseek(outfd_, 0, SEEK_SET);
222  FRDFileHeader_v1 frdFileHeader(perFileEventCount_.value(), (uint32_t)ls, perFileSize_.value());
223  write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v1));
224  closefd();
225  //move raw file from open to run directory
226  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind("/"))).c_str());
227 
228  edm::LogInfo("RawEventFileWriterForBU")
229  << "Wrote RAW input file: " << fileName_ << " with perFileEventCount = " << perFileEventCount_.value()
230  << " and size " << perFileSize_.value();
231  } else {
232  closefd();
233  //move raw file from open to run directory
234  rename(fileName_.c_str(), (destinationDir_ + fileName_.substr(fileName_.rfind("/"))).c_str());
235  //create equivalent JSON file
236  //TODO:fix this to use DaqDirector convention and better extension replace
238  std::string path = source.replace_extension(".jsn").string();
239 
240  fileMon_->snap(ls);
243 
244  //move the json file from open
245  rename(path.c_str(), (destinationDir_ + path.substr(path.rfind("/"))).c_str());
246 
247  edm::LogInfo("RawEventFileWriterForBU")
248  << "Wrote JSON input file: " << path << " with perFileEventCount = " << perFileEventCount_.value()
249  << " and size " << perFileSize_.value();
250  }
251  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
256  //update open lumi value when first file is completed
257  lumiOpen_ = ls;
258 }
259 
261  if (outfd_ != -1) {
263  closefd();
264  }
265  lumiMon_->snap(ls);
266 
267  std::ostringstream ostr;
268 
269  if (run_ == -1)
271 
272  ostr << destinationDir_ << "/" << runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS"
273  << ".jsn";
274  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
275  //closefd();
276 
277  std::string path = ostr.str();
280 
283  perRunLumiCount_.value() += 1;
285 
286  perLumiEventCount_ = 0;
287  perLumiFileCount_ = 0;
289  perLumiSize_ = 0;
290  lumiClosed_ = ls;
291 }
292 
294  if (lumiOpen_ > lumiClosed_)
296  edm::LogInfo("RawEventFileWriterForBU") << "Writing EOR file!";
297  if (!destinationDir_.empty()) {
298  // create EoR file
299  if (run_ == -1)
301  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
302  runMon_->snap(0);
304  }
305 }
306 
307 //TODO:get from DaqDirector !
309  //dirty hack: extract run number from destination directory
310  std::string::size_type pos = destinationDir.find("run");
311  std::string run = destinationDir.substr(pos + 3);
312  run_ = atoi(run.c_str());
313  std::stringstream ss;
314  ss << "run" << std::setfill('0') << std::setw(6) << run_;
315  runPrefix_ = ss.str();
316 }
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:74
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:51
RawEventFileWriterForBU::perFileEventCount_
jsoncollector::IntJ perFileEventCount_
Definition: RawEventFileWriterForBU.h:64
MessageLogger.h
RawEventFileWriterForBU::eolJsonDef_
jsoncollector::DataPointDefinition eolJsonDef_
Definition: RawEventFileWriterForBU.h:72
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:116
RawEventFileWriterForBU::perRunLastLumi_
jsoncollector::IntJ perRunLastLumi_
Definition: RawEventFileWriterForBU.h:56
FRDEventMsgView
Definition: FRDEventMessage.h:137
RawEventFileWriterForBU::adlera_
uint32 adlera_
Definition: RawEventFileWriterForBU.h:83
RawEventFileWriterForBU::microSleep_
int microSleep_
Definition: RawEventFileWriterForBU.h:80
RawEventFileWriterForBU::eorJsonDef_
jsoncollector::DataPointDefinition eorJsonDef_
Definition: RawEventFileWriterForBU.h:73
RawEventFileWriterForBU::fileName_
std::string fileName_
Definition: RawEventFileWriterForBU.h:77
RawEventFileWriterForBU::perLumiLostEventCount_
jsoncollector::IntJ perLumiLostEventCount_
Definition: RawEventFileWriterForBU.h:61
jsoncollector::FastMonitor::commit
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:115
pos
Definition: PixelAliasList.h:18
edm::LogInfo
Definition: MessageLogger.h:254
RawEventFileWriterForBU::perRunLumiCount_
jsoncollector::IntJ perRunLumiCount_
Definition: RawEventFileWriterForBU.h:55
cms::cuda::assert
assert(be >=bs)
RawEventFileWriterForBU::perLumiFileCount_
jsoncollector::IntJ perLumiFileCount_
Definition: RawEventFileWriterForBU.h:59
mps_check.msg
tuple msg
Definition: mps_check.py:285
RawEventFileWriterForBU::perRunEventCount_
jsoncollector::IntJ perRunEventCount_
Definition: RawEventFileWriterForBU.h:53
FRDEventMessage.h
RawEventFileWriterForBU::doOutputEventFragment
void doOutputEventFragment(unsigned char *dataPtr, unsigned long dataSize)
Definition: RawEventFileWriterForBU.cc:110
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:86
RawEventFileWriterForBU::~RawEventFileWriterForBU
~RawEventFileWriterForBU()
Definition: RawEventFileWriterForBU.cc:87
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:293
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
RawEventFileWriterForBU::adlerb_
uint32 adlerb_
Definition: RawEventFileWriterForBU.h:84
hgcalPlots.stat
stat
Definition: hgcalPlots.py:1111
jsoncollector::FastMonitor::setDefPath
void setDefPath(std::string const &dpath)
Definition: FastMonitor.h:32
RawEventFileWriterForBU::perLumiEventCount_
jsoncollector::IntJ perLumiEventCount_
Definition: RawEventFileWriterForBU.h:58
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:62
RawEventFileWriterForBU::frdFileVersion_
unsigned int frdFileVersion_
Definition: RawEventFileWriterForBU.h:81
RawEventFileWriterForBU::lumiClosed_
unsigned int lumiClosed_
Definition: RawEventFileWriterForBU.h:87
RawEventFileWriterForBU::runMon_
jsoncollector::FastMonitor * runMon_
Definition: RawEventFileWriterForBU.h:69
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
RawEventFileWriterForBU::closefd
bool closefd()
Definition: RawEventFileWriterForBU.h:38
RawEventFileWriterForBU::perRunFileCount_
jsoncollector::IntJ perRunFileCount_
Definition: RawEventFileWriterForBU.h:54
RawEventFileWriterForBU::writeJsds
void writeJsds()
Definition: RawEventFileWriterForBU.cc:185
Skims_PA_cff.content
content
Definition: Skims_PA_cff.py:19
edm::ParameterSet
Definition: ParameterSet.h:36
RawEventFileWriterForBU::finishFileWrite
void finishFileWrite(int ls)
Definition: RawEventFileWriterForBU.cc:218
RawEventFileWriterForBU::outfd_
int outfd_
Definition: RawEventFileWriterForBU.h:48
RawEventFileWriterForBU::rawJsonDef_
jsoncollector::DataPointDefinition rawJsonDef_
Definition: RawEventFileWriterForBU.h:71
createfilelist.int
int
Definition: createfilelist.py:10
EvFDaqDirector.h
RawEventFileWriterForBU::RawEventFileWriterForBU
RawEventFileWriterForBU(edm::ParameterSet const &ps)
Definition: RawEventFileWriterForBU.cc:26
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:50
FRDFileHeader_v1
Definition: FRDFileHeader.h:22
RawEventFileWriterForBU::perLumiTotalEventCount_
jsoncollector::IntJ perLumiTotalEventCount_
Definition: RawEventFileWriterForBU.h:60
RawEventFileWriterForBU::perFileSize_
jsoncollector::IntJ perFileSize_
Definition: RawEventFileWriterForBU.h:65
jsoncollector::FastMonitor::outputFullJSON
bool outputFullJSON(std::string const &path, unsigned int lumi)
Definition: FastMonitor.cc:264
writedatasetfile.run
run
Definition: writedatasetfile.py:27
Exception
Definition: hltDiff.cc:246
RawEventFileWriterForBU::destinationDir_
std::string destinationDir_
Definition: RawEventFileWriterForBU.h:78
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:308
RawEventFileWriterForBU::fileMon_
jsoncollector::FastMonitor * fileMon_
Definition: RawEventFileWriterForBU.h:67
RawEventFileWriterForBU::endOfLS
void endOfLS(int ls)
Definition: RawEventFileWriterForBU.cc:260
RawEventFileWriterForBU::doOutputEvent
void doOutputEvent(FRDEventMsgView const &msg)
Definition: RawEventFileWriterForBU.cc:93
RawEventFileWriterForBU::lumiMon_
jsoncollector::FastMonitor * lumiMon_
Definition: RawEventFileWriterForBU.h:68