CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
7 
11 
12 #include <iostream>
13 #include <sstream>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <boost/filesystem/fstream.hpp>
19 
20 using namespace jsoncollector;
21 
22 
23 //TODO:get run directory information from DaqDirector
24 
26  // default to .5ms sleep per event
27  microSleep_(ps.getUntrackedParameter<int>("microSleep", 0))
28  //debug_(ps.getUntrackedParameter<bool>("debug", False))
29 {
30 
31  //per-file JSD and FastMonitor
32  rawJsonDef_.setDefaultGroup("legend");
34 
35  perFileEventCount_.setName("NEvents");
36  perFileSize_.setName("NBytes");
37 
38  fileMon_ = new FastMonitor(&rawJsonDef_,false);
41  fileMon_->commit(nullptr);
42 
43  //per-lumi JSD and FastMonitor
44  eolJsonDef_.setDefaultGroup("legend");
47  eolJsonDef_.addLegendItem("TotalEvents","integer",DataPointDefinition::SUM);
48  eolJsonDef_.addLegendItem("NLostEvents","integer",DataPointDefinition::SUM);
49 
50  perLumiEventCount_.setName("NEvents");
51  perLumiFileCount_.setName("NFiles");
52  perLumiTotalEventCount_.setName("TotalEvents");
53  perLumiLostEventCount_.setName("NLostEvents");
54  perLumiSize_.setName("NBytes");
55 
56  lumiMon_ = new FastMonitor(&eolJsonDef_,false);
62  lumiMon_->commit(nullptr);
63 
64 
65  //per-run JSD and FastMonitor
66  eorJsonDef_.setDefaultGroup("legend");
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 }
85 
87 {
88 
89 }
90 
92 {
93  delete fileMon_;
94  delete lumiMon_;
95  delete runMon_;
96 }
97 
99 {
100  ssize_t retval = write(outfd_,(void*)msg.startAddress(), msg.size());
101 
102  if((unsigned)retval!= msg.size()){
103  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
104  << "Error writing FED Raw Data event data to "
105  << fileName_ << ". Possibly the output disk "
106  << "is full?" << std::endl;
107  }
108 
109  // throttle event output
110  usleep(microSleep_);
112  perFileSize_.value()+=msg.size();
113 
114  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
115 }
116 
117 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize)
118 {
119 
120  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
121  << "Unsupported output mode ";
122 
123  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
124 }
125 
126 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
127 {
128  destinationDir_ = destinationDir;
129 
130  if (closefd()) finishFileWrite(ls);
131 
132  fileName_ = name;
133 
134  if (!writtenJSDs_) {
135  writeJsds();
136 /* std::stringstream ss;
137  ss << destinationDir_ << "/jsd";
138  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
139 
140  std::string rawJSDName = ss.str()+"/rawData.jsd";
141  std::string eolJSDName = ss.str()+"/EoLS.jsd";
142  std::string eorJSDName = ss.str()+"/EoR.jsd";
143 
144  fileMon_->setDefPath(rawJSDName);
145  lumiMon_->setDefPath(eolJSDName);
146  runMon_->setDefPath(eorJSDName);
147 
148  struct stat fstat;
149  if (stat (rawJSDName.c_str(), &fstat) != 0) {
150  std::string content;
151  JSONSerializer::serialize(&rawJsonDef_,content);
152  FileIO::writeStringToFile(rawJSDName, content);
153  }
154 
155  if (stat (eolJSDName.c_str(), &fstat) != 0) {
156  std::string content;
157  JSONSerializer::serialize(&eolJsonDef_,content);
158  FileIO::writeStringToFile(eolJSDName, content);
159  }
160 
161  if (stat (eorJSDName.c_str(), &fstat) != 0) {
162  std::string content;
163  JSONSerializer::serialize(&eorJsonDef_,content);
164  FileIO::writeStringToFile(eorJSDName, content);
165  }
166 */
167  writtenJSDs_=true;
168 
169  }
170 
171  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
172  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
173  if(outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
174  throw cms::Exception("RawEventFileWriterForBU","initialize")
175  << "Error opening FED Raw Data event output file: " << name
176  << ": " << strerror(errno) << "\n";
177  }
178 
180  perFileSize_.value() = 0;
181 
182 
183  adlera_ = 1;
184  adlerb_ = 0;
185 }
186 
188 {
189 
190  std::stringstream ss;
191  ss << destinationDir_ << "/jsd";
192  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
193 
194  std::string rawJSDName = ss.str()+"/rawData.jsd";
195  std::string eolJSDName = ss.str()+"/EoLS.jsd";
196  std::string eorJSDName = ss.str()+"/EoR.jsd";
197 
198  fileMon_->setDefPath(rawJSDName);
199  lumiMon_->setDefPath(eolJSDName);
200  runMon_->setDefPath(eorJSDName);
201 
202  struct stat fstat;
203  if (stat (rawJSDName.c_str(), &fstat) != 0) {
206  FileIO::writeStringToFile(rawJSDName, content);
207  }
208 
209  if (stat (eolJSDName.c_str(), &fstat) != 0) {
212  FileIO::writeStringToFile(eolJSDName, content);
213  }
214 
215  if (stat (eorJSDName.c_str(), &fstat) != 0) {
218  FileIO::writeStringToFile(eorJSDName, content);
219  }
220 }
221 
223 {
224 
225  //move raw file from open to run directory
226  rename(fileName_.c_str(),(destinationDir_+fileName_.substr(fileName_.rfind("/"))).c_str());
227 
228  //create equivalent JSON file
229  std::stringstream ss;
230  //TODO:fix this to use DaqDirector convention and better extension replace
232  std::string path = source.replace_extension(".jsn").string();
233 
234  fileMon_->snap(ls);
235  fileMon_->outputFullJSON(path, ls);
237 
238  //move the json file from open
239  rename(path.c_str(),(destinationDir_+path.substr(path.rfind("/"))).c_str());
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  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
249  << " with perFileEventCount = " << perFileEventCount_.value()
250  << " and size " << perFileSize_.value();
251 
252 }
253 
254 
256 {
257  if (closefd()) finishFileWrite(ls);
258  lumiMon_->snap(ls);
259 
260  std::ostringstream ostr;
261 
263 
264  ostr << destinationDir_ << "/"<< runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS" << ".jsn";
265  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
266  //closefd();
267 
268  std::string path = ostr.str();
269  lumiMon_->outputFullJSON(path, ls);
271 
274  perRunLumiCount_.value() += 1;
276 
277  perLumiEventCount_ = 0;
278  perLumiFileCount_ = 0;
280  perLumiSize_ = 0;
281  lumiClosed_ = ls;
282 }
283 
285 {
287  edm::LogInfo("RawEventFileWriterForBU") << "Writing EOR file!";
288  if (destinationDir_.size() > 0)
289  {
290  // create EoR file
292  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
293  runMon_->snap(0);
294  runMon_->outputFullJSON(path, 0);
295  }
296 }
297 
298 //TODO:get from DaqDirector !
300 {
301  //dirty hack: extract run number from destination directory
302  std::string::size_type pos = destinationDir.find("run");
303  std::string run = destinationDir.substr(pos+3);
304  run_=atoi(run.c_str());
305  std::stringstream ss;
306  ss << "run" << std::setfill('0') << std::setw(6) << run_;
307  runPrefix_ = ss.str();
308 }
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:30
jsoncollector::IntJ perLumiLostEventCount_
std::pair< Binary, Binary > serialize(const T &payload)
Definition: Serialization.h:54
jsoncollector::IntJ perFileEventCount_
jsoncollector::DataPointDefinition eolJsonDef_
jsoncollector::IntJ perRunLumiCount_
jsoncollector::IntJ perRunLastLumi_
jsoncollector::DataPointDefinition eorJsonDef_
jsoncollector::IntJ perLumiFileCount_
jsoncollector::IntJ perRunEventCount_
uint32 size() const
void doOutputEventFragment(unsigned char *dataPtr, unsigned long dataSize)
def ls
Definition: eostools.py:348
uint16_t size_type
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:63
uint8 * startAddress() const
jsoncollector::IntJ perLumiEventCount_
virtual void setName(std::string name)
jsoncollector::IntJ perLumiSize_
jsoncollector::FastMonitor * runMon_
jsoncollector::IntJ perRunFileCount_
RawEventFileWriterForBU(edm::ParameterSet const &ps)
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:110
jsoncollector::DataPointDefinition rawJsonDef_
#define SUM(A, B)
jsoncollector::IntJ perLumiTotalEventCount_
void snap(unsigned int ls)
Definition: FastMonitor.cc:191
def mkdir
Definition: eostools.py:250
jsoncollector::IntJ perFileSize_
void setDefaultGroup(std::string const &group)
void makeRunPrefix(std::string const &destinationDir)
static std::string const source
Definition: EdmProvDump.cc:43
void doOutputEvent(FRDEventMsgView const &msg)
jsoncollector::FastMonitor * lumiMon_
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:290
bool outputFullJSON(std::string const &path, unsigned int lumi)
Definition: FastMonitor.cc:273