CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalSelectiveReadoutProducer.cc
Go to the documentation of this file.
10 //#include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
11 
13 
14 #include <memory>
15 #include <fstream>
16 #include <atomic>
17 
18 
19 using namespace std;
20 
22  : params_(params)
23 {
24  //settings:
25  // settings which are only in python config files:
26  digiProducer_ = params.getParameter<string>("digiProducer");
27  ebdigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
28  eedigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
29  ebSRPdigiCollection_ = params.getParameter<std::string>("EBSRPdigiCollection");
30  eeSRPdigiCollection_ = params.getParameter<std::string>("EESRPdigiCollection");
31  ebSrFlagCollection_ = params.getParameter<std::string>("EBSrFlagCollection");
32  eeSrFlagCollection_ = params.getParameter<std::string>("EESrFlagCollection");
33  trigPrimProducer_ = params.getParameter<string>("trigPrimProducer");
34  trigPrimCollection_ = params.getParameter<string>("trigPrimCollection");
35  trigPrimBypass_ = params.getParameter<bool>("trigPrimBypass");
36  trigPrimBypassMode_ = params.getParameter<int>("trigPrimBypassMode");
37  dumpFlags_ = params.getUntrackedParameter<int>("dumpFlags", 0);
38  writeSrFlags_ = params.getUntrackedParameter<bool>("writeSrFlags", false);
39  produceDigis_ = params.getUntrackedParameter<bool>("produceDigis", true);
40  // settings which can come from either condition database or python configuration file:
41  useCondDb_ = false;
42  try{
43  if(params.getParameter<bool>("configFromCondDB")){
44  useCondDb_ = true;
45  }
46  } catch(cms::Exception){
47  /* pameter not found */
48  edm::LogWarning("EcalSelectiveReadout") << "Parameter configFromCondDB of EcalSelectiveReadout module not found. "
49  "Selective readout configuration will be read from python file.";
50  }
51  if(!useCondDb_){
52  settingsFromFile_ = auto_ptr<EcalSRSettings>(new EcalSRSettings());
55  }
56 
57  //declares the products made by this producer:
58  if(produceDigis_){
59  produces<EBDigiCollection>(ebSRPdigiCollection_);
60  produces<EEDigiCollection>(eeSRPdigiCollection_);
61  }
62 
63  if (writeSrFlags_) {
64  produces<EBSrFlagCollection>(ebSrFlagCollection_);
65  produces<EESrFlagCollection>(eeSrFlagCollection_);
66  }
67 
68  useFullReadout_ = false;
69  useFullReadout_ = params.getParameter<bool>("UseFullReadout");
70 
71  theGeometry = 0;
73  theElecMap = 0;
74 }
75 
76 
77 
79 { }
80 
81 
82 void
84 {
85  if(useCondDb_){
86  //getting selective readout configuration:
88 
89  if(useFullReadout_){
90  eventSetup.get<EcalSRSettingsRcd>().get("fullReadout",hSr);
91  }
92  else{
93  eventSetup.get<EcalSRSettingsRcd>().get(hSr);
94  }
95  settings_ = hSr.product();
96  }
97 
98  //gets the trigger primitives:
99  EcalTrigPrimDigiCollection emptyTPColl;
100  const EcalTrigPrimDigiCollection* trigPrims =
101  (trigPrimBypass_ && trigPrimBypassMode_==0)?&emptyTPColl:getTrigPrims(event);
102 
103 
104  //gets the digis from the events:
105  EBDigiCollection dummyEbDigiColl;
106  EEDigiCollection dummyEeDigiColl;
107 
108  const EBDigiCollection* ebDigis = produceDigis_?getEBDigis(event)
109  :&dummyEbDigiColl;
110  const EEDigiCollection* eeDigis = produceDigis_?getEEDigis(event)
111  :&dummyEeDigiColl;
112 
113  //runs the selective readout algorithm:
114  auto_ptr<EBDigiCollection> selectedEBDigis;
115  auto_ptr<EEDigiCollection> selectedEEDigis;
116  auto_ptr<EBSrFlagCollection> ebSrFlags;
117  auto_ptr<EESrFlagCollection> eeSrFlags;
118 
119  if(produceDigis_){
120  selectedEBDigis = auto_ptr<EBDigiCollection>(new EBDigiCollection);
121  selectedEEDigis = auto_ptr<EEDigiCollection>(new EEDigiCollection);
122  }
123 
124  if(writeSrFlags_){
125  ebSrFlags = auto_ptr<EBSrFlagCollection>(new EBSrFlagCollection);
126  eeSrFlags = auto_ptr<EESrFlagCollection>(new EESrFlagCollection);
127  }
128 
129  if(suppressor_.get() == 0){
130  //Check the validity of EcalSRSettings
132 
133  //instantiates the selective readout algorithm:
134  suppressor_ = auto_ptr<EcalSelectiveReadoutSuppressor>(new EcalSelectiveReadoutSuppressor(params_, settings_));
135 
136  // check that everything is up-to-date
137  checkGeometry(eventSetup);
138  checkTriggerMap(eventSetup);
139  checkElecMap(eventSetup);
140  }
141 
142  suppressor_->run(eventSetup, *trigPrims, *ebDigis, *eeDigis,
143  selectedEBDigis.get(), selectedEEDigis.get(),
144  ebSrFlags.get(), eeSrFlags.get());
145 
146  static int iEvent = 1;
147  if(dumpFlags_>=iEvent){
148  ofstream ttfFile("TTF.txt", (iEvent==1?ios::trunc:ios::app));
149  suppressor_->printTTFlags(ttfFile, iEvent,
150  iEvent==1?true:false);
151 
152  ofstream srfFile("SRF.txt", (iEvent==1?ios::trunc:ios::app));
153  if(iEvent==1){
154  suppressor_->getEcalSelectiveReadout()->printHeader(srfFile);
155  }
156  srfFile << "# Event " << iEvent << "\n";
157  suppressor_->getEcalSelectiveReadout()->print(srfFile);
158  srfFile << "\n";
159 
160  ofstream afFile("AF.txt", (iEvent==1?ios::trunc:ios::app));
161  printSrFlags(afFile, *ebSrFlags, *eeSrFlags, iEvent,
162  iEvent==1?true:false);
163  }
164 
165  ++iEvent; //event counter
166 
167  if(produceDigis_){
168  //puts the selected digis into the event:
169  event.put(selectedEBDigis, ebSRPdigiCollection_);
170  event.put(selectedEEDigis, eeSRPdigiCollection_);
171  }
172 
173  //puts the SR flags into the event:
174  if(writeSrFlags_) {
175  event.put(ebSrFlags, ebSrFlagCollection_);
176  event.put(eeSrFlags, eeSrFlagCollection_);
177  }
178 }
179 
180 const EBDigiCollection*
182 {
184  event.getByLabel(digiProducer_, ebdigiCollection_, hEBDigis);
185  //product() method is called before id() in order to get an exception
186  //if the handle is not available (check not done by id() method).
187  const EBDigiCollection* result = hEBDigis.product();
188  static bool firstCall= true;
189  if(firstCall){
190  checkWeights(event, hEBDigis.id());
191  firstCall = false;
192  }
193  return result;
194 }
195 
196 const EEDigiCollection*
198 {
200  event.getByLabel(digiProducer_, eedigiCollection_, hEEDigis);
201  //product() method is called before id() in order to get an exception
202  //if the handle is not available (check not done by id() method).
203  const EEDigiCollection* result = hEEDigis.product();
204  static bool firstCall = true;
205  if(firstCall){
206  checkWeights(event, hEEDigis.id());
207  firstCall = false;
208  }
209  return result;
210 }
211 
214 {
216  event.getByLabel(trigPrimProducer_, trigPrimCollection_, hTPDigis);
217  return hTPDigis.product();
218 }
219 
220 
222 {
223  edm::ESHandle<CaloGeometry> hGeometry;
224  eventSetup.get<CaloGeometryRecord>().get(hGeometry);
225 
226  const CaloGeometry * pGeometry = &*hGeometry;
227 
228  // see if we need to update
229  if(pGeometry != theGeometry) {
230  theGeometry = pGeometry;
231  suppressor_->setGeometry(theGeometry);
232  }
233 }
234 
235 
237 {
238 
240  eventSetup.get<IdealGeometryRecord>().get(eTTmap);
241 
242  const EcalTrigTowerConstituentsMap * pMap = &*eTTmap;
243 
244  // see if we need to update
245  if(pMap!= theTriggerTowerMap) {
246  theTriggerTowerMap = pMap;
247  suppressor_->setTriggerMap(theTriggerTowerMap);
248  }
249 }
250 
251 
253 {
254 
256  eventSetup.get<EcalMappingRcd>().get(eElecmap);
257 
258  const EcalElectronicsMapping * pMap = &*eElecmap;
259 
260  // see if we need to update
261  if(pMap!= theElecMap) {
262  theElecMap = pMap;
263  suppressor_->setElecMap(theElecMap);
264  }
265 }
266 
267 
269  const char tccFlagMarker[] = { 'x', '.', 'S', '?', 'C', 'E', 'E', 'E', 'E'};
272 
273  //static bool firstCall=true;
274  // if(firstCall){
275  // firstCall=false;
276  os << "# TCC flag map\n#\n"
277  "# +-->Phi " << tccFlagMarker[0+1] << ": 000 (low interest)\n"
278  "# | " << tccFlagMarker[1+1] << ": 001 (mid interest)\n"
279  "# | " << tccFlagMarker[2+1] << ": 010 (not valid)\n"
280  "# V Eta " << tccFlagMarker[3+1] << ": 011 (high interest)\n"
281  "# " << tccFlagMarker[4+1] << ": 1xx forced readout (Hw error)\n"
282  "#\n";
283  //}
284 
285  vector<vector<int> > ttf(nEta, vector<int>(nPhi, -1));
287  it != tp.end(); ++it){
288  const EcalTriggerPrimitiveDigi& trigPrim = *it;
289  if(trigPrim.size()>0){
290  int iEta = trigPrim.id().ieta();
291  int iEta0 = iEta<0?iEta+nEta/2:iEta+nEta/2-1;
292  int iPhi0 = trigPrim.id().iphi() - 1;
293  ttf[iEta0][iPhi0] = trigPrim.ttFlag();
294  }
295  }
296  for(int iEta=0; iEta<nEta; ++iEta){
297  for(int iPhi=0; iPhi<nPhi; ++iPhi){
298  os << tccFlagMarker[ttf[iEta][iPhi]+1];
299  }
300  os << "\n";
301  }
302 }
303 
305  const edm::ProductID& noZsDigiId) const{
306  const vector<float> & weights = settings_->dccNormalizedWeights_[0]; //params_.getParameter<vector<double> >("dccNormalizedWeights");
308  static std::atomic<bool> warnWeightCnt{true};
309  bool expected = true;
310  if((int)weights.size() > nFIRTaps && warnWeightCnt.compare_exchange_strong(expected,false,std::memory_order_acq_rel)){
311  edm::LogWarning("Configuration") << "The list of DCC zero suppression FIR "
312  "weights given in parameter dccNormalizedWeights is longer "
313  "than the expected depth of the FIR filter :(" << nFIRTaps << "). "
314  "The last weights will be discarded.";
315  }
316 
317  if(weights.size()>0){
318  int iMaxWeight = 0;
319  double maxWeight = weights[iMaxWeight];
320  //looks for index of maximum weight
321  for(unsigned i=0; i<weights.size(); ++i){
322  if(weights[i]>maxWeight){
323  iMaxWeight = i;
324  maxWeight = weights[iMaxWeight];
325  }
326  }
327 
328  //position of time sample whose maximum weight is applied:
329  int maxWeightBin = settings_->ecalDccZs1stSample_[0] //params_.getParameter<int>("ecalDccZs1stSample")
330  + iMaxWeight;
331 
332  //gets the bin of maximum (in case of raw data it will not exist)
333  int binOfMax = 0;
334  bool rc = getBinOfMax(evt, noZsDigiId, binOfMax);
335 
336  if(rc && maxWeightBin!=binOfMax){
337  edm::LogWarning("Configuration")
338  << "The maximum weight of DCC zero suppression FIR filter is not "
339  "applied to the expected maximum sample(" << binOfMax
340  << (binOfMax==1?"st":(binOfMax==2?"nd":(binOfMax==3?"rd":"th")))
341  << " time sample). This may indicate faulty 'dccNormalizedWeights' "
342  "or 'ecalDccZs1sSample' parameters.";
343  }
344  }
345 }
346 
347 bool
349  const edm::ProductID& noZsDigiId,
350  int& binOfMax) const{
351  bool rc;
352  const edm::Provenance p=evt.getProvenance(noZsDigiId);
354  vector<string> ebDigiParamList = result.getParameterNames();
355  string bofm("binOfMaximum");
356  if(find(ebDigiParamList.begin(), ebDigiParamList.end(), bofm)
357  != ebDigiParamList.end()){//bofm found
358  binOfMax=result.getParameter<int>("binOfMaximum");
359  rc = true;
360  } else{
361  rc = false;
362  }
363  return rc;
364 }
365 
366 void
368  const EBSrFlagCollection& ebSrFlags,
369  const EESrFlagCollection& eeSrFlags,
370  int iEvent,
371  bool withHeader){
372  const char srpFlagMarker[] = {'.', 'z', 'Z', 'F', '4','5','6','7'};
373  if(withHeader){
374  time_t t;
375  time(&t);
376  const char* date = ctime(&t);
377  os << "#SRP flag map\n#\n"
378  "# Generatied on: " << date << "\n#\n"
379  "# +-->Phi/Y " << srpFlagMarker[0] << ": suppressed\n"
380  "# | " << srpFlagMarker[1] << ": ZS 1\n"
381  "# | " << srpFlagMarker[2] << ": ZS 2\n"
382  "# V Eta/X " << srpFlagMarker[3] << ": full readout\n"
383  "#\n";
384  }
385 
386  //EE-,EB,EE+ map wil be written onto file in following format:
387  //
388  // 72
389  // <-------------->
390  // 20
391  // <--->
392  // EEE A +-----> Y
393  // EEEEE | |
394  // EE EE | 20 EE- |
395  // EEEEE | |
396  // EEE V V X
397  // BBBBBBBBBBBBBBBBB A
398  // BBBBBBBBBBBBBBBBB | +-----> Phi
399  // BBBBBBBBBBBBBBBBB | |
400  // BBBBBBBBBBBBBBBBB | 34 EB |
401  // BBBBBBBBBBBBBBBBB | |
402  // BBBBBBBBBBBBBBBBB | V Eta
403  // BBBBBBBBBBBBBBBBB |
404  // BBBBBBBBBBBBBBBBB |
405  // BBBBBBBBBBBBBBBBB V
406  // EEE A +-----> Y
407  // EEEEE | |
408  // EE EE | 20 EE+ |
409  // EEEEE | |
410  // EEE V V X
411  //
412  //
413  //
414  //
415  //event header:
416  if(iEvent>=0){
417  os << "# Event " << iEvent << "\n";
418  }
419 
420  //retrieve flags:
421  const int nEndcaps = 2;
422  const int nScX = 20;
423  const int nScY = 20;
424  int eeSrf[nEndcaps][nScX][nScY];
425  for(size_t i=0; i < sizeof(eeSrf)/sizeof(int); ((int*)eeSrf)[i++] = -1){};
426  for(EESrFlagCollection::const_iterator it = eeSrFlags.begin();
427  it != eeSrFlags.end(); ++it){
428  const EESrFlag& flag = *it;
429  int iZ0 = flag.id().zside()>0?1:0;
430  int iX0 = flag.id().ix()-1;
431  int iY0 = flag.id().iy()-1;
432  assert(iZ0>=0 && iZ0<nEndcaps);
433  assert(iX0>=0 && iX0<nScX);
434  assert(iY0>=0 && iY0<nScY);
435  eeSrf[iZ0][iX0][iY0] = flag.value();
436  }
437  const int nEbTtEta = 34;
438  const int nEeTtEta = 11;
439  const int nTtEta = nEeTtEta*2+nEbTtEta;
440  const int nTtPhi = 72;
441  int ebSrf[nEbTtEta][nTtPhi];
442  for(size_t i=0; i<sizeof(ebSrf)/sizeof(int); ((int*)ebSrf)[i++] = -1){};
443  for(EBSrFlagCollection::const_iterator it = ebSrFlags.begin();
444  it != ebSrFlags.end(); ++it){
445 
446  const EBSrFlag& flag = *it;
447  int iEta = flag.id().ieta();
448  int iEta0 = iEta + nTtEta/2 - (iEta>=0?1:0); //0->55 from eta=-3 to eta=3
449  int iEbEta0 = iEta0 - nEeTtEta;//0->33 from eta=-1.48 to eta=1.48
450  int iPhi0 = flag.id().iphi() - 1;
451  assert(iEbEta0>=0 && iEbEta0<nEbTtEta);
452  assert(iPhi0>=0 && iPhi0<nTtPhi);
453 
454 // cout << __FILE__ << ":" << __LINE__ << ": "
455 // << iEta << "\t" << flag.id().iphi() << " -> "
456 // << iEbEta0 << "\t" << iPhi0
457 // << "... Flag: " << flag.value() << "\n";
458 
459 
460  ebSrf[iEbEta0][iPhi0] = flag.value();
461  }
462 
463 
464  //print flags:
465 
466  //EE-
467  for(int iX0=0; iX0<nScX; ++iX0){
468  for(int iY0=0; iY0<nScY; ++iY0){
469  int srFlag = eeSrf[0][iX0][iY0];
470  assert(srFlag>=-1
471  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
472  os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
473  }
474  os << "\n"; //one Y supercystal column per line
475  } //next supercrystal X-index
476 
477  //EB
478  for(int iEta0 = 0;
479  iEta0 < nEbTtEta;
480  ++iEta0){
481  for(int iPhi0 = 0; iPhi0 < nTtPhi; ++iPhi0){
482  int srFlag = ebSrf[iEta0][iPhi0];
483  assert(srFlag>=-1
484  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
485  os << (srFlag==-1?'?':srpFlagMarker[srFlag]);
486  }
487  os << "\n"; //one phi per line
488  }
489 
490  //EE+
491  for(int iX0=0; iX0<nScX; ++iX0){
492  for(int iY0=0; iY0<nScY; ++iY0){
493  int srFlag = eeSrf[1][iX0][iY0];
494  assert(srFlag>=-1
495  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
496  os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
497  }
498  os << "\n"; //one Y supercystal column per line
499  } //next supercrystal X-index
500 
501  //event trailer:
502  os << "\n";
503 }
504 
506  if(settings.dccNormalizedWeights_.size() != 1){
507  throw cms::Exception("Configuration") << "Selective readout emulator, EcalSelectiveReadout, supports only single set of ZS weights. "
508  "while the configuration contains " << settings.dccNormalizedWeights_.size() << " set(s)\n";
509  }
510 
511 // if(settings.dccNormalizedWeights_.size() != 1
512 // && settings.dccNormalizedWeights_.size() != 2
513 // && settings.dccNormalizedWeights_.size() != 54
514 // && settings.dccNormalizedWeights_.size() != 75848){
515 // throw cms::Exception("Configuration") << "Invalid number of DCC weight set (" << settings.dccNormalizedWeights_.size()
516 // << ") in condition object EcalSRSetting::dccNormalizedWeights_. "
517 // << "Valid counts are: 1 (single set), 2 (EB and EE), 54 (one per DCC) and 75848 "
518 // "(one per crystal)\n";
519 // }
520 
521  if(settings.dccNormalizedWeights_.size() != settings.ecalDccZs1stSample_.size()){
522  throw cms::Exception("Configuration") << "Inconsistency between number of weigth sets ("
523  << settings.dccNormalizedWeights_.size() << ") and "
524  << "number of ecalDccZs1Sample values ("
525  << settings.ecalDccZs1stSample_.size() << ").";
526  }
527 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
int value() const
Definition: EcalSrFlag.h:44
const EEDigiCollection * getEEDigis(edm::Event &event) const
void checkTriggerMap(const edm::EventSetup &eventSetup)
ProductID id() const
Definition: HandleBase.cc:15
static void importParameterSet(EcalSRSettings &sr, const edm::ParameterSet &ps)
void checkGeometry(const edm::EventSetup &eventSetup)
std::vector< EcalTriggerPrimitiveDigi >::const_iterator const_iterator
std::vector< std::vector< float > > dccNormalizedWeights_
const char tccFlagMarker[]
Definition: GenABIO.cc:189
bool getBinOfMax(const edm::Event &evt, const edm::ProductID &noZsDigiId, int &binOfMax) const
void printTTFlags(const EcalTrigPrimDigiCollection &tp, std::ostream &os) const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
int ieta() const
get the tower ieta
virtual void produce(edm::Event &event, const edm::EventSetup &eventSetup)
void checkElecMap(const edm::EventSetup &eventSetup)
int iEvent
Definition: GenABIO.cc:243
edm::SortedCollection< EBSrFlag > EBSrFlagCollection
int ix() const
Definition: EcalScDetId.h:71
const EcalTrigTowerConstituentsMap * theTriggerTowerMap
edm::SortedCollection< EESrFlag > EESrFlagCollection
std::vector< int > ecalDccZs1stSample_
tuple result
Definition: query.py:137
const EcalElectronicsMapping * theElecMap
std::vector< std::string > getParameterNames() const
std::auto_ptr< EcalSelectiveReadoutSuppressor > suppressor_
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
void checkWeights(const edm::Event &evt, const edm::ProductID &noZSDigiId) const
int iy() const
Definition: EcalScDetId.h:77
const_iterator end() const
int iphi() const
get the tower iphi
static const int nEndcaps
Definition: GenABIO.cc:138
const EBDigiCollection * getEBDigis(edm::Event &event) const
const EcalTrigPrimDigiCollection * getTrigPrims(edm::Event &event) const
const EcalTrigTowerDetId & id() const
const EcalScDetId & id() const
Definition: EESrFlag.h:37
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
static const size_t nTriggerTowersInEta
int zside() const
Definition: EcalScDetId.h:65
static const size_t nTriggerTowersInPhi
T const * product() const
Definition: Handle.h:81
const EcalTrigTowerDetId & id() const
Definition: EBSrFlag.h:36
static void printSrFlags(std::ostream &os, const EBSrFlagCollection &ebSrFlags, const EESrFlagCollection &eeSrFlags, int iEvent=-1, bool withHeader=true)
EcalSelectiveReadoutProducer(const edm::ParameterSet &params)
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:76
static void checkValidity(const EcalSRSettings &settings)
int ttFlag() const
get the Trigger tower Flag of interesting sample
ParameterSet const & parameterSet(Provenance const &provenance)
Definition: Provenance.cc:11
const_iterator begin() const
const char srpFlagMarker[]
Definition: GenABIO.cc:188
std::auto_ptr< EcalSRSettings > settingsFromFile_