CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/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.7 2012/08/28 14:18:14 yana 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   HcalTopologyMode::Mode mode = HcalTopologyMode::LHC;
00084   int maxDepthHB = 2;
00085   int maxDepthHE = 3;
00086   if( iConfig.exists( "hcalTopologyConstants" ))
00087   {
00088     const edm::ParameterSet hcalTopoConsts = iConfig.getParameter<edm::ParameterSet>( "hcalTopologyConstants" );
00089     StringToEnumParser<HcalTopologyMode::Mode> parser;
00090     mode = (HcalTopologyMode::Mode) parser.parseString(hcalTopoConsts.getParameter<std::string>("mode"));
00091     maxDepthHB = hcalTopoConsts.getParameter<int>("maxDepthHB");
00092     maxDepthHE = hcalTopoConsts.getParameter<int>("maxDepthHE");
00093   }
00094   
00095   theTrigTowerGeometry = new HcalTrigTowerGeometry( new HcalTopology( mode, maxDepthHB, maxDepthHE ));
00096 }
00097 
00098 
00099 L1CaloHcalScaleConfigOnlineProd::~L1CaloHcalScaleConfigOnlineProd()
00100 {
00101   // do anything here that needs to be done at desctruction time
00102   // (e.g. close files, deallocate resources etc.)
00103 
00104   if(caloTPG != 0)
00105     delete caloTPG;
00106 }
00107 
00108 boost::shared_ptr< L1CaloHcalScale >
00109 L1CaloHcalScaleConfigOnlineProd::newObject( const std::string& objectKey )
00110 {
00111      using namespace edm::es;
00112  
00113      std:: cout << "object Key " << objectKey <<std::endl <<std::flush;
00114 
00115      if(objectKey == "NULL" || objectKey == "")  // return default blank ecal scale      
00116        return boost::shared_ptr< L1CaloHcalScale >( hcalScale );
00117      if(objectKey == "IDENTITY"){  // return identity ecal scale  
00118        
00119        delete hcalScale;
00120        
00121        hcalScale = new L1CaloHcalScale(1);
00122        
00123        return boost::shared_ptr< L1CaloHcalScale >( hcalScale);
00124      }
00125   
00126      std::vector<unsigned int> analyticalLUT(1024, 0);
00127      std::vector<unsigned int> identityLUT(1024, 0);
00128      
00129      // Compute compression LUT
00130      for (unsigned int i=0; i < 1024; i++) {
00131        analyticalLUT[i] = (unsigned int)(sqrt(14.94*log(1.+i/14.94)*i) + 0.5);
00132        identityLUT[i] = std::min(i,0xffu);
00133      }
00134      
00135      hcaluncomp.clear();
00136      for (int i = 0; i < 4176; i++){
00137        RCTdecompression decompressionTable(256,0);
00138        hcaluncomp.push_back(decompressionTable);
00139     }
00140      
00141 
00142 
00143      std::vector < std::string > mainStrings;
00144      mainStrings.push_back("HCAL_LUT_METADATA");
00145      mainStrings.push_back("HCAL_LUT_CHAN_DATA");
00146 
00147      // ~~~~~~~~~ Cut values ~~~~~~~~~
00148 
00149  
00150      std::vector< std::string > metaStrings ;
00151      metaStrings.push_back("RCTLSB");  
00152      metaStrings.push_back("NOMINAL_GAIN");  
00153    
00154  
00155     l1t::OMDSReader::QueryResults paramResults =
00156        m_omdsReader.basicQueryView( metaStrings,
00157                                 "CMS_HCL_HCAL_COND",
00158                                 "V_HCAL_LUT_METADATA_V1",
00159                                 "V_HCAL_LUT_METADATA_V1.TAG_NAME",
00160                                 m_omdsReader.basicQuery(
00161                                                         "HCAL_LUT_METADATA",
00162                                                         "CMS_RCT",
00163                                                         "HCAL_SCALE_KEY",
00164                                                         "HCAL_SCALE_KEY.HCAL_TAG",
00165                                                         m_omdsReader.singleAttribute(objectKey)));
00166     
00167 
00168     
00169     
00170     if( paramResults.queryFailed()
00171         || (paramResults.numberRows()!=1) ) // check query successful
00172        {
00173          edm::LogError( "L1-O2O" ) << "Problem with L1CaloHcalScale key.  Unable to find lutparam dat table" ;
00174          return boost::shared_ptr< L1CaloHcalScale >() ;
00175        }
00176         
00177     double hcalLSB, nominal_gain;
00178     paramResults.fillVariable("RCTLSB",hcalLSB);    
00179     paramResults.fillVariable("NOMINAL_GAIN",nominal_gain);
00180  
00181     float    rctlsb = hcalLSB;
00182 
00183 
00184 
00185     l1t::OMDSReader::QueryResults chanKey =m_omdsReader.basicQuery(
00186                                                                   "HCAL_LUT_CHAN_DATA",
00187                                                                   "CMS_RCT",
00188                                                                   "HCAL_SCALE_KEY",
00189                                                                   "HCAL_SCALE_KEY.HCAL_TAG",
00190                                                                   m_omdsReader.singleAttribute(objectKey));
00191       
00192     //coral::AttributeList myresult;
00193     //    myresult.extend(
00194     
00195  
00196     std::string schemaName("CMS_HCL_HCAL_COND");
00197     coral::ISchema& schema = m_omdsReader.dbSession()->schema( schemaName ) ;
00198     coral::IQuery* query = schema.newQuery(); ;
00199 
00200      
00201     std::vector< std::string > channelStrings;
00202     channelStrings.push_back("IPHI");
00203     channelStrings.push_back("IETA");
00204     channelStrings.push_back("LUT_GRANULARITY");
00205     channelStrings.push_back("OUTPUT_LUT_THRESHOLD");
00206     channelStrings.push_back("OBJECTNAME");
00207 
00208                  
00209    
00210      std::vector< std::string >::const_iterator it = channelStrings.begin() ;
00211      std::vector< std::string >::const_iterator end = channelStrings.end() ;
00212      for( ; it != end ; ++it )
00213        {
00214          query->addToOutputList( *it ) ;
00215        }
00216 
00217     std::string ob = "OBJECTNAME";
00218     coral::AttributeList myresult; 
00219     myresult.extend("IPHI", typeid(int)); 
00220     myresult.extend("IETA", typeid(int)); 
00221     myresult.extend("LUT_GRANULARITY", typeid(int)); 
00222     myresult.extend("OUTPUT_LUT_THRESHOLD", typeid(int)); 
00223     myresult.extend( ob,typeid(std::string));//, typeid(std::string)); 
00224 
00225     query->defineOutput( myresult ); 
00226 
00227     query->addToTableList( "V_HCAL_LUT_CHAN_DATA_V1");
00228 
00229     query->setCondition(
00230                         "V_HCAL_LUT_CHAN_DATA_V1.TAG_NAME = :" + chanKey.columnNames().front(),
00231                         chanKey.attributeLists().front());
00232 
00233     coral::ICursor& cursor = query->execute();
00234 
00235  // when the query goes out of scope.
00236     std::vector<coral::AttributeList> atts;
00237     while (cursor.next()) {
00238         atts.push_back(cursor.currentRow());
00239     };
00240 
00241     delete query;
00242 
00243     l1t::OMDSReader::QueryResults chanResults(channelStrings,atts); 
00244     if( chanResults.queryFailed()
00245         || (chanResults.numberRows()==0) ) // check query successful
00246        {
00247          edm::LogError( "L1-O2O" ) << "Problem with L1CaloHcalScale key.  Unable to find lutparam dat table nrows" << chanResults.numberRows() ;
00248          return boost::shared_ptr< L1CaloHcalScale >() ;
00249        }
00250 
00251 
00252 
00253     chanResults.attributeLists();
00254      for(int i = 0; i < chanResults.numberRows() ; ++i){
00255        std::string objectName;
00256        chanResults.fillVariableFromRow("OBJECTNAME",i, objectName);
00257        //       int
00258        if(objectName == "HcalTrigTowerDetId") { //trig tower
00259          int ieta, iphi, lutGranularity, threshold;
00260          
00261          
00262          chanResults.fillVariableFromRow("LUT_GRANULARITY",i,lutGranularity);
00263          chanResults.fillVariableFromRow("IPHI",i,iphi);
00264          chanResults.fillVariableFromRow("IETA",i,ieta);
00265          chanResults.fillVariableFromRow("OUTPUT_LUT_THRESHOLD",i,threshold);
00266          
00267 
00268          unsigned int outputLut[1024];
00269 
00270          uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00271 
00272          double eta_low = 0., eta_high = 0.;
00273          theTrigTowerGeometry->towerEtaBounds(ieta,eta_low,eta_high); 
00274          double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
00275 
00276 
00277          if (!caloTPG->HTvalid(ieta, iphi)) continue;
00278          double factor = 0.;
00279          if (abs(ieta) >= theTrigTowerGeometry->firstHFTower())
00280            factor = rctlsb;
00281          else 
00282            factor = nominal_gain / cosh_ieta * lutGranularity;
00283          for (int k = 0; k < threshold; ++k)
00284            outputLut[k] = 0;
00285          
00286          for (unsigned int k = threshold; k < 1024; ++k)
00287            outputLut[k] = (abs(ieta) < theTrigTowerGeometry->firstHFTower()) ? analyticalLUT[k] : identityLUT[k];
00288          
00289 
00290            // tpg - compressed value
00291            unsigned int tpg = outputLut[0];
00292           
00293            int low = 0;
00294 
00295           for (unsigned int k = 0; k < 1024; ++k){
00296              if (outputLut[k] != tpg){
00297                 unsigned int mid = (low + k)/2;
00298                 hcaluncomp[lutId][tpg] = (tpg == 0 ? low : factor * mid);
00299                 low = k;
00300                 tpg = outputLut[k];
00301              }
00302           }
00303           hcaluncomp[lutId][tpg] = factor * low;
00304        }
00305      }
00306      
00307 
00308 
00309      for( unsigned short ieta = 1 ; ieta <= L1CaloHcalScale::nBinEta; ++ieta ){
00310        for(int pos = 0; pos <=1; pos++){
00311          for( unsigned short irank = 0 ; irank < L1CaloHcalScale::nBinRank; ++irank ){
00312      
00313            
00314            
00315            int zside = (int)  pow(-1,pos);
00316            int nphi = 0;
00317            double etvalue = 0.;
00318            
00319 
00320            for(int iphi = 1; iphi<=72; iphi++){
00321              if(!caloTPG->HTvalid(ieta, iphi))
00322                continue;
00323              uint32_t lutId = caloTPG->getOutputLUTId(ieta,iphi);
00324              nphi++;
00325              etvalue += (double) hcaluncomp[lutId][irank];
00326 
00327            } // phi
00328            if (nphi > 0) etvalue /= nphi;
00329            
00330            hcalScale->setBin(irank, ieta, zside, etvalue);
00331 
00332          } // rank
00333        } // zside
00334      }// eta
00335 
00336      std::cout << std::setprecision(10);
00337      hcalScale->print(std::cout);
00338 // ------------ method called to produce the data  ------------
00339      return boost::shared_ptr< L1CaloHcalScale >( hcalScale );
00340 
00341 }
00342 //define this as a plug-in
00343 DEFINE_FWK_EVENTSETUP_MODULE(L1CaloHcalScaleConfigOnlineProd);