CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/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.6 2010/12/24 18:20:29 efron 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;
00175 
00176 
00177 
00178     l1t::OMDSReader::QueryResults chanKey =m_omdsReader.basicQuery(
00179                                                                   "HCAL_LUT_CHAN_DATA",
00180                                                                   "CMS_RCT",
00181                                                                   "HCAL_SCALE_KEY",
00182                                                                   "HCAL_SCALE_KEY.HCAL_TAG",
00183                                                                   m_omdsReader.singleAttribute(objectKey));
00184       
00185     //coral::AttributeList myresult;
00186     //    myresult.extend(
00187     
00188  
00189     std::string schemaName("CMS_HCL_HCAL_COND");
00190     coral::ISchema& schema = m_omdsReader.dbSession()->schema( schemaName ) ;
00191     coral::IQuery* query = schema.newQuery(); ;
00192 
00193      
00194     std::vector< std::string > channelStrings;
00195     channelStrings.push_back("IPHI");
00196     channelStrings.push_back("IETA");
00197     channelStrings.push_back("LUT_GRANULARITY");
00198     channelStrings.push_back("OUTPUT_LUT_THRESHOLD");
00199     channelStrings.push_back("OBJECTNAME");
00200 
00201                  
00202    
00203      std::vector< std::string >::const_iterator it = channelStrings.begin() ;
00204      std::vector< std::string >::const_iterator end = channelStrings.end() ;
00205      for( ; it != end ; ++it )
00206        {
00207          query->addToOutputList( *it ) ;
00208        }
00209 
00210     std::string ob = "OBJECTNAME";
00211     coral::AttributeList myresult; 
00212     myresult.extend("IPHI", typeid(int)); 
00213     myresult.extend("IETA", typeid(int)); 
00214     myresult.extend("LUT_GRANULARITY", typeid(int)); 
00215     myresult.extend("OUTPUT_LUT_THRESHOLD", typeid(int)); 
00216     myresult.extend( ob,typeid(std::string));//, typeid(std::string)); 
00217 
00218     query->defineOutput( myresult ); 
00219 
00220     query->addToTableList( "V_HCAL_LUT_CHAN_DATA_V1");
00221 
00222     query->setCondition(
00223                         "V_HCAL_LUT_CHAN_DATA_V1.TAG_NAME = :" + chanKey.columnNames().front(),
00224                         chanKey.attributeLists().front());
00225 
00226     coral::ICursor& cursor = query->execute();
00227 
00228  // when the query goes out of scope.
00229     std::vector<coral::AttributeList> atts;
00230     while (cursor.next()) {
00231         atts.push_back(cursor.currentRow());
00232     };
00233 
00234     delete query;
00235 
00236     l1t::OMDSReader::QueryResults chanResults(channelStrings,atts); 
00237     if( chanResults.queryFailed()
00238         || (chanResults.numberRows()==0) ) // check query successful
00239        {
00240          edm::LogError( "L1-O2O" ) << "Problem with L1CaloHcalScale key.  Unable to find lutparam dat table nrows" << chanResults.numberRows() ;
00241          return boost::shared_ptr< L1CaloHcalScale >() ;
00242        }
00243 
00244 
00245 
00246     chanResults.attributeLists();
00247      for(int i = 0; i < chanResults.numberRows() ; ++i){
00248        std::string objectName;
00249        chanResults.fillVariableFromRow("OBJECTNAME",i, objectName);
00250        //       int
00251        if(objectName == "HcalTrigTowerDetId") { //trig tower
00252          int ieta, iphi, lutGranularity, threshold;
00253          
00254          
00255          chanResults.fillVariableFromRow("LUT_GRANULARITY",i,lutGranularity);
00256          chanResults.fillVariableFromRow("IPHI",i,iphi);
00257          chanResults.fillVariableFromRow("IETA",i,ieta);
00258          chanResults.fillVariableFromRow("OUTPUT_LUT_THRESHOLD",i,threshold);
00259          
00260 
00261          unsigned int outputLut[1024];
00262 
00263          uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00264 
00265          double eta_low = 0., eta_high = 0.;
00266          theTrigTowerGeometry.towerEtaBounds(ieta,eta_low,eta_high); 
00267          double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
00268 
00269 
00270          if (!caloTPG->HTvalid(ieta, iphi)) continue;
00271          double factor = 0.;
00272          if (abs(ieta) >= theTrigTowerGeometry.firstHFTower())
00273            factor = rctlsb;
00274          else 
00275            factor = nominal_gain / cosh_ieta * lutGranularity;
00276          for (int k = 0; k < threshold; ++k)
00277            outputLut[k] = 0;
00278          
00279          for (unsigned int k = threshold; k < 1024; ++k)
00280            outputLut[k] = (abs(ieta) < theTrigTowerGeometry.firstHFTower()) ? analyticalLUT[k] : identityLUT[k];
00281          
00282 
00283            // tpg - compressed value
00284            unsigned int tpg = outputLut[0];
00285           
00286            int low = 0;
00287 
00288           for (unsigned int k = 0; k < 1024; ++k){
00289              if (outputLut[k] != tpg){
00290                 unsigned int mid = (low + k)/2;
00291                 hcaluncomp[lutId][tpg] = (tpg == 0 ? low : factor * mid);
00292                 low = k;
00293                 tpg = outputLut[k];
00294              }
00295           }
00296           hcaluncomp[lutId][tpg] = factor * low;
00297        }
00298      }
00299      
00300 
00301 
00302      for( unsigned short ieta = 1 ; ieta <= L1CaloHcalScale::nBinEta; ++ieta ){
00303        for(int pos = 0; pos <=1; pos++){
00304          for( unsigned short irank = 0 ; irank < L1CaloHcalScale::nBinRank; ++irank ){
00305      
00306            
00307            
00308            int zside = (int)  pow(-1,pos);
00309            int nphi = 0;
00310            double etvalue = 0.;
00311            
00312 
00313            for(int iphi = 1; iphi<=72; iphi++){
00314              if(!caloTPG->HTvalid(ieta, iphi))
00315                continue;
00316              uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00317              nphi++;
00318              etvalue += (double) hcaluncomp[lutId][irank];
00319 
00320            } // phi
00321            if (nphi > 0) etvalue /= nphi;
00322            
00323            hcalScale->setBin(irank, ieta, zside, etvalue);
00324 
00325          } // rank
00326        } // zside
00327      }// eta
00328 
00329      std::cout << std::setprecision(10);
00330      hcalScale->print(std::cout);
00331 // ------------ method called to produce the data  ------------
00332      return boost::shared_ptr< L1CaloHcalScale >( hcalScale );
00333 
00334 }
00335 //define this as a plug-in
00336 DEFINE_FWK_EVENTSETUP_MODULE(L1CaloHcalScaleConfigOnlineProd);