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 //TODO:get run directory information from DaqDirector
23 
25 
27  // default to .5ms sleep per event
28  microSleep_(ps.getUntrackedParameter<int>("microSleep", 0))
29  //debug_(ps.getUntrackedParameter<bool>("debug", False))
30 {
31 
32  //per-file JSD and FastMonitor
33  rawJsonDef_.setDefaultGroup("legend");
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");
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 
66  //per-run JSD and FastMonitor
67  eorJsonDef_.setDefaultGroup("legend");
72 
73  perRunEventCount_.setName("NEvents");
74  perRunFileCount_.setName("NFiles");
75  perRunLumiCount_.setName("NLumis");
76  perRunLastLumi_.setName("LastLumi");
77 
78  runMon_ = new FastMonitor(&eorJsonDef_,false);
83  runMon_->commit(nullptr);
84 
85  instance = this;
86 
87  // SIGINT Handler
88  struct sigaction sigIntHandler;
89  sigIntHandler.sa_handler = RawEventFileWriterForBU::staticHandler;
90  sigemptyset(&sigIntHandler.sa_mask);
91  sigIntHandler.sa_flags = 0;
92  sigaction(SIGINT, &sigIntHandler, NULL);
93 
94 }
95 
97 {
98 
99 }
100 
102 {
103  delete fileMon_;
104  delete lumiMon_;
105  delete runMon_;
106 }
107 
109 {
110  ssize_t retval = write(outfd_,(void*)msg.startAddress(), msg.size());
111 
112  if((unsigned)retval!= msg.size()){
113  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
114  << "Error writing FED Raw Data event data to "
115  << fileName_ << ". Possibly the output disk "
116  << "is full?" << std::endl;
117  }
118 
119  // throttle event output
120  usleep(microSleep_);
122  perFileSize_.value()+=msg.size();
123 
124  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
125 }
126 
127 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize)
128 {
129 
130  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
131  << "Unsupported output mode ";
132 
133  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
134 }
135 
136 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
137 {
138  destinationDir_ = destinationDir;
139 
140  if (closefd()) finishFileWrite(ls);
141 
142  fileName_ = name;
143 
144  if (!writtenJSDs_) {
145  writeJsds();
146 /* std::stringstream ss;
147  ss << destinationDir_ << "/jsd";
148  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
149 
150  std::string rawJSDName = ss.str()+"/rawData.jsd";
151  std::string eolJSDName = ss.str()+"/EoLS.jsd";
152  std::string eorJSDName = ss.str()+"/EoR.jsd";
153 
154  fileMon_->setDefPath(rawJSDName);
155  lumiMon_->setDefPath(eolJSDName);
156  runMon_->setDefPath(eorJSDName);
157 
158  struct stat fstat;
159  if (stat (rawJSDName.c_str(), &fstat) != 0) {
160  std::string content;
161  JSONSerializer::serialize(&rawJsonDef_,content);
162  FileIO::writeStringToFile(rawJSDName, content);
163  }
164 
165  if (stat (eolJSDName.c_str(), &fstat) != 0) {
166  std::string content;
167  JSONSerializer::serialize(&eolJsonDef_,content);
168  FileIO::writeStringToFile(eolJSDName, content);
169  }
170 
171  if (stat (eorJSDName.c_str(), &fstat) != 0) {
172  std::string content;
173  JSONSerializer::serialize(&eorJsonDef_,content);
174  FileIO::writeStringToFile(eorJSDName, content);
175  }
176 */
177  writtenJSDs_=true;
178 
179  }
180 
181  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
182  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
183  if(outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
184  throw cms::Exception("RawEventFileWriterForBU","initialize")
185  << "Error opening FED Raw Data event output file: " << name
186  << ": " << strerror(errno) << "\n";
187  }
188 
190  perFileSize_.value() = 0;
191 
192 
193  adlera_ = 1;
194  adlerb_ = 0;
195 }
196 
198 {
199 
200  std::stringstream ss;
201  ss << destinationDir_ << "/jsd";
202  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
203 
204  std::string rawJSDName = ss.str()+"/rawData.jsd";
205  std::string eolJSDName = ss.str()+"/EoLS.jsd";
206  std::string eorJSDName = ss.str()+"/EoR.jsd";
207 
208  fileMon_->setDefPath(rawJSDName);
209  lumiMon_->setDefPath(eolJSDName);
210  runMon_->setDefPath(eorJSDName);
211 
212  struct stat fstat;
213  if (stat (rawJSDName.c_str(), &fstat) != 0) {
216  FileIO::writeStringToFile(rawJSDName, content);
217  }
218 
219  if (stat (eolJSDName.c_str(), &fstat) != 0) {
222  FileIO::writeStringToFile(eolJSDName, content);
223  }
224 
225  if (stat (eorJSDName.c_str(), &fstat) != 0) {
228  FileIO::writeStringToFile(eorJSDName, content);
229  }
230 }
231 
233 {
234 
235  //move raw file from open to run directory
236  rename(fileName_.c_str(),(destinationDir_+fileName_.substr(fileName_.rfind("/"))).c_str());
237 
238  //create equivalent JSON file
239  std::stringstream ss;
240  //TODO:fix this to use DaqDirector convention and better extension replace
242  std::string path = source.replace_extension(".jsn").string();
243 
244  fileMon_->snap(ls);
245  fileMon_->outputFullJSON(path, ls, false);
247 
248  //move the json file from open
249  rename(path.c_str(),(destinationDir_+path.substr(path.rfind("/"))).c_str());
250  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
255  //update open lumi value when first file is completed
256  lumiOpen_ = ls;
257 
258  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
259  << " with perFileEventCount = " << perFileEventCount_.value()
260  << " and size " << perFileSize_.value();
261 
262 }
263 
264 
266 {
267  if (closefd()) finishFileWrite(ls);
268  lumiMon_->snap(ls);
269 
270  std::ostringstream ostr;
271 
273 
274  ostr << destinationDir_ << "/"<< runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS" << ".jsn";
275  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
276  //closefd();
277 
278  std::string path = ostr.str();
279  lumiMon_->outputFullJSON(path, ls);
281 
284  perRunLumiCount_.value() += 1;
286 
287  perLumiEventCount_ = 0;
288  perLumiFileCount_ = 0;
290  perLumiSize_ = 0;
291  lumiClosed_ = ls;
292 }
293 
295 {
296  // create EoR file
297  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
298  runMon_->snap(0);
299  runMon_->outputFullJSON(path, 0);
300 }
301 
302 // runs on SIGINT and terminates the process
304 {
306  printf("Caught signal %d. Writing EOR file!\n",s);
307  if (destinationDir_.size() > 0)
308  {
309  // create EoR file
311  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
312  runMon_->snap(0);
313  runMon_->outputFullJSON(path, 0);
314  }
315  _exit(0);
316 }
317 
318 //TODO:get from DaqDirector !
320 {
321  //dirty hack: extract run number from destination directory
322  std::string::size_type pos = destinationDir.find("run");
323  std::string run = destinationDir.substr(pos+3);
324  run_=atoi(run.c_str());
325  std::stringstream ss;
326  ss << "run" << std::setfill('0') << std::setw(6) << run_;
327  runPrefix_ = ss.str();
328 }
void addLegendItem(std::string const &name, std::string const &type, std::string const &operation)
void initialize(std::string const &destinationDir, std::string const &name, int ls)
void setDefPath(std::string const &dpath)
Definition: FastMonitor.h:30
bool outputFullJSON(std::string const &path, unsigned int lumi, bool log=true)
Definition: FastMonitor.cc:249
DataPointDefinition rawJsonDef_
uint32 size() const
void doOutputEventFragment(unsigned char *dataPtr, unsigned long dataSize)
#define NULL
Definition: scimark2.h:8
uint16_t size_type
void registerGlobalMonitorable(JsonMonitorable *newMonitorable, bool NAifZeroUpdates, unsigned int *nBins=nullptr)
Definition: FastMonitor.cc:62
DataPointDefinition eorJsonDef_
uint8 * startAddress() const
tuple path
else: Piece not in the list, fine.
virtual void setName(std::string name)
static void staticHandler(int s)
RawEventFileWriterForBU(edm::ParameterSet const &ps)
void commit(std::vector< unsigned int > *streamLumisPtr)
Definition: FastMonitor.cc:109
#define SUM(A, B)
std::pair< Binary, Binary > serialize(const T &payload, bool packingOnly=false)
Definition: Serialization.h:55
static RawEventFileWriterForBU * instance
void snap(unsigned int ls)
Definition: FastMonitor.cc:190
void setDefaultGroup(std::string const &group)
void makeRunPrefix(std::string const &destinationDir)
DataPointDefinition eolJsonDef_
static std::string const source
Definition: EdmProvDump.cc:42
void doOutputEvent(FRDEventMsgView const &msg)
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:268