CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoEventOutputModuleForFU.h
Go to the documentation of this file.
1 #ifndef IOPool_Streamer_RecoEventOutputModuleForFU_h
2 #define IOPool_Streamer_RecoEventOutputModuleForFU_h
3 
8 
9 #include <sstream>
10 #include <iomanip>
11 #include "boost/filesystem.hpp"
12 
13 #include "../interface/JsonMonitorable.h"
14 #include "../interface/FastMonitor.h"
15 #include "../interface/JSONSerializer.h"
16 
17 #include "FastMonitoringService.h"
18 
19 namespace evf {
20  template<typename Consumer>
22 
31  public:
32  explicit RecoEventOutputModuleForFU(edm::ParameterSet const& ps);
34  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
35 
36  private:
37  virtual void start() const;
38  virtual void stop() const;
39  virtual void doOutputHeader(InitMsgBuilder const& init_message) const;
40  virtual void doOutputEvent(EventMsgBuilder const& msg) const;
41  // virtual void beginRun(edm::RunPrincipal const&);
44 
46  // find run dir
47  boost::filesystem::path runDirectory(
48  edm::Service<evf::EvFDaqDirector>()->findCurrentRunDir());
49  smpath_ = runDirectory.string();
50  edm::LogInfo("RecoEventOutputModuleForFU") << "Writing .dat files to "
51  << smpath_;
52  // create open dir if not already there
53  boost::filesystem::path openPath = runDirectory;
54  openPath /= "open";
55  // do these dirs need to be created?
56  bool foundOpenDir = false;
57  if (boost::filesystem::is_directory(openPath))
58  foundOpenDir = true;
59  if (!foundOpenDir) {
60  std::cout << "<open> FU dir not found. Creating..."
61  << std::endl;
62  boost::filesystem::create_directories(openPath);
63  }
64  }
65 
66  private:
67  std::auto_ptr<Consumer> c_;
75  mutable IntJ accepted_;
77  boost::shared_ptr<FastMonitor> jsonMonitor_;
79 
80 
81  }; //end-of-class-def
82 
83  template<typename Consumer>
85  edm::StreamerOutputModuleBase(ps),
86  c_(new Consumer(ps)),
87  stream_label_(ps.getParameter<std::string>("@module_label")),
88  baseDir_(ps.getUntrackedParameter<std::string>("baseDir","")),
89  processed_(0),
90  accepted_(0),
91  filelist_("")
92  {
94 
97 
98  processed_.setName("Processed");
99  accepted_.setName("Accepted");
100  filelist_.setName("Filelist");
101  vector<JsonMonitorable*> monParams;
102  monParams.push_back(&processed_);
103  monParams.push_back(&accepted_);
104  monParams.push_back(&filelist_);
105 
106  jsonMonitor_.reset(new FastMonitor(monParams, jsonDefPath_));
107  }
108 
109  template<typename Consumer>
111 
112  template<typename Consumer>
113  void
115  {
116  std::cout << "RecoEventOutputModuleForFU: start() method " << std::endl;
117 
118  const std::string initFileName = edm::Service<evf::EvFDaqDirector>()->getInitFilePath(stream_label_);
119 
120  std::cout << "RecoEventOutputModuleForFU, initializing streams. init stream: "
121  << initFileName << std::endl;
122 
123  c_->setInitMessageFile(initFileName);
124  c_->start();
125  }
126 
127  template<typename Consumer>
128  void
130  c_->stop();
131  }
132 
133  template<typename Consumer>
134  void
136  c_->doOutputHeader(init_message);
137  }
138 
139 //______________________________________________________________________________
140  template<typename Consumer>
141  void
143  accepted_.value()++;
144  c_->doOutputEvent(msg); // You can't use msg in RecoEventOutputModuleForFU after this point
145  }
146 
147  template<typename Consumer>
148  void
152  Consumer::fillDescription(desc);
153  desc.addUntracked<std::string>("baseDir", "")
154  ->setComment("Top level output directory");
155  descriptions.add("streamerOutput", desc);
156  }
157 
158 // template<typename Consumer>
159 // void RecoEventOutputModuleForFU<Consumer>::beginRun(edm::RunPrincipal const &run){
160 
161 
162 // }
163 
164  template<typename Consumer>
166  std::cout << "RecoEventOutputModuleForFU : begin lumi " << std::endl;
167  openDatFilePath_ = edm::Service<evf::EvFDaqDirector>()->getOpenDatFilePath(ls.luminosityBlock(),stream_label_);
168  c_->setOutputFile(openDatFilePath_.string());
169  filelist_ = openDatFilePath_.filename().string();
170  }
171 
172  template<typename Consumer>
174  std::cout << "RecoEventOutputModuleForFU : end lumi " << std::endl;
175  c_->closeOutputFile();
176  processed_.value() = fms_->getEventsProcessedForLumi(ls.luminosityBlock());
177  if(processed_.value()!=0){
178  int b;
179  // move dat file to one level up - this is VERRRRRY inefficient, come up with a smarter idea
180 
181  FILE *des = edm::Service<evf::EvFDaqDirector>()->maybeCreateAndLockFileHeadForStream(ls.luminosityBlock(),stream_label_);
182  FILE *src = fopen(openDatFilePath_.string().c_str(),"r");
183  if(des != 0 && src !=0){
184  while((b=fgetc(src))!= EOF){
185  fputc((unsigned char)b,des);
186  }
187  }
188 
189  edm::Service<evf::EvFDaqDirector>()->unlockAndCloseMergeStream();
190  fclose(src);
191  }
192  //remove file
193  remove(openDatFilePath_.string().c_str());
194 
195  // output jsn file
196  if(processed_.value()!=0){
197  jsonMonitor_->snap(false, "");
198  const std::string outputJsonNameStream =
199  edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(ls.luminosityBlock(),stream_label_);
200  jsonMonitor_->outputFullHistoDataPoint(outputJsonNameStream);
201  }
202 
203  // reset monitoring params
204  accepted_.value() = 0;
205  filelist_ = "";
206  }
207 
208 } // end of namespace-edm
209 
210 #endif
static void fillDescription(ParameterSetDescription &desc)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
virtual void doOutputEvent(EventMsgBuilder const &msg) const
std::string getOutputDefPath() const
virtual void doOutputHeader(InitMsgBuilder const &init_message) const
virtual void beginLuminosityBlock(edm::LuminosityBlockPrincipal const &, edm::ModuleCallingContext const *)
RecoEventOutputModuleForFU(edm::ParameterSet const &ps)
LuminosityBlockNumber_t luminosityBlock() const
boost::shared_ptr< FastMonitor > jsonMonitor_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double b
Definition: hdecay.h:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
tuple cout
Definition: gather_cfg.py:121
virtual void endLuminosityBlock(edm::LuminosityBlockPrincipal const &, edm::ModuleCallingContext const *)