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 
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <signal.h>
15 
16 
18 
20  printf("Caught signal %d. Writing EOR file!\n",s);
21  if (destinationDir_.size() > 0)
22  {
23  // CREATE EOR file
24  string path = destinationDir_ + "/" + "EoR.jsd";
25  string output = "EOR";
26  FileIO::writeStringToFile(path, output);
27  //dirty hack: extract run number from destination directory
28  std::string::size_type pos = destinationDir_.find("run");
29  std::string run = destinationDir_.substr(pos+3);
30  path=destinationDir_ + "/" + "EoR_" + run + ".jsn";
31  FileIO::writeStringToFile(path, output);
32  }
33  _exit(0);
34 }
35 
37  jsonDefLocation_(ps.getUntrackedParameter<string>("jsonDefLocation","")),
38  // default to .5ms sleep per event
39  microSleep_(ps.getUntrackedParameter<int>("microSleep", 0))
40 {
41  // initialize(ps.getUntrackedParameter<std::string>("fileName", "testFRDfile.dat"));
43  // set names of the variables to be matched with JSON Definition
44  perLumiEventCount_.setName("NEvents");
45 
46  // create a vector of all monitorable parameters to be passed to the monitor
47  vector<JsonMonitorable*> lumiMonParams;
48  lumiMonParams.push_back(&perLumiEventCount_);
49 
50  // create a DataPointMonitor using vector of monitorable parameters and a path to a JSON Definition file
51  lumiMon_ = new DataPointMonitor(lumiMonParams, jsonDefLocation_);
52 
53 
55  perFileEventCount_.setName("NEvents");
56 
57  // create a vector of all monitorable parameters to be passed to the monitor
58  vector<JsonMonitorable*> fileMonParams;
59  fileMonParams.push_back(&perFileEventCount_);
60 
61  perFileMon_ = new DataPointMonitor(fileMonParams, jsonDefLocation_);
62  instance = this;
63 
64  // SIGINT Handler
65  struct sigaction sigIntHandler;
66  sigIntHandler.sa_handler = RawEventFileWriterForBU::staticHandler;
67  sigemptyset(&sigIntHandler.sa_mask);
68  sigIntHandler.sa_flags = 0;
69  sigaction(SIGINT, &sigIntHandler, NULL);
70 
71 }
72 
74 {
75  // initialize(fileName);
76 
77 }
78 
80 {
81  // ost_->close();
82  if (lumiMon_ != 0)
83  delete lumiMon_;
84  if (perFileMon_ != 0)
85  delete perFileMon_;
86 }
87 
89 {
90  // ost_->write((const char*) msg.startAddress(), msg.size());
91  // if (ost_->fail()) {
92  ssize_t retval = write(outfd_,(void*)msg.startAddress(), msg.size());
93  if((unsigned)retval!= msg.size()){
94  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
95  << "Error writing FED Raw Data event data to "
96  << fileName_ << ". Possibly the output disk "
97  << "is full?" << std::endl;
98  }
99 
100  // throttle event output
101  usleep(microSleep_);
102 
105 
106  // ost_->flush();
107  // if (ost_->fail()) {
108  // throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
109  // << "Error writing FED Raw Data event data to "
110  // << fileName_ << ". Possibly the output disk "
111  // << "is full?" << std::endl;
112  // }
113 
114  // cms::Adler32((const char*) msg.startAddress(), msg.size(), adlera_, adlerb_);
115 }
116 
118 {
119  ost_->flush();
120  if (ost_->fail()) {
121  throw cms::Exception("RawEventFileWriterForBU", "doOutputEvent")
122  << "Error writing FED Raw Data event data to "
123  << fileName_ << ". Possibly the output disk "
124  << "is full?" << std::endl;
125  }
126 }
127 
129  unsigned long dataSize)
130 {
131  ost_->write((const char*) dataPtr, dataSize);
132  if (ost_->fail()) {
133  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
134  << "Error writing FED Raw Data event data to "
135  << fileName_ << ". Possibly the output disk "
136  << "is full?" << std::endl;
137  }
138 
139  ost_->flush();
140  if (ost_->fail()) {
141  throw cms::Exception("RawEventFileWriterForBU", "doOutputEventFragment")
142  << "Error writing FED Raw Data event data to "
143  << fileName_ << ". Possibly the output disk "
144  << "is full?" << std::endl;
145  }
146 
147  cms::Adler32((const char*) dataPtr, dataSize, adlera_, adlerb_);
148 }
149 
150 void RawEventFileWriterForBU::initialize(std::string const& destinationDir, std::string const& name, int ls)
151 {
152  std::string oldFileName = fileName_;
153  fileName_ = name;
154  destinationDir_ = destinationDir;
155  if(outfd_!=0){
156  close(outfd_);
157  outfd_=0;
158  }
159  outfd_ = open(fileName_.c_str(), O_WRONLY | O_CREAT, S_IRWXU);
160  if(outfd_ <= 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
161  throw cms::Exception("RawEventFileWriterForBU","initialize")
162  << "Error opening FED Raw Data event output file: " << name
163  << ": " << strerror(errno) << "\n";
164  }
165  ost_.reset(new std::ofstream(name.c_str(), std::ios_base::binary | std::ios_base::out));
166 
167  //move old file to done directory
168  if (!oldFileName.empty()) {
169  //rename(oldFileName.c_str(),destinationDir_.c_str());
170 
171  DataPoint dp;
172  perFileMon_->snap(dp);
173  string output;
174  JSONSerializer::serialize(&dp, output);
175  std::stringstream ss;
176  ss << destinationDir_ << "/" << oldFileName.substr(oldFileName.rfind("/") + 1, oldFileName.size() - oldFileName.rfind("/") - 5) << ".jsn";
177  string path = ss.str();
178  FileIO::writeStringToFile(path, output);
179  //now that the json file is there, move the raw file
180  int fretval = rename(oldFileName.c_str(),(destinationDir_+oldFileName.substr(oldFileName.rfind("/"))).c_str());
181  // if (debug_)
182  edm::LogInfo("RawEventFileWriterForBU") << " tried move " << oldFileName << " to " << destinationDir_
183  << " status " << fretval << " errno " << strerror(errno);
184 
185  edm::LogInfo("RawEventFileWriterForBU") << "Wrote JSON input file: " << path
186  << " with perFileEventCount = " << perFileEventCount_.value();
187 
189 
190  }
191 
192 
193  if (!ost_->is_open()) {
194  throw cms::Exception("RawEventFileWriterForBU","initialize")
195  << "Error opening FED Raw Data event output file: " << name << "\n";
196  }
197 
198  adlera_ = 1;
199  adlerb_ = 0;
200 }
201 
203 {
204  //writing empty EoLS file (will be filled with information)
205  // create a DataPoint object and take a snapshot of the monitored data into it
206  DataPoint dp;
207  lumiMon_->snap(dp);
208 
209  std::ostringstream ostr;
210  ostr << destinationDir_ << "/EoLS_" << std::setfill('0') << std::setw(4) << ls << ".jsn";
211  int outfd_ = open(ostr.str().c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
212  if(outfd_!=0){close(outfd_); outfd_=0;}
213 
214  // serialize the DataPoint and output it
215  string output;
216  JSONSerializer::serialize(&dp, output);
217 
218  string path = ostr.str();
219  FileIO::writeStringToFile(path, output);
220 
221  perLumiEventCount_ = 0;
222 }
void initialize(std::string const &destinationDir, std::string const &name, int ls)
uint32 size() const
void doOutputEventFragment(unsigned char *dataPtr, unsigned long dataSize)
#define NULL
Definition: scimark2.h:8
uint16_t size_type
uint8 * startAddress() const
std::auto_ptr< std::ofstream > ost_
static void staticHandler(int s)
RawEventFileWriterForBU(edm::ParameterSet const &ps)
tuple out
Definition: dbtoconf.py:99
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
auto dp
Definition: deltaR.h:24
static RawEventFileWriterForBU * instance
Binary serialize(const T &payload, bool packingOnly=false)
Definition: Serialization.h:88
void doOutputEvent(FRDEventMsgView const &msg)
void snap(DataPoint &outputDataPoint)