CMS 3D CMS Logo

GtPsbTextToDigi.cc
Go to the documentation of this file.
1 
2 #include "GtPsbTextToDigi.h"
3 // general
6 #include <iomanip>
7 #include <iostream>
8 // gct
13 //#include "DataFormats/L1GlobalCaloTrigger/interface/L1GctJetCounts.h"
14 
16  : m_fileEventOffset(
17  iConfig.getUntrackedParameter<int>("FileEventOffset", 0)),
18  m_textFileName(iConfig.getParameter<std::string>("TextFileName")),
19  m_nevt(0) {
20  // Produces collections
21  produces<L1GctEmCandCollection>("isoEm");
22  produces<L1GctEmCandCollection>("nonIsoEm");
23  produces<L1GctJetCandCollection>("cenJets");
24  produces<L1GctJetCandCollection>("forJets");
25  produces<L1GctJetCandCollection>("tauJets");
26  // produces<L1GctEtTotal>();
27 
28  // Open the input files
29  for (int ifile = 0; ifile < 4; ifile++) {
30  // gct em cand coll: (noiso) 0<->0, 1<->1, (iso) 6<->2, 7<->3
31  int ii = (ifile < 2) ? ifile : ifile + 4;
32  std::stringstream fileStream;
33  fileStream << m_textFileName << ii << ".txt";
34  std::string fileName(fileStream.str());
35  m_file[ifile].open(fileName.c_str(), std::ios::in);
36  if (!m_file[ifile].good()) {
37  throw cms::Exception("GtPsbTextToDigiTextFileOpenError")
38  // LogDebug("GtPsbTextToDigi")
39  << "GtPsbTextToDigi::GtPsbTextToDigi : "
40  << " couldn't open the file "
41  << fileName
42  //<< "...skipping!"
43  << std::endl;
44  }
45  std::hex(m_file[ifile]);
46  }
47 
48  // Initialize bc0 position holder
49  for (int i = 0; i < 4; i++)
50  m_bc0[i] = -1;
51 }
52 
54  // Close the input files
55  for (unsigned i = 0; i < 4; i++) {
56  m_file[i].close();
57  }
58 }
59 
62  std::unique_ptr<L1GctEmCandCollection> gctIsolaEm(
63  new L1GctEmCandCollection());
64  std::unique_ptr<L1GctEmCandCollection> gctNoIsoEm(
65  new L1GctEmCandCollection());
66  std::unique_ptr<L1GctJetCandCollection> gctCenJets(
68  std::unique_ptr<L1GctJetCandCollection> gctForJets(
70  std::unique_ptr<L1GctJetCandCollection> gctTauJets(
72  // std::unique_ptr<L1GctEtTotal> gctEtTotal( new L1GctEtTotal () );
73  for (int i = 0; i < 4; i++) {
74  gctIsolaEm->push_back(L1GctEmCand(0, true));
75  gctNoIsoEm->push_back(L1GctEmCand(0, false));
76  gctCenJets->push_back(L1GctJetCand(0, false, false));
77  gctForJets->push_back(L1GctJetCand(0, false, true));
78  gctTauJets->push_back(L1GctJetCand(0, true, false));
79  // gctEtTotal->push_back(());
80  }
81  iEvent.put(std::move(gctIsolaEm), "isoEm");
82  iEvent.put(std::move(gctNoIsoEm), "nonIsoEm");
83  iEvent.put(std::move(gctCenJets), "cenJets");
84  iEvent.put(std::move(gctForJets), "forJets");
85  iEvent.put(std::move(gctTauJets), "tauJets");
86  // iEvent.put(std::move(gctEtTotal));
87 
88  LogDebug("GtPsbTextToDigi") << "putting empty digi (evt:" << m_nevt << ")\n";
89 }
90 
92  const edm::EventSetup &iSetup) {
93 
94  // specify clock cycle bit sequence 1 0 1 0... or 0 1 0 1...
95  unsigned short cbs[2] = {1, 0};
96 
97  // Skip event if required
98  if (m_nevt < m_fileEventOffset) {
99  putEmptyDigi(iEvent);
100  LogDebug("GtPsbTextToDigi")
101  << "[GtPsbTextToDigi::produce()] skipping event " << m_nevt
102  << std::endl;
103  m_nevt++;
104  return;
105  } else if (m_nevt == 0 && m_fileEventOffset < 0) {
106  // skip first fileEventOffset input events
107  unsigned long int buff;
108  for (int ievt = 0; ievt < abs(m_fileEventOffset); ievt++) {
109  for (int ifile = 0; ifile < 4; ifile++) {
110  for (int cycle = 0; cycle < 2; cycle++) {
111  std::hex(m_file[ifile]);
112  m_file[ifile] >> buff;
113  unsigned tmp = (buff >> 15) & 0x1;
114  if (tmp != cbs[cycle]) {
115  if (m_bc0[ifile] == -1 && cycle == 1 && tmp == 1)
116  m_bc0[ifile] = ievt;
117  else
118  throw cms::Exception("GtPsbTextToDigiTextFileFormatError")
119  // std::cout << "GtPsbTextToDigiTextFileFormatError"
120  << "GtPsbTextToDigi::produce : "
121  << " found format inconsistency in file #" << ifile
122  << "\n in skipped line:" << ievt * 2 + 1 << " cycle:" << tmp
123  << " is different from " << cbs[cycle] << std::endl;
124  }
125  }
126  }
127  LogDebug("GtPsbTextToDigi")
128  << "[GtPsbTextToDigi::produce()] skipping input " << ievt
129  << std::endl;
130  }
131  }
132  m_nevt++;
133 
134  // New collections
135  std::unique_ptr<L1GctEmCandCollection> gctIsolaEm(
136  new L1GctEmCandCollection());
137  std::unique_ptr<L1GctEmCandCollection> gctNoIsoEm(
138  new L1GctEmCandCollection());
139  std::unique_ptr<L1GctJetCandCollection> gctCenJets(
140  new L1GctJetCandCollection());
141  std::unique_ptr<L1GctJetCandCollection> gctForJets(
142  new L1GctJetCandCollection());
143  std::unique_ptr<L1GctJetCandCollection> gctTauJets(
144  new L1GctJetCandCollection());
145  // std::unique_ptr<L1GctEtTotal> gctEtTotal( new L1GctEtTotal () );
146 
148  uint16_t data[4][2] = {{0}};
149  for (int i = 0; i < 4; i++)
150  for (int j = 0; j < 2; j++)
151  data[i][j] = 0;
152 
153  // Loop over files
154  for (int ifile = 0; ifile < 4; ifile++) {
155  int ii = (ifile < 2) ? ifile : ifile + 4;
156 
157  // Check we're not at the end of the file
158  if (m_file[ifile].eof()) {
159  LogDebug("GtPsbTextToDigi")
160  << "GtPsbTextToDigi::produce : "
161  << " unexpected end of file " << m_textFileName << ii << ".txt"
162  << std::endl;
163  putEmptyDigi(iEvent);
164  continue;
165  }
166 
167  if (!m_file[ifile].good()) {
168  LogDebug("GtPsbTextToDigi") << "GtPsbTextToDigi::produce : "
169  << " problem reading file " << m_textFileName
170  << ii << ".txt" << std::endl;
171  putEmptyDigi(iEvent);
172  continue;
173  }
174 
176  unsigned long int uLongBuffer;
177 
178  for (unsigned cycle = 0; cycle < 2; cycle++) {
179  m_file[ifile] >> uLongBuffer;
180  unsigned tmp = (uLongBuffer >> 15) & 0x1;
181 
183  if (false && tmp != cbs[cycle])
184  std::cout << "[GtPsbTextToDigi::produce()] asserting "
185  << " evt:" << m_nevt << " ifile:" << ifile
186  << " cycle:" << cbs[cycle] << std::hex
187  << " buffer:" << uLongBuffer << " tmp: " << tmp << std::dec
188  << "\n\n"
189  << std::flush;
190 
191  if (tmp != cbs[cycle]) {
192  if (m_bc0[ifile] == -1 && cycle == 1 && tmp == 1) {
194  } else {
195  throw cms::Exception("GtPsbTextToDigiTextFileFormatError")
196  // std::cout << "GtPsbTextToDigiTextFileFormatError "
197  << "GtPsbTextToDigi::produce : "
198  << " found format inconsistency in file #" << ifile
199  << "\n in line:" << (m_nevt - m_fileEventOffset) * 2 - 1
200  << " cycle:" << tmp << " is different from " << cbs[cycle]
201  << std::endl;
202  }
203  }
204  data[ifile][cycle] = (uLongBuffer & 0x7fff);
205  } // cycle loop
206  } // ifile loop
207 
209  unsigned iIsola, iNoIso;
210  for (unsigned cycle = 0; cycle < 2; cycle++) {
211  for (unsigned i = 0; i < 2; i++) {
212  iIsola = i + 2;
213  iNoIso = i;
214  gctIsolaEm->push_back(L1GctEmCand(data[iIsola][cycle] & 0x7fff, true));
215  gctNoIsoEm->push_back(L1GctEmCand(data[iNoIso][cycle] & 0x7fff, false));
216  L1GctEmCand candI(data[iIsola][cycle], true);
217  L1GctEmCand candN(data[iNoIso][cycle], false);
218  }
219  }
220 
222  iEvent.put(std::move(gctIsolaEm), "isoEm");
223  iEvent.put(std::move(gctNoIsoEm), "nonIsoEm");
224  iEvent.put(std::move(gctCenJets), "cenJets");
225  iEvent.put(std::move(gctForJets), "forJets");
226  iEvent.put(std::move(gctTauJets), "tauJets");
227  // iEvent.put(std::move(gctEtTotal));
228 }
229 
232  int nmem = 4;
233  bool match = true;
234  for (int i = 0; i < nmem - 1; i++)
235  match &= (m_bc0[i] == m_bc0[i + 1]);
236  LogDebug("GtPsbTextToDigi") << "[GtPsbTextToDigi::endJob()] ";
237  if (!match)
238  LogDebug("GtPsbTextToDigi")
239  << "did not find matching BC0 in all input files: ";
240  else
241  LogDebug("GtPsbTextToDigi") << "detected common BC0 in all input files: ";
242  for (int i = 0; i < nmem; i++)
243  LogDebug("GtPsbTextToDigi") << " " << m_bc0[i];
244  LogDebug("GtPsbTextToDigi") << std::flush << std::endl;
245 }
#define LogDebug(id)
~GtPsbTextToDigi() override
void endJob() override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
Level-1 Trigger jet candidate.
Definition: L1GctJetCand.h:18
void produce(edm::Event &, const edm::EventSetup &) override
GtPsbTextToDigi(const edm::ParameterSet &)
std::string m_textFileName
Name out input file.
Level-1 Trigger EM candidate at output of GCT.
Definition: L1GctEmCand.h:22
std::vector< L1GctJetCand > L1GctJetCandCollection
int iEvent
Definition: GenABIO.cc:224
int m_fileEventOffset
Number of events to be offset wrt input.
std::ifstream m_file[4]
File handle.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void putEmptyDigi(edm::Event &)
Create empty digi collection.
int m_nevt
Event counter.
ii
Definition: cuy.py:590
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
int m_bc0[4]
Hold detected BC0 signal position per file.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
def move(src, dest)
Definition: eostools.py:511
std::vector< L1GctEmCand > L1GctEmCandCollection