CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TestConsumer.cc
Go to the documentation of this file.
1 
5 
6 #include <string>
7 #include <cstring>
8 #include <fstream>
9 #include <sstream>
10 
11 using namespace edm;
12 
13 namespace edmtest
14 {
15  typedef boost::shared_ptr<std::ofstream> OutPtr;
16  typedef std::vector<char> SaveArea;
17 
18  std::string makeFileName(const std::string& base, int num)
19  {
20  std::ostringstream ost;
21  ost << base << num << ".dat";
22  return ost.str();
23  }
24 
25  OutPtr makeFile(const std::string name,int num)
26  {
27  OutPtr p(new std::ofstream(makeFileName(name,num).c_str(),
28  std::ios_base::binary | std::ios_base::out));
29 
30  if(!(*p))
31  {
32  throw edm::Exception(errors::Configuration,"TestConsumer")
33  << "cannot open file " << name;
34  }
35 
36  return p;
37  }
38 
39  struct Worker
40  {
41  Worker(const std::string& s, int m);
42 
43  std::string filename_;
44  int file_num_;
45  int cnt_;
46  int max_;
49 
50  void checkCount();
51  void saveReg(void* buf, int len);
52  void writeReg();
53  };
54 
55  Worker::Worker(const std::string& s,int m):
56  filename_(s),
57  file_num_(),
58  cnt_(0),
59  max_(m),
60  ost_(makeFile(filename_,file_num_))
61  {
62  }
63 
65  {
66  if(cnt_!=0 && (cnt_%max_) == 0)
67  {
68  ++file_num_;
70  writeReg();
71  }
72  ++cnt_;
73 
74  }
75 
77  {
78  if(!reg_.empty())
79  {
80  int len = reg_.size();
81  ost_->write((const char*)(&len),sizeof(int));
82  ost_->write((const char*)&reg_[0],len);
83  }
84  }
85 
86  void Worker::saveReg(void* buf, int len)
87  {
88  reg_.resize(len);
89  memcpy(&reg_[0],buf,len);
90  }
91 
92 
93  // ----------------------------------
94 
96  edm::EventBuffer* buf):
97  worker_(new Worker(ps.getParameter<std::string>("fileName"),
98  ps.getUntrackedParameter<int>("numPerFile",1<<31))),
99  bufs_(buf)
100  {
101  // first write out all the product registry data into the front
102  // of the output file (in text format)
103  }
104 
106  {
107  }
108 
110  {
111  worker_->checkCount();
112 
114 
115  int sz = cb.size();
116  worker_->ost_->write((const char*)(&sz),sizeof(int));
117  worker_->ost_->write((const char*)cb.buffer(),sz);
118 
119  }
120 
122  {
124  pb.commit();
125  }
126 
127  void TestConsumer::sendRegistry(void* buf, int len)
128  {
129  worker_->saveReg(buf,len);
130  worker_->writeReg();
131  }
132 }
std::string makeFileName(const std::string &base, int num)
Definition: TestConsumer.cc:18
tuple base
Main Program
Definition: newFWLiteAna.py:92
edm::EventBuffer * bufs_
Definition: TestConsumer.h:27
TestConsumer(edm::ParameterSet const &ps, edm::EventBuffer *buf)
Definition: TestConsumer.cc:95
boost::shared_ptr< Worker > worker_
Definition: TestConsumer.h:26
boost::shared_ptr< std::ofstream > OutPtr
Definition: TestConsumer.cc:15
std::vector< char > SaveArea
Definition: TestConsumer.cc:16
OutPtr makeFile(const std::string name, int num)
Definition: TestConsumer.cc:25
tuple out
Definition: dbtoconf.py:99
void sendRegistry(void *buf, int len)
long long int num
Definition: procUtils.cc:71
void saveReg(void *buf, int len)
Definition: TestConsumer.cc:86
Worker(ModuleDescription const &iMD, WorkerParams const &iWP)
Definition: Worker.cc:44
std::string filename_
Definition: TestConsumer.cc:43