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 
48  perLumiEventCount_.setName("NEvents");
49  perLumiFileCount_.setName("NFiles");
50  perLumiTotalEventCount_.setName("TotalEvents");
51 
52  lumiMon_ = new FastMonitor(&eolJsonDef_,false);
56  lumiMon_->commit(nullptr);
57 
58 
59  //per-run JSD and FastMonitor
60  eorJsonDef_.setDefaultGroup("legend");
64 
65  perRunEventCount_.setName("NEvents");
66  perRunFileCount_.setName("NFiles");
67  perRunLumiCount_.setName("NLumis");
68 
69  runMon_ = new FastMonitor(&eorJsonDef_,false);
73  runMon_->commit(nullptr);
74 
75  instance = this;
76 
77  // SIGINT Handler
78  struct sigaction sigIntHandler;
79  sigIntHandler.sa_handler = RawEventFileWriterForBU::staticHandler;
80  sigemptyset(&sigIntHandler.sa_mask);
81  sigIntHandler.sa_flags = 0;
82  sigaction(SIGINT, &sigIntHandler, NULL);
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_);
111 
115 
116  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
117 }
118 
119 void RawEventFileWriterForBU::doOutputEventFragment(unsigned char* dataPtr, unsigned long dataSize)
120 {
121 
122  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
123  << "Unsupported output mode ";
124 
125  //cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
126 }
127 
128 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
129 {
130  destinationDir_ = destinationDir;
131 
132  if (closefd()) finishFileWrite(ls);
133 
134  fileName_ = name;
135 
136  if (!writtenJSDs_) {
137  writeJsds();
138 /* std::stringstream ss;
139  ss << destinationDir_ << "/jsd";
140  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
141 
142  std::string rawJSDName = ss.str()+"/rawData.jsd";
143  std::string eolJSDName = ss.str()+"/EoLS.jsd";
144  std::string eorJSDName = ss.str()+"/EoR.jsd";
145 
146  fileMon_->setDefPath(rawJSDName);
147  lumiMon_->setDefPath(eolJSDName);
148  runMon_->setDefPath(eorJSDName);
149 
150  struct stat fstat;
151  if (stat (rawJSDName.c_str(), &fstat) != 0) {
152  std::string content;
153  JSONSerializer::serialize(&rawJsonDef_,content);
154  FileIO::writeStringToFile(rawJSDName, content);
155  }
156 
157  if (stat (eolJSDName.c_str(), &fstat) != 0) {
158  std::string content;
159  JSONSerializer::serialize(&eolJsonDef_,content);
160  FileIO::writeStringToFile(eolJSDName, content);
161  }
162 
163  if (stat (eorJSDName.c_str(), &fstat) != 0) {
164  std::string content;
165  JSONSerializer::serialize(&eorJsonDef_,content);
166  FileIO::writeStringToFile(eorJSDName, content);
167  }
168 */
169  writtenJSDs_=true;
170 
171  }
172 
173  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
174  edm::LogInfo("RawEventFileWriterForBU") << " opened " << fileName_;
175  if(outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
176  throw cms::Exception("RawEventFileWriterForBU","initialize")
177  << "Error opening FED Raw Data event output file: " << name
178  << ": " << strerror(errno) << "\n";
179  }
180 
183 
184 
185  adlera_ = 1;
186  adlerb_ = 0;
187 }
188 
190 {
191 
192  std::stringstream ss;
193  ss << destinationDir_ << "/jsd";
194  mkdir(ss.str().c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
195 
196  std::string rawJSDName = ss.str()+"/rawData.jsd";
197  std::string eolJSDName = ss.str()+"/EoLS.jsd";
198  std::string eorJSDName = ss.str()+"/EoR.jsd";
199 
200  fileMon_->setDefPath(rawJSDName);
201  lumiMon_->setDefPath(eolJSDName);
202  runMon_->setDefPath(eorJSDName);
203 
204  struct stat fstat;
205  if (stat (rawJSDName.c_str(), &fstat) != 0) {
208  FileIO::writeStringToFile(rawJSDName, content);
209  }
210 
211  if (stat (eolJSDName.c_str(), &fstat) != 0) {
214  FileIO::writeStringToFile(eolJSDName, content);
215  }
216 
217  if (stat (eorJSDName.c_str(), &fstat) != 0) {
220  FileIO::writeStringToFile(eorJSDName, content);
221  }
222 }
223 
225 {
226 
227  //move raw file from open to run directory
228  rename(fileName_.c_str(),(destinationDir_+fileName_.substr(fileName_.rfind("/"))).c_str());
229 
230  //create equivalent JSON file
231  std::stringstream ss;
232  //TODO:fix this to use DaqDirector convention and better extension replace
234  std::string path = source.replace_extension(".jsn").string();
235 
236  fileMon_->snap(ls);
237  fileMon_->outputFullJSON(path, ls, false);
239 
240  //move the json file from open
241  rename(path.c_str(),(destinationDir_+path.substr(path.rfind("/"))).c_str());
242 
243  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
244  << " with perFileEventCount = " << perFileEventCount_.value();
245 
246 }
247 
248 
250 {
251  if (closefd()) finishFileWrite(ls);
252  lumiMon_->snap(ls);
253 
254  std::ostringstream ostr;
255 
257 
258  ostr << destinationDir_ << "/"<< runPrefix_ << "_ls" << std::setfill('0') << std::setw(4) << ls << "_EoLS" << ".jsn";
259  //outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
260  //closefd();
261 
262  std::string path = ostr.str();
263  lumiMon_->outputFullJSON(path, ls);
265 
268  perRunLumiCount_.value() += 1;
269 
270  perLumiEventCount_ = 0;
271  perLumiFileCount_ = 0;
273 }
274 
276 {
277  // create EoR file
278  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
279  runMon_->snap(0);
280  runMon_->outputFullJSON(path, 0);
281 }
282 
283 // runs on SIGINT and terminates the process
285 {
286  printf("Caught signal %d. Writing EOR file!\n",s);
287  if (destinationDir_.size() > 0)
288  {
289  // create EoR file
291  std::string path = destinationDir_ + "/" + runPrefix_ + "_ls0000_EoR.jsn";
292  runMon_->snap(0);
293  runMon_->outputFullJSON(path, 0);
294  }
295  _exit(0);
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)
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