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 <iomanip>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <signal.h>
20 #include <boost/filesystem/fstream.hpp>
21 
22 using namespace jsoncollector;
23 
24 
25 //TODO:get run directory information from DaqDirector
26 
28 
30  // default to .5ms sleep per event
31  microSleep_(ps.getUntrackedParameter<int>("microSleep", 0))
32  //debug_(ps.getUntrackedParameter<bool>("debug", False))
33 {
34 
35  //per-file JSD and FastMonitor
36  rawJsonDef_.setDefaultGroup("legend");
38 
39  perFileEventCount_.setName("NEvents");
40  perFileSize_.setName("NBytes");
41 
42  fileMon_ = new FastMonitor(&rawJsonDef_,false);
45  fileMon_->commit(nullptr);
46 
47  //per-lumi JSD and FastMonitor
48  eolJsonDef_.setDefaultGroup("legend");
51  eolJsonDef_.addLegendItem("TotalEvents","integer",DataPointDefinition::SUM);
52  eolJsonDef_.addLegendItem("NLostEvents","integer",DataPointDefinition::SUM);
53 
54  perLumiEventCount_.setName("NEvents");
55  perLumiFileCount_.setName("NFiles");
56  perLumiTotalEventCount_.setName("TotalEvents");
57  perLumiLostEventCount_.setName("NLostEvents");
58  perLumiSize_.setName("NBytes");
59 
60  lumiMon_ = new FastMonitor(&eolJsonDef_,false);
66  lumiMon_->commit(nullptr);
67 
68 
69  //per-run JSD and FastMonitor
70  eorJsonDef_.setDefaultGroup("legend");
75 
76  perRunEventCount_.setName("NEvents");
77  perRunFileCount_.setName("NFiles");
78  perRunLumiCount_.setName("NLumis");
79  perRunLastLumi_.setName("LastLumi");
80 
81  runMon_ = new FastMonitor(&eorJsonDef_,false);
86  runMon_->commit(nullptr);
87 
88  instance = this;
89 
90  // SIGINT Handler
91  struct sigaction sigIntHandler;
92  sigIntHandler.sa_handler = RawEventFileWriterForBU::staticHandler;
93  sigemptyset(&sigIntHandler.sa_mask);
94  sigIntHandler.sa_flags = 0;
95  sigaction(SIGINT, &sigIntHandler, NULL);
96 
97 }
98 
100 {
101 
102 }
103 
105 {
106  delete fileMon_;
107  delete lumiMon_;
108  delete runMon_;
109 }
110 
112 {
113  ssize_t retval = write(outfd_,(void*)msg.startAddress(), msg.size());
114 
115  if((unsigned)retval!= msg.size()){
116  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
117  << "Error writing FED Raw Data event data to "
118  << fileName_ << ". Possibly the output disk "
119  << "is full?" << std::endl;
120  }
121 
122  // throttle event output
123  usleep(microSleep_);
125  perFileSize_.value()+=msg.size();
126 
127  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
128 }
129 
130 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize)
131 {
132 
133  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
134  << "Unsupported output mode ";
135 
136  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
137 }
138 
139 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
140 {
141  destinationDir_ = destinationDir;
142 
143  if (closefd()) finishFileWrite(ls);
144 
145  fileName_ = name;
146 
147  if (!writtenJSDs_) {
148  writeJsds();
149 /* std::stringstream ss;
150  ss << destinationDir_ << "/jsd";
151  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
152 
153  std::string rawJSDName = ss.str()+"/rawData.jsd";
154  std::string eolJSDName = ss.str()+"/EoLS.jsd";
155  std::string eorJSDName = ss.str()+"/EoR.jsd";
156 
157  fileMon_->setDefPath(rawJSDName);
158  lumiMon_->setDefPath(eolJSDName);
159  runMon_->setDefPath(eorJSDName);
160 
161  struct stat fstat;
162  if (stat (rawJSDName.c_str(), &fstat) != 0) {
163  std::string content;
164  JSONSerializer::serialize(&rawJsonDef_,content);
165  FileIO::writeStringToFile(rawJSDName, content);
166  }
167 
168  if (stat (eolJSDName.c_str(), &fstat) != 0) {
169  std::string content;
170  JSONSerializer::serialize(&eolJsonDef_,content);
171  FileIO::writeStringToFile(eolJSDName, content);
172  }
173 
174  if (stat (eorJSDName.c_str(), &fstat) != 0) {
175  std::string content;
176  JSONSerializer::serialize(&eorJsonDef_,content);
177  FileIO::writeStringToFile(eorJSDName, content);
178  }
179 */
180  writtenJSDs_=true;
181 
182  }
183 
184  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
185  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
186  if(outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
187  throw cms::Exception("RawEventFileWriterForBU","initialize")
188  << "Error opening FED Raw Data event output file: " << name
189  << ": " << strerror(errno) << "\n";
190  }
191 
193  perFileSize_.value() = 0;
194 
195 
196  adlera_ = 1;
197  adlerb_ = 0;
198 }
199 
201 {
202 
203  std::stringstream ss;
204  ss << destinationDir_ << "/jsd";
205  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
206 
207  std::string rawJSDName = ss.str()+"/rawData.jsd";
208  std::string eolJSDName = ss.str()+"/EoLS.jsd";
209  std::string eorJSDName = ss.str()+"/EoR.jsd";
210 
211  fileMon_->setDefPath(rawJSDName);
212  lumiMon_->setDefPath(eolJSDName);
213  runMon_->setDefPath(eorJSDName);
214 
215  struct stat fstat;
216  if (stat (rawJSDName.c_str(), &fstat) != 0) {
219  FileIO::writeStringToFile(rawJSDName, content);
220  }
221 
222  if (stat (eolJSDName.c_str(), &fstat) != 0) {
225  FileIO::writeStringToFile(eolJSDName, content);
226  }
227 
228  if (stat (eorJSDName.c_str(), &fstat) != 0) {
231  FileIO::writeStringToFile(eorJSDName, content);
232  }
233 }
234 
236 {
237 
238  //move raw file from open to run directory
239  rename(fileName_.c_str(),(destinationDir_+fileName_.substr(fileName_.rfind("/"))).c_str());
240 
241  //create equivalent JSON file
242  std::stringstream ss;
243  //TODO:fix this to use DaqDirector convention and better extension replace
245  std::string path = source.replace_extension(".jsn").string();
246 
247  fileMon_->snap(ls);
248  fileMon_->outputFullJSON(path, ls, false);
250 
251  //move the json file from open
252  rename(path.c_str(),(destinationDir_+path.substr(path.rfind("/"))).c_str());
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  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
262  << " with perFileEventCount = " << perFileEventCount_.value()
263  << " and size " << perFileSize_.value();
264 
265 }
266 
267 
269 {
270  if (closefd()) finishFileWrite(ls);
271  lumiMon_->snap(ls);
272 
273  std::ostringstream ostr;
274 
276 
277  ostr << destinationDir_ << "/"<< runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS" << ".jsn";
278  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
279  //closefd();
280 
281  std::string path = ostr.str();
282  lumiMon_->outputFullJSON(path, ls);
284 
287  perRunLumiCount_.value() += 1;
289 
290  perLumiEventCount_ = 0;
291  perLumiFileCount_ = 0;
293  perLumiSize_ = 0;
294  lumiClosed_ = ls;
295 }
296 
298 {
299  // create EoR file
300  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
301  runMon_->snap(0);
302  runMon_->outputFullJSON(path, 0);
303 }
304 
305 // runs on SIGINT and terminates the process
307 {
309  printf("Caught signal %d. Writing EOR file!\n",s);
310  if (destinationDir_.size() > 0)
311  {
312  // create EoR file
314  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
315  runMon_->snap(0);
316  runMon_->outputFullJSON(path, 0);
317  }
318  _exit(0);
319 }
320 
321 //TODO:get from DaqDirector !
323 {
324  //dirty hack: extract run number from destination directory
325  std::string::size_type pos = destinationDir.find("run");
326  std::string run = destinationDir.substr(pos+3);
327  run_=atoi(run.c_str());
328  std::stringstream ss;
329  ss << "run" << std::setfill('0') << std::setw(6) << run_;
330  runPrefix_ = ss.str();
331 }
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_
bool outputFullJSON(std::string const &path, unsigned int lumi, bool log=true)
Definition: FastMonitor.cc:249
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
#define NULL
Definition: scimark2.h:8
uint16_t size_type
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:62
uint8 * startAddress() const
jsoncollector::IntJ perLumiEventCount_
virtual void setName(std::string name)
jsoncollector::IntJ perLumiSize_
static void staticHandler(int s)
jsoncollector::FastMonitor * runMon_
jsoncollector::IntJ perRunFileCount_
RawEventFileWriterForBU(edm::ParameterSet const &ps)
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:109
jsoncollector::DataPointDefinition rawJsonDef_
#define SUM(A, B)
static RawEventFileWriterForBU * instance
jsoncollector::IntJ perLumiTotalEventCount_
void snap(unsigned int ls)
Definition: FastMonitor.cc:190
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:268