00001 #include "SimCalorimetry/EcalSelectiveReadoutProducers/interface/EcalSelectiveReadoutProducer.h"
00002 #include "FWCore/Framework/interface/Event.h"
00003 #include "FWCore/Framework/interface/EventSetup.h"
00004 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include <memory>
00008
00009 #include <fstream>
00010
00011 #include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
00012
00013 using namespace std;
00014
00015 EcalSelectiveReadoutProducer::EcalSelectiveReadoutProducer(const edm::ParameterSet& params)
00016 : params_(params)
00017 {
00018
00019 digiProducer_ = params.getParameter<string>("digiProducer");
00020 ebdigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
00021 eedigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
00022 ebSRPdigiCollection_ = params.getParameter<std::string>("EBSRPdigiCollection");
00023 eeSRPdigiCollection_ = params.getParameter<std::string>("EESRPdigiCollection");
00024 ebSrFlagCollection_ = params.getParameter<std::string>("EBSrFlagCollection");
00025 eeSrFlagCollection_ = params.getParameter<std::string>("EESrFlagCollection");
00026 trigPrimProducer_ = params.getParameter<string>("trigPrimProducer");
00027 trigPrimCollection_ = params.getParameter<string>("trigPrimCollection");
00028 trigPrimBypass_ = params.getParameter<bool>("trigPrimBypass");
00029 dumpFlags_ = params.getUntrackedParameter<int>("dumpFlags", 0);
00030 writeSrFlags_ = params.getUntrackedParameter<bool>("writeSrFlags",false);
00031
00032 suppressor_ = auto_ptr<EcalSelectiveReadoutSuppressor>(new EcalSelectiveReadoutSuppressor(params));
00033
00034 produces<EBDigiCollection>(ebSRPdigiCollection_);
00035 produces<EEDigiCollection>(eeSRPdigiCollection_);
00036
00037 if ( writeSrFlags_ ) {
00038 produces<EBSrFlagCollection>(ebSrFlagCollection_);
00039 produces<EESrFlagCollection>(eeSrFlagCollection_);
00040 }
00041
00042 theGeometry = 0;
00043 theTriggerTowerMap = 0;
00044
00045 }
00046
00047
00048
00049 EcalSelectiveReadoutProducer::~EcalSelectiveReadoutProducer()
00050 { }
00051
00052
00053 void
00054 EcalSelectiveReadoutProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup)
00055 {
00056
00057 checkGeometry(eventSetup);
00058 checkTriggerMap(eventSetup);
00059
00060
00061 EcalTrigPrimDigiCollection emptyTPColl;
00062 const EcalTrigPrimDigiCollection* trigPrims =
00063 trigPrimBypass_?&emptyTPColl:getTrigPrims(event);
00064
00065
00066
00067 const EBDigiCollection* ebDigis = getEBDigis(event);
00068 const EEDigiCollection* eeDigis = getEEDigis(event);
00069
00070
00071 auto_ptr<EBDigiCollection> selectedEBDigis(new EBDigiCollection);
00072 auto_ptr<EEDigiCollection> selectedEEDigis(new EEDigiCollection);
00073 auto_ptr<EBSrFlagCollection> ebSrFlags(new EBSrFlagCollection);
00074 auto_ptr<EESrFlagCollection> eeSrFlags(new EESrFlagCollection);
00075
00076 suppressor_->run(eventSetup, *trigPrims, *ebDigis, *eeDigis,
00077 *selectedEBDigis, *selectedEEDigis,
00078 *ebSrFlags, *eeSrFlags);
00079
00080 static int iEvent = 1;
00081 if(dumpFlags_>=iEvent){
00082 ofstream ttfFile("TTF.txt", (iEvent==1?ios::trunc:ios::app));
00083 suppressor_->printTTFlags(ttfFile, iEvent,
00084 iEvent==1?true:false);
00085
00086 ofstream srfFile("SRF.txt", (iEvent==1?ios::trunc:ios::app));
00087 if(iEvent==1){
00088 suppressor_->getEcalSelectiveReadout()->printHeader(srfFile);
00089 }
00090 srfFile << "# Event " << iEvent << "\n";
00091 suppressor_->getEcalSelectiveReadout()->print(srfFile);
00092 srfFile << "\n";
00093
00094 ofstream afFile("AF.txt", (iEvent==1?ios::trunc:ios::app));
00095 printSrFlags(afFile, *ebSrFlags, *eeSrFlags, iEvent,
00096 iEvent==1?true:false);
00097 }
00098
00099 ++iEvent;
00100
00101
00102 event.put(selectedEBDigis, ebSRPdigiCollection_);
00103 event.put(selectedEEDigis, eeSRPdigiCollection_);
00104
00105
00106 if ( writeSrFlags_ ) {
00107 event.put(ebSrFlags, ebSrFlagCollection_);
00108 event.put(eeSrFlags, eeSrFlagCollection_);
00109 }
00110 }
00111
00112 const EBDigiCollection*
00113 EcalSelectiveReadoutProducer::getEBDigis(edm::Event& event) const
00114 {
00115 edm::Handle<EBDigiCollection> hEBDigis;
00116 event.getByLabel(digiProducer_, ebdigiCollection_, hEBDigis);
00117
00118
00119 const EBDigiCollection* result = hEBDigis.product();
00120 static bool firstCall= true;
00121 if(firstCall){
00122 checkWeights(event, hEBDigis.id());
00123 firstCall = false;
00124 }
00125 return result;
00126 }
00127
00128 const EEDigiCollection*
00129 EcalSelectiveReadoutProducer::getEEDigis(edm::Event& event) const
00130 {
00131 edm::Handle<EEDigiCollection> hEEDigis;
00132 event.getByLabel(digiProducer_, eedigiCollection_, hEEDigis);
00133
00134
00135 const EEDigiCollection* result = hEEDigis.product();
00136 static bool firstCall = true;
00137 if(firstCall){
00138 checkWeights(event, hEEDigis.id());
00139 firstCall = false;
00140 }
00141 return result;
00142 }
00143
00144 const EcalTrigPrimDigiCollection*
00145 EcalSelectiveReadoutProducer::getTrigPrims(edm::Event& event) const
00146 {
00147 edm::Handle<EcalTrigPrimDigiCollection> hTPDigis;
00148 event.getByLabel(trigPrimProducer_, trigPrimCollection_, hTPDigis);
00149 return hTPDigis.product();
00150 }
00151
00152
00153 void EcalSelectiveReadoutProducer::checkGeometry(const edm::EventSetup & eventSetup)
00154 {
00155 edm::ESHandle<CaloGeometry> hGeometry;
00156 eventSetup.get<CaloGeometryRecord>().get(hGeometry);
00157
00158 const CaloGeometry * pGeometry = &*hGeometry;
00159
00160
00161 if(pGeometry != theGeometry) {
00162 theGeometry = pGeometry;
00163 suppressor_->setGeometry(theGeometry);
00164 }
00165 }
00166
00167
00168 void EcalSelectiveReadoutProducer::checkTriggerMap(const edm::EventSetup & eventSetup)
00169 {
00170
00171 edm::ESHandle<EcalTrigTowerConstituentsMap> eTTmap;
00172 eventSetup.get<IdealGeometryRecord>().get(eTTmap);
00173
00174 const EcalTrigTowerConstituentsMap * pMap = &*eTTmap;
00175
00176
00177 if(pMap!= theTriggerTowerMap) {
00178 theTriggerTowerMap = pMap;
00179 suppressor_->setTriggerMap(theTriggerTowerMap);
00180 }
00181 }
00182
00183
00184 void EcalSelectiveReadoutProducer::printTTFlags(const EcalTrigPrimDigiCollection& tp, ostream& os) const{
00185 const char tccFlagMarker[] = { 'x', '.', 'S', '?', 'C', 'E', 'E', 'E', 'E'};
00186 const int nEta = EcalSelectiveReadout::nTriggerTowersInEta;
00187 const int nPhi = EcalSelectiveReadout::nTriggerTowersInPhi;
00188
00189
00190
00191
00192 os << "# TCC flag map\n#\n"
00193 "# +-->Phi " << tccFlagMarker[0+1] << ": 000 (low interest)\n"
00194 "# | " << tccFlagMarker[1+1] << ": 001 (mid interest)\n"
00195 "# | " << tccFlagMarker[2+1] << ": 010 (not valid)\n"
00196 "# V Eta " << tccFlagMarker[3+1] << ": 011 (high interest)\n"
00197 "# " << tccFlagMarker[4+1] << ": 1xx forced readout (Hw error)\n"
00198 "#\n";
00199
00200
00201 vector<vector<int> > ttf(nEta, vector<int>(nPhi, -1));
00202 for(EcalTrigPrimDigiCollection::const_iterator it = tp.begin();
00203 it != tp.end(); ++it){
00204 const EcalTriggerPrimitiveDigi& trigPrim = *it;
00205 if(trigPrim.size()>0){
00206 int iEta = trigPrim.id().ieta();
00207 int iEta0 = iEta<0?iEta+nEta/2:iEta+nEta/2-1;
00208 int iPhi0 = trigPrim.id().iphi() - 1;
00209 ttf[iEta0][iPhi0] = trigPrim.ttFlag();
00210 }
00211 }
00212 for(int iEta=0; iEta<nEta; ++iEta){
00213 for(int iPhi=0; iPhi<nPhi; ++iPhi){
00214 os << tccFlagMarker[ttf[iEta][iPhi]+1];
00215 }
00216 os << "\n";
00217 }
00218 }
00219
00220 void EcalSelectiveReadoutProducer::checkWeights(const edm::Event& evt,
00221 const edm::ProductID& noZsDigiId) const{
00222 const vector<double> & weights = params_.getParameter<vector<double> >("dccNormalizedWeights");
00223 int nFIRTaps = EcalSelectiveReadoutSuppressor::getFIRTapCount();
00224 static bool warnWeightCnt = true;
00225 if((int)weights.size() > nFIRTaps && warnWeightCnt){
00226 edm::LogWarning("Configuration") << "The list of DCC zero suppression FIR "
00227 "weights given in parameter dccNormalizedWeights is longer "
00228 "than the expected depth of the FIR filter :(" << nFIRTaps << "). "
00229 "The last weights will be discarded.";
00230 warnWeightCnt = false;
00231 }
00232
00233 if(weights.size()>0){
00234 int iMaxWeight = 0;
00235 double maxWeight = weights[iMaxWeight];
00236
00237 for(unsigned i=0; i<weights.size(); ++i){
00238 if(weights[i]>maxWeight){
00239 iMaxWeight = i;
00240 maxWeight = weights[iMaxWeight];
00241 }
00242 }
00243
00244
00245 int maxWeightBin = params_.getParameter<int>("ecalDccZs1stSample")
00246 + iMaxWeight;
00247
00248
00249 int binOfMax = 0;
00250 bool rc = getBinOfMax(evt, noZsDigiId, binOfMax);
00251
00252 if(rc && maxWeightBin!=binOfMax){
00253 edm::LogWarning("Configuration")
00254 << "The maximum weight of DCC zero suppression FIR filter is not "
00255 "applied to the expected maximum sample(" << binOfMax
00256 << (binOfMax==1?"st":(binOfMax==2?"nd":(binOfMax==3?"rd":"th")))
00257 << " time sample). This may indicate faulty 'dccNormalizedWeights' "
00258 "or 'ecalDccZs1sSample' parameters.";
00259 }
00260 }
00261 }
00262
00263 bool
00264 EcalSelectiveReadoutProducer::getBinOfMax(const edm::Event& evt,
00265 const edm::ProductID& noZsDigiId,
00266 int& binOfMax) const{
00267 bool rc;
00268 const edm::Provenance p=evt.getProvenance(noZsDigiId);
00269 edm::ParameterSet result = getParameterSet(p.psetID());
00270 vector<string> ebDigiParamList = result.getParameterNames();
00271 string bofm("binOfMaximum");
00272 if(find(ebDigiParamList.begin(), ebDigiParamList.end(), bofm)
00273 != ebDigiParamList.end()){
00274 binOfMax=result.getParameter<int>("binOfMaximum");
00275 rc = true;
00276 } else{
00277 rc = false;
00278 }
00279 return rc;
00280 }
00281
00282 void
00283 EcalSelectiveReadoutProducer::printSrFlags(ostream& os,
00284 const EBSrFlagCollection& ebSrFlags,
00285 const EESrFlagCollection& eeSrFlags,
00286 int iEvent,
00287 bool withHeader){
00288 const char srpFlagMarker[] = {'.', 'z', 'Z', 'F', '4','5','6','7'};
00289 if(withHeader){
00290 time_t t;
00291 time(&t);
00292 const char* date = ctime(&t);
00293 os << "#SRP flag map\n#\n"
00294 "# Generatied on: " << date << "\n#\n"
00295 "# +-->Phi/Y " << srpFlagMarker[0] << ": suppressed\n"
00296 "# | " << srpFlagMarker[1] << ": ZS 1\n"
00297 "# | " << srpFlagMarker[2] << ": ZS 2\n"
00298 "# V Eta/X " << srpFlagMarker[3] << ": full readout\n"
00299 "#\n";
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 if(iEvent>=0){
00333 os << "# Event " << iEvent << "\n";
00334 }
00335
00336
00337 const int nEndcaps = 2;
00338 const int nScX = 20;
00339 const int nScY = 20;
00340 int eeSrf[nEndcaps][nScX][nScY];
00341 for(size_t i=0; i < sizeof(eeSrf)/sizeof(int); ((int*)eeSrf)[i++] = -1){};
00342 for(EESrFlagCollection::const_iterator it = eeSrFlags.begin();
00343 it != eeSrFlags.end(); ++it){
00344 const EESrFlag& flag = *it;
00345 int iZ0 = flag.id().zside()>0?1:0;
00346 int iX0 = flag.id().ix()-1;
00347 int iY0 = flag.id().iy()-1;
00348 assert(iZ0>=0 && iZ0<nEndcaps);
00349 assert(iX0>=0 && iX0<nScX);
00350 assert(iY0>=0 && iY0<nScY);
00351 eeSrf[iZ0][iX0][iY0] = flag.value();
00352 }
00353 const int nEbTtEta = 34;
00354 const int nEeTtEta = 11;
00355 const int nTtEta = nEeTtEta*2+nEbTtEta;
00356 const int nTtPhi = 72;
00357 int ebSrf[nEbTtEta][nTtPhi];
00358 for(size_t i=0; i<sizeof(ebSrf)/sizeof(int); ((int*)ebSrf)[i++] = -1){};
00359 for(EBSrFlagCollection::const_iterator it = ebSrFlags.begin();
00360 it != ebSrFlags.end(); ++it){
00361
00362 const EBSrFlag& flag = *it;
00363 int iEta = flag.id().ieta();
00364 int iEta0 = iEta + nTtEta/2 - (iEta>=0?1:0);
00365 int iEbEta0 = iEta0 - nEeTtEta;
00366 int iPhi0 = flag.id().iphi() - 1;
00367 assert(iEbEta0>=0 && iEbEta0<nEbTtEta);
00368 assert(iPhi0>=0 && iPhi0<nTtPhi);
00369
00370
00371
00372
00373
00374
00375
00376 ebSrf[iEbEta0][iPhi0] = flag.value();
00377 }
00378
00379
00380
00381
00382
00383 for(int iX0=0; iX0<nScX; ++iX0){
00384 for(int iY0=0; iY0<nScY; ++iY0){
00385 int srFlag = eeSrf[0][iX0][iY0];
00386 assert(srFlag>=-1
00387 && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
00388 os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
00389 }
00390 os << "\n";
00391 }
00392
00393
00394 for(int iEta0 = 0;
00395 iEta0 < nEbTtEta;
00396 ++iEta0){
00397 for(int iPhi0 = 0; iPhi0 < nTtPhi; ++iPhi0){
00398 int srFlag = ebSrf[iEta0][iPhi0];
00399 assert(srFlag>=-1
00400 && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
00401 os << (srFlag==-1?'?':srpFlagMarker[srFlag]);
00402 }
00403 os << "\n";
00404 }
00405
00406
00407 for(int iX0=0; iX0<nScX; ++iX0){
00408 for(int iY0=0; iY0<nScY; ++iY0){
00409 int srFlag = eeSrf[1][iX0][iY0];
00410 assert(srFlag>=-1
00411 && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
00412 os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
00413 }
00414 os << "\n";
00415 }
00416
00417
00418 os << "\n";
00419 }