CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RawEventSourceFromBU.cc
Go to the documentation of this file.
1 //
3 // RawEventSourceFromBU
4 // -----------------
5 //
6 // EM - data da destinarsi
8 #ifdef RAW_EVENT_SOURCE_FROM_BU_IS_READY
9 
17 
18 #include "FWCore/Sources/interface/ExternalInputSource.h"
19 
23 
25 
26 #include "EvFDaqDirector.h"
27 
28 #include <unistd.h>
29 #include <string>
30 #include <vector>
31 #include <fstream>
32 #include <cstring>
33 
36 
37 
38 namespace errorstreamsource{
39  static unsigned int gtpEvmId_ = FEDNumbering::MINTriggerGTPFEDID;
40  //static unsigned int gtpeId_ = FEDNumbering::MINTriggerEGTPFEDID; // unused
41 }
42 
43 
44 class RawEventSourceFromBU : public edm::ExternalInputSource
45 {
46 public:
47  // construction/destruction
48  RawEventSourceFromBU(edm::ParameterSet const& pset,
49  edm::InputSourceDescription const& desc);
50  virtual ~RawEventSourceFromBU();
51 
52 private:
53  // member functions
54  void setRunAndEventInfo();
55  bool produce(edm::Event& e);
56 
57  void beginJob();
58  void beginRun(edm::Run& r){
59  std::cout << "RawEventSourceFromBU: begin run " << std::endl;
60  // openFile();
61  }
62  void endRun(edm::Run& r) {;}
63  void beginLuminosityBlock(edm::LuminosityBlock& lb) {;}
64  void endLuminosityBlock(edm::LuminosityBlock& lb) {;}
65 
66  bool openFile();
67 
68 
69 private:
70  // member data
71  std::vector<std::string>::const_iterator itFileName_;
72  std::string baseDir_;
73  std::string runDir_;
74  unsigned int fileLumi_;
75  std::ifstream fin_;
76 };
77 
78 
79 using namespace std;
80 
81 
83 // construction/destruction
85 
86 //______________________________________________________________________________
87 RawEventSourceFromBU::RawEventSourceFromBU(edm::ParameterSet const& pset,
88  edm::InputSourceDescription const& desc)
89  : ExternalInputSource(pset,desc)
90  , fileLumi_(0)
91 {
92  produces<FEDRawDataCollection>();
93 }
94 
95 
96 //______________________________________________________________________________
97 RawEventSourceFromBU::~RawEventSourceFromBU()
98 {
99 
100 }
101 
102 
104 // implementation of member functions
106 
107 //______________________________________________________________________________
109 {
110  std::cout << "RawEventSourceFromBU beginjob " << std::endl;
111  baseDir_ = edm::Service<evf::EvFDaqDirector>()->baseDir();
112  runDir_ = edm::Service<evf::EvFDaqDirector>()->findHighestRunDir();
113  setRunNumber(edm::Service<evf::EvFDaqDirector>()->findHighestRun());
114 }
115 
116 
117 //______________________________________________________________________________
118 void RawEventSourceFromBU::setRunAndEventInfo()
119 {
120  // std::cout << "Set run and event info " << std::endl;
121  uint32_t version(1);
122  uint32_t runNumber(0);
123  uint32_t lumiNumber(1);
124  uint32_t evtNumber(0);
125  bool status;
126  while(!fin_.is_open()){
127  if(!openFile()){
128  setEventNumber(0);
129  return;
130  }
131  }
132 
133  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
134  if (runNumber < 32) {
135  version = runNumber;
136  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
137  }
138  if (version >= 2) {
139  status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
140  }
141  status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
142 
143  if (!status) {
144  fin_.close();
145  fin_.clear();
146  if(fileLumi_>0)
147  edm::Service<evf::EvFDaqDirector>()->removeFile(fileLumi_);
148  return setRunAndEventInfo();
149  }
150  if(runNumber != run()) throw cms::Exception("LogicError")<< "Run number from file - " << runNumber
151  << " does not correspond to the one from directory " << run()
152  << "\n";
153  if(lumiNumber != (fileLumi_)) throw cms::Exception("LogicError")<< "Ls number from readin file - " << lumiNumber
154  << " does not correspond to the one from directory " << fileLumi_-1
155  << "\n";
156  setLuminosityBlockNumber_t(lumiNumber);
157  setEventNumber(evtNumber);
158 }
159 
160 
161 //______________________________________________________________________________
162 bool RawEventSourceFromBU::produce(edm::Event& e)
163 {
164  // std::cout << "produce " << std::endl;
165  unsigned int totalEventSize = 0;
166  if (!fin_.is_open()) return false;
167 
168  auto_ptr<FEDRawDataCollection> result(new FEDRawDataCollection());
169 
170  uint32_t fedSize[1024];
171  fin_.read((char*)fedSize,1024*sizeof(uint32_t));
172  for (unsigned int i=0;i<1024;i++) {
173  // std::cout << "fedsize " << i << " " << fedSize[i] << std::endl;
174  totalEventSize += fedSize[i];
175  }
176  unsigned int gtpevmsize = fedSize[errorstreamsource::gtpEvmId_];
177  if(gtpevmsize>0)
178  evf::evtn::evm_board_setformat(gtpevmsize);
179  // std::cout << "total event size " << totalEventSize << std::endl;
180  char *event = new char[totalEventSize];
181  fin_.read(event,totalEventSize);
182  while(totalEventSize>0) {
183  totalEventSize -= 8;
184  fedt_t *fedt = (fedt_t*)(event+totalEventSize);
185  unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
186  fedsize *= 8; // fed size in bytes
187  // std::cout << "fedsize = " << fedsize << std::endl;
188  totalEventSize -= (fedsize - 8);
189  fedh_t *fedh = (fedh_t *)(event+totalEventSize);
190  unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
192  unsigned int gpsl = evf::evtn::getgpslow((unsigned char*)fedh);
193  unsigned int gpsh = evf::evtn::getgpshigh((unsigned char*)fedh);
194  edm::TimeValue_t time = gpsh;
195  time = (time << 32) + gpsl;
196  setTime(time);
197  }
198  FEDRawData& fedData=result->FEDData(soid);
199  fedData.resize(fedsize);
200  memcpy(fedData.data(),event+totalEventSize,fedsize);
201  // std::cout << "now totalEventSize = " << totalEventSize << std::endl;
202  }
203  e.put(result);
204  delete[] event;
205  return true;
206 }
207 
208 
209 //______________________________________________________________________________
210 bool RawEventSourceFromBU::openFile()
211 {
212  // get next file to open
213  int lockstate = 0;
214 
215 
216  if(lockstate < 0) return false;
217  // potentially, next file is for reading so let's first check and get rid of the previous used file
218  if (fin_.is_open())
219  {
220  fin_.close();
221  fin_.clear();
222  // if previous file was processed ok ask the director to delete it
223  if(fileLumi_>0)
224  edm::Service<evf::EvFDaqDirector>()->removeFile(fileLumi_);
225  }
226  // check if someone is working on the next ls, in that case, bump the ls and try again - if successful will return the right number
227  unsigned int nextLs = fileLumi_+1;
228  lockstate=edm::Service<evf::EvFDaqDirector>()->updateFuLock(nextLs);
229  fileLumi_ = nextLs;
230 
231  //check the lock is still healthy
232  if(lockstate < 0) return false;
233 
234  // check if we are not hitting the current ls file being written by BU
235  while((lockstate=edm::Service<evf::EvFDaqDirector>()->readBuLock())>=(int)fileLumi_)
236  {
237  ::sleep(1);
238  std::cout << "waiting for file to be released by bu for ls " << fileLumi_ << std::endl;
239  }
240 
241  // get the correct filename from the director
242  std::string filename = edm::Service<evf::EvFDaqDirector>()->getFileForLumi(fileLumi_+1);
243  // std::cout << " here " << fileLumi_ << std::endl;
244 
245  fin_.open(filename);
246  // fileLumi_++;
247  // std::cout << " here " << fileLumi_ << std::endl;
248  std::cout << " is open " << fin_.is_open() << std::endl;
249  return true;
250 }
251 
252 
254 // define this class as an input source
256 DEFINE_FWK_INPUT_SOURCE(RawEventSourceFromBU);
257 
258 #endif
int i
Definition: DBlmapReader.cc:9
unsigned int getgpshigh(const unsigned char *)
unsigned int sourceid
Definition: fed_header.h:32
void beginJob()
Definition: Breakpoints.cc:15
void evm_board_setformat(size_t size)
#define DEFINE_FWK_INPUT_SOURCE(type)
void resize(size_t newsize)
Definition: FEDRawData.cc:32
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
tuple result
Definition: query.py:137
#define FED_EVSZ_EXTRACT(a)
Definition: fed_trailer.h:36
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
unsigned long long TimeValue_t
Definition: Timestamp.h:28
unsigned int eventsize
Definition: fed_trailer.h:33
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:121
constexpr unsigned int gtpEvmId_
tuple status
Definition: ntuplemaker.py:245
unsigned int getgpslow(const unsigned char *)
Definition: Run.h:41
#define FED_SOID_EXTRACT(a)
Definition: fed_header.h:53