CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/L1TriggerConfig/L1ScalesProducers/src/L1CaloHcalScaleConfigOnlineProd.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    L1EmEtScaleOnlineProd
00004 // Class:      L1EmEtScaleOnlineProd
00005 // 
00013 //
00014 // Original Author:  Werner Man-Li Sun
00015 //         Created:  Tue Sep 16 22:43:22 CEST 2008
00016 // $Id: L1CaloHcalScaleConfigOnlineProd.cc,v 1.5 2010/07/20 04:16:38 wmtan Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 
00023 // user include files
00024 #include "CondTools/L1Trigger/interface/L1ConfigOnlineProdBase.h"
00025 #include "CondFormats/L1TObjects/interface/L1CaloHcalScale.h"
00026 #include "CondFormats/DataRecord/interface/L1CaloHcalScaleRcd.h"
00027 #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
00028 #include "CalibCalorimetry/CaloTPG/src/CaloTPGTranscoderULUT.h"
00029 #include "CondTools/L1Trigger/interface/OMDSReader.h"
00030 
00031 #include <cmath>
00032 #include <iostream>
00033 #include <iomanip>
00034 
00035 //
00036 // class declaration
00037 //
00038 
00039 class L1CaloHcalScaleConfigOnlineProd :
00040   public L1ConfigOnlineProdBase< L1CaloHcalScaleRcd, L1CaloHcalScale > {
00041    public:
00042       L1CaloHcalScaleConfigOnlineProd(const edm::ParameterSet& iConfig);
00043       ~L1CaloHcalScaleConfigOnlineProd();
00044 
00045   virtual boost::shared_ptr< L1CaloHcalScale > newObject(
00046     const std::string& objectKey ) ;
00047 
00048 
00049    private:
00050  
00051   L1CaloHcalScale* hcalScale;
00052   HcalTrigTowerGeometry theTrigTowerGeometry;
00053   CaloTPGTranscoderULUT* caloTPG;
00054   typedef std::vector<double> RCTdecompression;
00055   std::vector<RCTdecompression> hcaluncomp;
00056 
00057   //  HcaluLUTTPGCoder* tpgCoder;// = new        HcaluLUTTPGCoder();
00058 
00059 
00060   HcalTrigTowerDetId* ttDetId;
00061 
00062       // ----------member data ---------------------------
00063 };
00064 
00065 //
00066 // constants, enums and typedefs
00067 //
00068 
00069 //
00070 // static data member definitions
00071 //
00072 
00073 //
00074 // constructors and destructor
00075 //
00076 L1CaloHcalScaleConfigOnlineProd::L1CaloHcalScaleConfigOnlineProd(
00077   const edm::ParameterSet& iConfig)
00078   : L1ConfigOnlineProdBase< L1CaloHcalScaleRcd, L1CaloHcalScale >( iConfig )
00079 {
00080   hcalScale = new L1CaloHcalScale(0);
00081   caloTPG = new CaloTPGTranscoderULUT();
00082 }
00083 
00084 
00085 L1CaloHcalScaleConfigOnlineProd::~L1CaloHcalScaleConfigOnlineProd()
00086 {
00087  
00088    // do anything here that needs to be done at desctruction time
00089    // (e.g. close files, deallocate resources etc.)
00090 
00091   if(caloTPG != 0)
00092     delete caloTPG;
00093 
00094 
00095 
00096 
00097 
00098 
00099 }
00100 
00101 boost::shared_ptr< L1CaloHcalScale >
00102 L1CaloHcalScaleConfigOnlineProd::newObject( const std::string& objectKey )
00103 {
00104      using namespace edm::es;
00105  
00106      std:: cout << "object Key " << objectKey <<std::endl <<std::flush;
00107 
00108      if(objectKey == "NULL" || objectKey == "")  // return default blank ecal scale      
00109        return boost::shared_ptr< L1CaloHcalScale >( hcalScale );
00110      if(objectKey == "IDENTITY"){  // return identity ecal scale  
00111        
00112        delete hcalScale;
00113        
00114        hcalScale = new L1CaloHcalScale(1);
00115        
00116        return boost::shared_ptr< L1CaloHcalScale >( hcalScale);
00117      }
00118   
00119      std::vector<unsigned int> analyticalLUT(1024, 0);
00120      std::vector<unsigned int> identityLUT(1024, 0);
00121      
00122      // Compute compression LUT
00123      for (unsigned int i=0; i < 1024; i++) {
00124        analyticalLUT[i] = (unsigned int)(sqrt(14.94*log(1.+i/14.94)*i) + 0.5);
00125        identityLUT[i] = std::min(i,0xffu);
00126      }
00127      
00128      hcaluncomp.clear();
00129      for (int i = 0; i < 4176; i++){
00130        RCTdecompression decompressionTable(256,0);
00131        hcaluncomp.push_back(decompressionTable);
00132     }
00133      
00134 
00135 
00136      std::vector < std::string > mainStrings;
00137      mainStrings.push_back("HCAL_LUT_METADATA");
00138      mainStrings.push_back("HCAL_LUT_CHAN_DATA");
00139 
00140      // ~~~~~~~~~ Cut values ~~~~~~~~~
00141 
00142  
00143      std::vector< std::string > metaStrings ;
00144      metaStrings.push_back("RCTLSB");  
00145      metaStrings.push_back("NOMINAL_GAIN");  
00146    
00147  
00148     l1t::OMDSReader::QueryResults paramResults =
00149        m_omdsReader.basicQueryView( metaStrings,
00150                                 "CMS_HCL_HCAL_COND",
00151                                 "V_HCAL_LUT_METADATA_V1",
00152                                 "V_HCAL_LUT_METADATA_V1.TAG_NAME",
00153                                 m_omdsReader.basicQuery(
00154                                                         "HCAL_LUT_METADATA",
00155                                                         "CMS_RCT",
00156                                                         "HCAL_SCALE_KEY",
00157                                                         "HCAL_SCALE_KEY.HCAL_TAG",
00158                                                         m_omdsReader.singleAttribute(objectKey)));
00159     
00160 
00161     
00162     
00163     if( paramResults.queryFailed()
00164         || (paramResults.numberRows()!=1) ) // check query successful
00165        {
00166          edm::LogError( "L1-O2O" ) << "Problem with L1CaloHcalScale key.  Unable to find lutparam dat table" ;
00167          return boost::shared_ptr< L1CaloHcalScale >() ;
00168        }
00169         
00170     double hcalLSB, nominal_gain;
00171     paramResults.fillVariable("RCTLSB",hcalLSB);    
00172     paramResults.fillVariable("NOMINAL_GAIN",nominal_gain);
00173  
00174         float    rctlsb = hcalLSB == 0.25 ? 1./4 : 1./8;
00175 
00176 
00177     l1t::OMDSReader::QueryResults chanKey =m_omdsReader.basicQuery(
00178                                                                   "HCAL_LUT_CHAN_DATA",
00179                                                                   "CMS_RCT",
00180                                                                   "HCAL_SCALE_KEY",
00181                                                                   "HCAL_SCALE_KEY.HCAL_TAG",
00182                                                                   m_omdsReader.singleAttribute(objectKey));
00183       
00184     //coral::AttributeList myresult;
00185     //    myresult.extend(
00186     
00187  
00188     std::string schemaName("CMS_HCL_HCAL_COND");
00189     coral::ISchema& schema = m_omdsReader.dbSession()->schema( schemaName ) ;
00190     coral::IQuery* query = schema.newQuery(); ;
00191 
00192      
00193     std::vector< std::string > channelStrings;
00194     channelStrings.push_back("IPHI");
00195     channelStrings.push_back("IETA");
00196     channelStrings.push_back("LUT_GRANULARITY");
00197     channelStrings.push_back("OUTPUT_LUT_THRESHOLD");
00198     channelStrings.push_back("OBJECTNAME");
00199 
00200                  
00201    
00202      std::vector< std::string >::const_iterator it = channelStrings.begin() ;
00203      std::vector< std::string >::const_iterator end = channelStrings.end() ;
00204      for( ; it != end ; ++it )
00205        {
00206          query->addToOutputList( *it ) ;
00207        }
00208 
00209     std::string ob = "OBJECTNAME";
00210     coral::AttributeList myresult; 
00211     myresult.extend("IPHI", typeid(int)); 
00212     myresult.extend("IETA", typeid(int)); 
00213     myresult.extend("LUT_GRANULARITY", typeid(int)); 
00214     myresult.extend("OUTPUT_LUT_THRESHOLD", typeid(int)); 
00215     myresult.extend( ob,typeid(std::string));//, typeid(std::string)); 
00216 
00217     query->defineOutput( myresult ); 
00218 
00219     query->addToTableList( "V_HCAL_LUT_CHAN_DATA_V1");
00220 
00221     query->setCondition(
00222                         "V_HCAL_LUT_CHAN_DATA_V1.TAG_NAME = :" + chanKey.columnNames().front(),
00223                         chanKey.attributeLists().front());
00224 
00225     coral::ICursor& cursor = query->execute();
00226 
00227  // when the query goes out of scope.
00228     std::vector<coral::AttributeList> atts;
00229     while (cursor.next()) {
00230         atts.push_back(cursor.currentRow());
00231     };
00232 
00233     delete query;
00234 
00235     l1t::OMDSReader::QueryResults chanResults(channelStrings,atts); 
00236     if( chanResults.queryFailed()
00237         || (chanResults.numberRows()==0) ) // check query successful
00238        {
00239          edm::LogError( "L1-O2O" ) << "Problem with L1CaloHcalScale key.  Unable to find lutparam dat table nrows" << chanResults.numberRows() ;
00240          return boost::shared_ptr< L1CaloHcalScale >() ;
00241        }
00242 
00243 
00244 
00245     chanResults.attributeLists();
00246      for(int i = 0; i < chanResults.numberRows() ; ++i){
00247        std::string objectName;
00248        chanResults.fillVariableFromRow("OBJECTNAME",i, objectName);
00249        //       int
00250        if(objectName == "HcalTrigTowerDetId") { //trig tower
00251          int ieta, iphi, lutGranularity, threshold;
00252          
00253          
00254          chanResults.fillVariableFromRow("LUT_GRANULARITY",i,lutGranularity);
00255          chanResults.fillVariableFromRow("IPHI",i,iphi);
00256          chanResults.fillVariableFromRow("IETA",i,ieta);
00257          chanResults.fillVariableFromRow("OUTPUT_LUT_THRESHOLD",i,threshold);
00258          
00259 
00260          unsigned int outputLut[1024];
00261 
00262          uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00263 
00264          double eta_low = 0., eta_high = 0.;
00265          theTrigTowerGeometry.towerEtaBounds(ieta,eta_low,eta_high); 
00266          double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
00267 
00268 
00269          if (!caloTPG->HTvalid(ieta, iphi)) continue;
00270          double factor = 0.;
00271          if (abs(ieta) >= theTrigTowerGeometry.firstHFTower())
00272            factor = rctlsb;
00273          else 
00274            factor = nominal_gain / cosh_ieta * lutGranularity;
00275          for (int k = 0; k < threshold; ++k)
00276            outputLut[k] = 0;
00277          
00278          for (unsigned int k = threshold; k < 1024; ++k)
00279            outputLut[k] = (abs(ieta) < theTrigTowerGeometry.firstHFTower()) ? analyticalLUT[k] : identityLUT[k];
00280          
00281 
00282            // tpg - compressed value
00283            unsigned int tpg = outputLut[0];
00284           
00285            int low = 0;
00286 
00287           for (unsigned int k = 0; k < 1024; ++k){
00288              if (outputLut[k] != tpg){
00289                 unsigned int mid = (low + k)/2;
00290                 hcaluncomp[lutId][tpg] = (tpg == 0 ? low : factor * mid);
00291                 low = k;
00292                 tpg = outputLut[k];
00293              }
00294           }
00295           hcaluncomp[lutId][tpg] = factor * low;
00296        }
00297      }
00298      
00299 
00300 
00301      for( unsigned short ieta = 1 ; ieta <= L1CaloHcalScale::nBinEta; ++ieta ){
00302        for(int pos = 0; pos <=1; pos++){
00303          for( unsigned short irank = 0 ; irank < L1CaloHcalScale::nBinRank; ++irank ){
00304      
00305            
00306            
00307            int zside = (int)  pow(-1,pos);
00308            int nphi = 0;
00309            double etvalue = 0.;
00310            
00311 
00312            for(int iphi = 1; iphi<=72; iphi++){
00313              if(!caloTPG->HTvalid(ieta, iphi))
00314                continue;
00315              uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00316              nphi++;
00317              etvalue += (double) hcaluncomp[lutId][irank];
00318 
00319            } // phi
00320            if (nphi > 0) etvalue /= nphi;
00321            
00322            hcalScale->setBin(irank, ieta, zside, etvalue);
00323 
00324          } // rank
00325        } // zside
00326      }// eta
00327 
00328      std::cout << std::setprecision(10);
00329      hcalScale->print(std::cout);
00330 // ------------ method called to produce the data  ------------
00331      return boost::shared_ptr< L1CaloHcalScale >( hcalScale );
00332 
00333 }
00334 //define this as a plug-in
00335 DEFINE_FWK_EVENTSETUP_MODULE(L1CaloHcalScaleConfigOnlineProd);