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 
38  fileMon_ = new FastMonitor(&rawJsonDef_,false);
40  fileMon_->commit(nullptr);
41 
42  //per-lumi JSD and FastMonitor
43  eolJsonDef_.setDefaultGroup("legend");
46  eolJsonDef_.addLegendItem("TotalEvents","integer",DataPointDefinition::SUM);
47  eolJsonDef_.addLegendItem("NLostEvents","integer",DataPointDefinition::SUM);
48 
49  perLumiEventCount_.setName("NEvents");
50  perLumiFileCount_.setName("NFiles");
51  perLumiTotalEventCount_.setName("TotalEvents");
52  perLumiLostEventCount_.setName("NLostEvents");
53 
54  lumiMon_ = new FastMonitor(&eolJsonDef_,false);
59  lumiMon_->commit(nullptr);
60 
61 
62  //per-run JSD and FastMonitor
63  eorJsonDef_.setDefaultGroup("legend");
68 
69  perRunEventCount_.setName("NEvents");
70  perRunFileCount_.setName("NFiles");
71  perRunLumiCount_.setName("NLumis");
72  perRunLastLumi_.setName("LastLumi");
73 
74  runMon_ = new FastMonitor(&eorJsonDef_,false);
79  runMon_->commit(nullptr);
80 
81  instance = this;
82 
83  // SIGINT Handler
84  struct sigaction sigIntHandler;
85  sigIntHandler.sa_handler = RawEventFileWriterForBU::staticHandler;
86  sigemptyset(&sigIntHandler.sa_mask);
87  sigIntHandler.sa_flags = 0;
88  sigaction(SIGINT, &sigIntHandler, NULL);
89 
90 }
91 
93 {
94 
95 }
96 
98 {
99  delete fileMon_;
100  delete lumiMon_;
101  delete runMon_;
102 }
103 
105 {
106  ssize_t retval = write(outfd_,(void*)msg.startAddress(), msg.size());
107 
108  if((unsigned)retval!= msg.size()){
109  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
110  << "Error writing FED Raw Data event data to "
111  << fileName_ << ". Possibly the output disk "
112  << "is full?" << std::endl;
113  }
114 
115  // throttle event output
116  usleep(microSleep_);
118 
119  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
120 }
121 
122 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize)
123 {
124 
125  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
126  << "Unsupported output mode ";
127 
128  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
129 }
130 
131 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
132 {
133  destinationDir_ = destinationDir;
134 
135  if (closefd()) finishFileWrite(ls);
136 
137  fileName_ = name;
138 
139  if (!writtenJSDs_) {
140  writeJsds();
141 /* std::stringstream ss;
142  ss << destinationDir_ << "/jsd";
143  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
144 
145  std::string rawJSDName = ss.str()+"/rawData.jsd";
146  std::string eolJSDName = ss.str()+"/EoLS.jsd";
147  std::string eorJSDName = ss.str()+"/EoR.jsd";
148 
149  fileMon_->setDefPath(rawJSDName);
150  lumiMon_->setDefPath(eolJSDName);
151  runMon_->setDefPath(eorJSDName);
152 
153  struct stat fstat;
154  if (stat (rawJSDName.c_str(), &fstat) != 0) {
155  std::string content;
156  JSONSerializer::serialize(&rawJsonDef_,content);
157  FileIO::writeStringToFile(rawJSDName, content);
158  }
159 
160  if (stat (eolJSDName.c_str(), &fstat) != 0) {
161  std::string content;
162  JSONSerializer::serialize(&eolJsonDef_,content);
163  FileIO::writeStringToFile(eolJSDName, content);
164  }
165 
166  if (stat (eorJSDName.c_str(), &fstat) != 0) {
167  std::string content;
168  JSONSerializer::serialize(&eorJsonDef_,content);
169  FileIO::writeStringToFile(eorJSDName, content);
170  }
171 */
172  writtenJSDs_=true;
173 
174  }
175 
176  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
177  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
178  if(outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
179  throw cms::Exception("RawEventFileWriterForBU","initialize")
180  << "Error opening FED Raw Data event output file: " << name
181  << ": " << strerror(errno) << "\n";
182  }
183 
185 
186 
187  adlera_ = 1;
188  adlerb_ = 0;
189 }
190 
192 {
193 
194  std::stringstream ss;
195  ss << destinationDir_ << "/jsd";
196  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
197 
198  std::string rawJSDName = ss.str()+"/rawData.jsd";
199  std::string eolJSDName = ss.str()+"/EoLS.jsd";
200  std::string eorJSDName = ss.str()+"/EoR.jsd";
201 
202  fileMon_->setDefPath(rawJSDName);
203  lumiMon_->setDefPath(eolJSDName);
204  runMon_->setDefPath(eorJSDName);
205 
206  struct stat fstat;
207  if (stat (rawJSDName.c_str(), &fstat) != 0) {
210  FileIO::writeStringToFile(rawJSDName, content);
211  }
212 
213  if (stat (eolJSDName.c_str(), &fstat) != 0) {
216  FileIO::writeStringToFile(eolJSDName, content);
217  }
218 
219  if (stat (eorJSDName.c_str(), &fstat) != 0) {
222  FileIO::writeStringToFile(eorJSDName, content);
223  }
224 }
225 
227 {
228 
229  //move raw file from open to run directory
230  rename(fileName_.c_str(),(destinationDir_+fileName_.substr(fileName_.rfind("/"))).c_str());
231 
232  //create equivalent JSON file
233  std::stringstream ss;
234  //TODO:fix this to use DaqDirector convention and better extension replace
236  std::string path = source.replace_extension(".jsn").string();
237 
238  fileMon_->snap(ls);
239  fileMon_->outputFullJSON(path, ls, false);
241 
242  //move the json file from open
243  rename(path.c_str(),(destinationDir_+path.substr(path.rfind("/"))).c_str());
244  //there is a small chance that script gets interrupted while this isn't consistent (non-atomic)
248  //update open lumi value when first file is completed
249  lumiOpen_ = ls;
250 
251  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
252  << " with perFileEventCount = " << perFileEventCount_.value();
253 
254 }
255 
256 
258 {
259  if (closefd()) finishFileWrite(ls);
260  lumiMon_->snap(ls);
261 
262  std::ostringstream ostr;
263 
265 
266  ostr << destinationDir_ << "/"<< runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS" << ".jsn";
267  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
268  //closefd();
269 
270  std::string path = ostr.str();
271  lumiMon_->outputFullJSON(path, ls);
273 
276  perRunLumiCount_.value() += 1;
278 
279  perLumiEventCount_ = 0;
280  perLumiFileCount_ = 0;
282  lumiClosed_ = ls;
283 }
284 
286 {
287  // create EoR file
288  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
289  runMon_->snap(0);
290  runMon_->outputFullJSON(path, 0);
291 }
292 
293 // runs on SIGINT and terminates the process
295 {
297  printf("Caught signal %d. Writing EOR file!\n",s);
298  if (destinationDir_.size() > 0)
299  {
300  // create EoR file
302  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
303  runMon_->snap(0);
304  runMon_->outputFullJSON(path, 0);
305  }
306  _exit(0);
307 }
308 
309 //TODO:get from DaqDirector !
311 {
312  //dirty hack: extract run number from destination directory
313  std::string::size_type pos = destinationDir.find("run");
314  std::string run = destinationDir.substr(pos+3);
315  run_=atoi(run.c_str());
316  std::stringstream ss;
317  ss << "run" << std::setfill('0') << std::setw(6) << run_;
318  runPrefix_ = ss.str();
319 }
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:92
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:43
void doOutputEvent(FRDEventMsgView const &msg)
void discardCollected(unsigned int forLumi)
Definition: FastMonitor.cc:268