CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/RecoLocalCalo/HcalRecAlgos/src/HcalSeverityLevelComputer.cc

Go to the documentation of this file.
00001 #include "RecoLocalCalo/HcalRecAlgos/interface/HcalSeverityLevelComputer.h"
00002 #include "CondFormats/HcalObjects/interface/HcalChannelStatus.h"
00003 #include "RecoLocalCalo/HcalRecAlgos/interface/HcalCaloFlagLabels.h"
00004 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 
00009 bool HcalSeverityLevelComputer::getChStBit(HcalSeverityDefinition& mydef, 
00010                                            const std::string& mybit)
00011 {
00012   if (mybit == "HcalCellOff") setBit(HcalChannelStatus::HcalCellOff, mydef.chStatusMask);
00013   else if (mybit == "HcalCellMask") setBit(HcalChannelStatus::HcalCellMask, mydef.chStatusMask);
00014   else if (mybit == "HcalCellDead") setBit(HcalChannelStatus::HcalCellDead, mydef.chStatusMask);
00015   else if (mybit == "HcalCellHot") setBit(HcalChannelStatus::HcalCellHot, mydef.chStatusMask);
00016   else if (mybit == "HcalCellStabErr") setBit(HcalChannelStatus::HcalCellStabErr, mydef.chStatusMask);
00017   else if (mybit == "HcalCellTimErr") setBit(HcalChannelStatus::HcalCellTimErr, mydef.chStatusMask);
00018   else if (mybit == "HcalCellTrigMask") setBit(HcalChannelStatus::HcalCellTrigMask, mydef.chStatusMask);
00019   else if (mybit == "HcalCellCaloTowerMask") setBit(HcalChannelStatus::HcalCellCaloTowerMask, mydef.chStatusMask);
00020   else if (mybit == "HcalCellCaloTowerProb") setBit(HcalChannelStatus::HcalCellCaloTowerProb, mydef.chStatusMask);
00021   else 
00022     { // error: unrecognized channel status name
00023       edm::LogWarning  ("HcalSeverityLevelComputer") 
00024         << "HcalSeverityLevelComputer: Error: ChannelStatusFlag >>" << mybit 
00025         << "<< unknown. Ignoring.";
00026       return false;
00027     }
00028   return true;
00029 }
00030 
00031 bool HcalSeverityLevelComputer::getRecHitFlag(HcalSeverityDefinition& mydef, 
00032                                               const std::string& mybit)
00033 {
00034   // HB, HE ++++++++++++++++++++
00035   if (mybit == "HBHEHpdHitMultiplicity") setBit(HcalCaloFlagLabels::HBHEHpdHitMultiplicity, mydef.HBHEFlagMask);
00036   else if (mybit == "HBHEPulseShape")    setBit(HcalCaloFlagLabels::HBHEPulseShape, mydef.HBHEFlagMask);
00037   else if (mybit == "HSCP_R1R2")         setBit(HcalCaloFlagLabels::HSCP_R1R2, mydef.HBHEFlagMask);
00038   else if (mybit == "HSCP_FracLeader")   setBit(HcalCaloFlagLabels::HSCP_FracLeader, mydef.HBHEFlagMask);
00039   else if (mybit == "HSCP_OuterEnergy")  setBit(HcalCaloFlagLabels::HSCP_OuterEnergy, mydef.HBHEFlagMask);
00040   else if (mybit == "HSCP_ExpFit")       setBit(HcalCaloFlagLabels::HSCP_ExpFit, mydef.HBHEFlagMask);
00041   else if (mybit == "HBHEFlatNoise")     setBit(HcalCaloFlagLabels::HBHEFlatNoise, mydef.HBHEFlagMask);
00042   else if (mybit == "HBHESpikeNoise")    setBit(HcalCaloFlagLabels::HBHESpikeNoise, mydef.HBHEFlagMask);
00043   else if (mybit == "HBHETriangleNoise") setBit(HcalCaloFlagLabels::HBHETriangleNoise, mydef.HBHEFlagMask);
00044   else if (mybit == "HBHETS4TS5Noise") setBit(HcalCaloFlagLabels::HBHETS4TS5Noise, mydef.HBHEFlagMask);
00045 
00046   // These are multi-bit counters; we may have to revisit how to set them in the SLComputer in the future
00047   else if (mybit=="HBHETimingTrustBits") setBit(HcalCaloFlagLabels::HBHETimingTrustBits, mydef.HBHEFlagMask );
00048   else if (mybit=="HBHETimingShapedCutsBits") setBit(HcalCaloFlagLabels::HBHETimingShapedCutsBits, mydef.HBHEFlagMask);
00049   else if (mybit=="HBHEIsolatedNoise")   setBit(HcalCaloFlagLabels::HBHEIsolatedNoise, mydef.HBHEFlagMask );
00050 
00051   // HO ++++++++++++++++++++
00052   else if (mybit == "HOBit")    setBit(HcalCaloFlagLabels::HOBit, mydef.HOFlagMask);
00053   
00054   // HF ++++++++++++++++++++
00055   else if (mybit == "HFLongShort")    setBit(HcalCaloFlagLabels::HFLongShort, mydef.HFFlagMask);
00056   else if (mybit == "HFDigiTime")    setBit(HcalCaloFlagLabels::HFDigiTime, mydef.HFFlagMask);
00057   else if (mybit == "HFInTimeWindow") setBit(HcalCaloFlagLabels::HFInTimeWindow, mydef.HFFlagMask);
00058   else if (mybit == "HFS8S1Ratio") setBit(HcalCaloFlagLabels::HFS8S1Ratio, mydef.HFFlagMask);
00059   else if (mybit == "HFPET")  setBit(HcalCaloFlagLabels::HFPET, mydef.HFFlagMask);
00060   else if (mybit == "HFTimingTrustBits")  setBit(HcalCaloFlagLabels::HFTimingTrustBits, mydef.HFFlagMask); // multi-bit counter
00061 
00062   // ZDC ++++++++++++++++++++
00063   else if (mybit == "ZDCBit")     setBit(HcalCaloFlagLabels::ZDCBit, mydef.ZDCFlagMask);
00064   
00065   // Calib ++++++++++++++++++++
00066   else if (mybit == "CalibrationBit")     setBit(HcalCaloFlagLabels::CalibrationBit, mydef.CalibFlagMask);
00067 
00068   // Common subdetector bits ++++++++++++++++++++++
00069   else if (mybit == "TimingSubtractedBit")  setAllRHMasks(HcalCaloFlagLabels::TimingSubtractedBit, mydef);
00070   else if (mybit == "TimingAddedBit")       setAllRHMasks(HcalCaloFlagLabels::TimingAddedBit,      mydef);
00071   else if (mybit == "TimingErrorBit")       setAllRHMasks(HcalCaloFlagLabels::TimingErrorBit,      mydef);
00072   else if (mybit == "ADCSaturationBit")     setAllRHMasks(HcalCaloFlagLabels::ADCSaturationBit,    mydef);
00073   else if (mybit == "UserDefinedBit0")      setAllRHMasks(HcalCaloFlagLabels::UserDefinedBit0,     mydef);
00074   else if (mybit == "UserDefinedBit1")      setAllRHMasks(HcalCaloFlagLabels::UserDefinedBit1,     mydef);
00075   else if (mybit == "UserDefinedBit2")      setAllRHMasks(HcalCaloFlagLabels::UserDefinedBit2,     mydef);
00076 
00077   // additional defined diagnostic bits; not currently used for rejection
00078   else if (mybit == "PresampleADC")         setAllRHMasks(HcalCaloFlagLabels::PresampleADC,     mydef);
00079   else if (mybit == "Fraction2TS")         setAllRHMasks(HcalCaloFlagLabels::Fraction2TS,     mydef); // should deprecate this at some point; it's been replaced by PresampleADC
00080 
00081 
00082 
00083   // unknown -------------------
00084   else
00085     {
00086       // error: unrecognized flag name
00087       edm::LogWarning  ("HcalSeverityLevelComputer") 
00088         << "HcalSeverityLevelComputer: Error: RecHitFlag >>" << mybit 
00089         << "<< unknown. Ignoring.";
00090       return false;
00091     }
00092   return true;
00093 }
00094 
00095 HcalSeverityLevelComputer::HcalSeverityLevelComputer( const edm::ParameterSet& iConfig)
00096 {
00097   // initialize: get the levels and masks from the cfg:
00098   typedef std::vector< edm::ParameterSet > myParameters;
00099   myParameters myLevels = iConfig.getParameter<myParameters>((std::string)"SeverityLevels");
00100 
00101   // now run through the parameter set vector:
00102   for ( myParameters::iterator itLevels = myLevels.begin(); itLevels != myLevels.end(); ++itLevels)
00103     {
00104       // create the basic object
00105       HcalSeverityDefinition mydef;
00106 
00107       // get the level:
00108       mydef.sevLevel = itLevels->getParameter<int>("Level");
00109 
00110       // get the RecHitFlags:
00111       std::vector<std::string> myRecHitFlags = 
00112         itLevels->getParameter<std::vector <std::string> > ("RecHitFlags");
00113 
00114       // get channel statuses:
00115       std::vector<std::string> myChStatuses = 
00116         itLevels->getParameter<std::vector <std::string> > ("ChannelStatus");
00117 
00118       // now translate the RecHitFlags and the ChannelStatuses into a mask each:
00119       // create counters for invalid flags to be able to catch cases where a definition consists only of invalid bit names:
00120       unsigned int bvalid = 0;
00121       unsigned int bnonempty = 0;      
00122       // channel status:
00123       for (unsigned k=0; k < myChStatuses.size(); k++)
00124         {
00125           if (myChStatuses[k].empty()) break; // empty string
00126           bnonempty++;
00127           bvalid+=getChStBit(mydef, myChStatuses[k]);
00128         }
00129       // RecHitFlag:
00130       //      HBHEStatusFlag, HOStatusFlag, HFStatusFlag, ZDCStatusFlag, CalibrationFlag
00131       for (unsigned k=0; k < myRecHitFlags.size(); k++)
00132         {
00133           if (myRecHitFlags[k].empty()) break; // empty string
00134           bnonempty++;
00135           bvalid+=getRecHitFlag(mydef, myRecHitFlags[k]);
00136         }
00137 
00138       //      std::cout << "Made Severity Level:" << std::endl;
00139       //      std::cout << mydef << std::endl;
00140 
00141       // case where definition is made entirely out of invalid flags but not empty strings
00142       if ((!bvalid) && (bnonempty)) 
00143         {
00144           edm::LogWarning ("HcalSeverityLevelComputer") 
00145             << "Warning: level " << mydef.sevLevel
00146             << " consists of invalid definitions only: "
00147             //      << myRecHitFlags << "; " << myChStatuses
00148             << " Ignoring definition.";
00149           continue;
00150         }
00151 
00152       // finally, append the masks to the mask vectors, sorting them according to level   
00153       std::vector<HcalSeverityDefinition>::iterator it = SevDef.begin();
00154 
00155       do
00156         {
00157           if (it == SevDef.end()) { SevDef.push_back(mydef); break; }
00158           
00159           if (it->sevLevel == mydef.sevLevel)
00160             {
00161               edm::LogWarning  ("HcalSeverityLevelComputer") 
00162                 << "HcalSeverityLevelComputer: Warning: level " << mydef.sevLevel 
00163                 << " already defined. Ignoring new definition.";
00164               break;
00165             }
00166 
00167           if (it->sevLevel < mydef.sevLevel) 
00168             if (( (it+1) == SevDef.end()) || ( (it+1)->sevLevel > mydef.sevLevel ))
00169               {
00170                 SevDef.insert(it+1, mydef);
00171                 break;
00172               }
00173 
00174           it++;
00175         }
00176       while(it != SevDef.end());
00177 
00178     } //for (myParameters::iterator itLevels=myLevels.begin(); itLevels != myLevels.end(); ++itLevels)
00179 
00180   edm::LogInfo("HcalSeverityLevelComputer") 
00181     << "HcalSeverityLevelComputer - Summary of Severity Levels:" << std::endl;
00182   for (std::vector<HcalSeverityDefinition>::iterator it = SevDef.begin(); it !=SevDef.end(); it++)
00183     {
00184       // debug: write the levels definitions on screen:
00185       edm::LogInfo("HcalSeverityLevelComputer") 
00186         << (*it) << std::endl;
00187     }
00188 
00189   //
00190   // Now make the definition for recoveredRecHit
00191   //
00192   std::vector<std::string> myRecovered = 
00193         iConfig.getParameter<std::vector <std::string> > ("RecoveredRecHitBits");
00194   RecoveredRecHit_ = new HcalSeverityDefinition();
00195   for (unsigned k=0; k < myRecovered.size(); k++)
00196     {
00197       if (myRecovered[k].empty()) break;
00198       getRecHitFlag( (*RecoveredRecHit_), myRecovered[k]);
00199     }
00200 
00201   //
00202   // Now make the definition for dropChannel
00203   //
00204   std::vector<std::string> myDrop = 
00205         iConfig.getParameter<std::vector <std::string> > ("DropChannelStatusBits");
00206   DropChannel_ = new HcalSeverityDefinition();
00207   for (unsigned k=0; k < myDrop.size(); k++)
00208     {
00209       if (myDrop[k].empty()) break;
00210       getChStBit( (*DropChannel_), myDrop[k]);
00211     }
00212 
00213   edm::LogInfo("HcalSeverityLevelComputer")
00214     << "HcalSeverityLevelComputer - Summary for Recovered RecHit bits: \n"
00215     << (*RecoveredRecHit_) << std::endl
00216     << "HcalSeverityLevelComputer - Summary for Drop the Channel bits: \n"
00217     << (*DropChannel_) << std::endl;
00218 
00219 
00220 } // HcalSeverityLevelComputer::HcalSeverityLevelComputer
00221 
00222 
00223 HcalSeverityLevelComputer::~HcalSeverityLevelComputer() {}
00224 
00225   
00226 int HcalSeverityLevelComputer::getSeverityLevel(const DetId& myid, const uint32_t& myflag, 
00227                                                 const uint32_t& mystatus) const
00228 {
00229   uint32_t myRecHitMask;
00230   HcalGenericDetId myId(myid);
00231   HcalGenericDetId::HcalGenericSubdetector mysubdet = myId.genericSubdet();
00232 
00233   // for (unsigned i=(SevDef.size()-1); i >= 0; i--) // Wrong
00234   // Since i is unsigned, i >= 0 is always true,
00235   // and the loop termination condition is never reached.
00236   // We offset the loop index by one to fix this.
00237   for (size_t j=(SevDef.size()); j > 0; j--)
00238     {
00239       size_t i = j - 1;
00240       switch (mysubdet)
00241         {
00242         case HcalGenericDetId::HcalGenBarrel : case HcalGenericDetId::HcalGenEndcap : 
00243           myRecHitMask = SevDef[i].HBHEFlagMask; break;
00244         case HcalGenericDetId::HcalGenOuter : myRecHitMask = SevDef[i].HOFlagMask; break;
00245         case HcalGenericDetId::HcalGenForward : myRecHitMask = SevDef[i].HFFlagMask; break;
00246         case HcalGenericDetId::HcalGenZDC : myRecHitMask = SevDef[i].ZDCFlagMask; break;
00247         case HcalGenericDetId::HcalGenCalibration : myRecHitMask = SevDef[i].CalibFlagMask; break;
00248         default: myRecHitMask = 0;
00249         }
00250  
00251       // for debugging:     
00252 //      std::cout << std::hex << " SLD: RHMask 0x" << myRecHitMask
00253 //              << " chstmask 0x" << SevDef[i].chStatusMask
00254 //              << " RHmask & myflag 0x" << (myRecHitMask&myflag)
00255 //              << " chstmask & mystatus 0x" << (SevDef[i].chStatusMask&mystatus)
00256 //              << std::dec << " level = " << SevDef[i].sevLevel << std::endl;
00257       
00258       // true if:
00259       // rechitmask empty and chstatusmask empty
00260       // rechitmask empty and chstatusmask&mychstat true
00261       // chstatusmask empty and rechitmask&myflag true
00262       // rechitmask&myflag true OR chstatusmask&mychstat true
00263 
00264 
00265       //      if ( ( ( (!myRecHitMask) || (myRecHitMask & myflag) ) &&
00266       if ( ( ( ( !SevDef[i].HBHEFlagMask 
00267                  && !SevDef[i].HOFlagMask 
00268                  && !SevDef[i].HFFlagMask 
00269                  && !SevDef[i].ZDCFlagMask 
00270                  && !SevDef[i].CalibFlagMask ) 
00271                || (myRecHitMask & myflag) ) 
00272              && ( (!SevDef[i].chStatusMask) || (SevDef[i].chStatusMask & mystatus) ) )
00273            || ( (myRecHitMask & myflag) || (SevDef[i].chStatusMask & mystatus) ) )
00274         return SevDef[i].sevLevel;
00275 
00276     }
00277 
00278   return -100;  // default value, if no definition applies
00279 }
00280   
00281 bool HcalSeverityLevelComputer::recoveredRecHit(const DetId& myid, const uint32_t& myflag) const
00282 {
00283   uint32_t myRecHitMask;
00284   HcalGenericDetId myId(myid);
00285   HcalGenericDetId::HcalGenericSubdetector mysubdet = myId.genericSubdet();
00286 
00287   switch (mysubdet)
00288     {
00289     case HcalGenericDetId::HcalGenBarrel : case HcalGenericDetId::HcalGenEndcap : 
00290       myRecHitMask = RecoveredRecHit_->HBHEFlagMask; break;
00291     case HcalGenericDetId::HcalGenOuter : myRecHitMask = RecoveredRecHit_->HOFlagMask; break;
00292     case HcalGenericDetId::HcalGenForward : myRecHitMask = RecoveredRecHit_->HFFlagMask; break;
00293     case HcalGenericDetId::HcalGenZDC : myRecHitMask = RecoveredRecHit_->ZDCFlagMask; break;
00294     case HcalGenericDetId::HcalGenCalibration : myRecHitMask = RecoveredRecHit_->CalibFlagMask; break;
00295     default: myRecHitMask = 0;
00296     }
00297 
00298   if (myRecHitMask & myflag) 
00299     return true;
00300 
00301   return false;
00302 }
00303 
00304 bool HcalSeverityLevelComputer::dropChannel(const uint32_t& mystatus) const
00305 {
00306   if (DropChannel_->chStatusMask & mystatus)
00307     return true;
00308 
00309   return false;
00310 }
00311 
00312 void HcalSeverityLevelComputer::setBit(const unsigned bitnumber, uint32_t& where) 
00313 {
00314   uint32_t statadd = 0x1<<(bitnumber);
00315   where = where|statadd;
00316 }
00317 
00318 void HcalSeverityLevelComputer::setAllRHMasks(const unsigned bitnumber, HcalSeverityDefinition& mydef)
00319 {
00320   setBit(bitnumber, mydef.HBHEFlagMask);
00321   setBit(bitnumber, mydef.HOFlagMask);
00322   setBit(bitnumber, mydef.HFFlagMask);
00323   setBit(bitnumber, mydef.ZDCFlagMask);
00324   setBit(bitnumber, mydef.CalibFlagMask);
00325 }
00326 
00327 std::ostream& operator<<(std::ostream& s, const HcalSeverityLevelComputer::HcalSeverityDefinition& def)
00328 {
00329   s << "Hcal Severity Level Definition, Level = " << def.sevLevel << std::endl;
00330   s << std::hex << std::showbase;
00331   s << "  channel status mask = " <<  def.chStatusMask << std::endl;
00332   s << "  HBHEFlagMask        = " <<  def.HBHEFlagMask << std::endl;
00333   s << "  HOFlagMask          = " <<  def.HOFlagMask << std::endl;
00334   s << "  HFFlagMask          = " <<  def.HFFlagMask << std::endl;
00335   s << "  ZDCFlagMask         = " <<  def.ZDCFlagMask << std::endl;
00336   s << "  CalibFlagMask       = " <<  def.CalibFlagMask << std::dec << std::noshowbase << std::endl;
00337   return s;
00338 }