CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CalibCalorimetry/EcalTrivialCondModules/src/ESTrivialConditionRetriever.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004 
00005 
00006 #include "CalibCalorimetry/EcalTrivialCondModules/interface/ESTrivialConditionRetriever.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 
00010 #include "DataFormats/EcalDetId/interface/ESDetId.h"
00011 
00012 //#include "DataFormats/Provenance/interface/Timestamp.h"
00013 
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 
00017 using namespace edm;
00018 
00019 ESTrivialConditionRetriever::ESTrivialConditionRetriever( const edm::ParameterSet&  ps)
00020 {
00021 
00022   // initilize parameters used to produce cond DB objects
00023   adcToGeVLowConstant_ = ps.getUntrackedParameter<double>("adcToGeVLowConstant",1.0);
00024   adcToGeVHighConstant_ = ps.getUntrackedParameter<double>("adcToGeVHighConstant",1.0);
00025 
00026   intercalibConstantMean_ = ps.getUntrackedParameter<double>("intercalibConstantMean",1.0);
00027   intercalibConstantSigma_ = ps.getUntrackedParameter<double>("intercalibConstantSigma",0.0);
00028 
00029   //  intercalibErrorMean_ = ps.getUntrackedParameter<double>("IntercalibErrorMean",0.0);
00030 
00031   ESpedMean_ = ps.getUntrackedParameter<double>("ESpedMean", 200.);
00032   ESpedRMS_  = ps.getUntrackedParameter<double>("ESpedRMS",  1.00);
00033 
00034   getWeightsFromFile_ = ps.getUntrackedParameter<bool>("getWeightsFromFile",false);
00035 
00036 
00037   std::string path="CalibCalorimetry/EcalTrivialCondModules/data/";
00038   std::string weightType;
00039   std::ostringstream str;
00040 
00041   weightType = str.str();
00042 
00043   amplWeightsFile_ = ps.getUntrackedParameter<std::string>("amplWeightsFile",path+"ampWeightsES"+weightType);
00044 
00045   // default weights for MGPA shape after pedestal subtraction
00046   getWeightsFromConfiguration(ps);
00047 
00048   producedESPedestals_ = ps.getUntrackedParameter<bool>("producedESPedestals",true);
00049   producedESWeights_ = ps.getUntrackedParameter<bool>("producedESWeights",true);
00050 
00051   producedESADCToGeVConstant_ = ps.getUntrackedParameter<bool>("producedESADCToGeVConstant",true);
00052 
00053   verbose_ = ps.getUntrackedParameter<int>("verbose", 0);
00054 
00055   //Tell Producer what we produce
00056   //setWhatproduce(this);
00057   if (producedESPedestals_)
00058     setWhatProduced(this, &ESTrivialConditionRetriever::produceESPedestals );
00059 
00060   if (producedESWeights_) {
00061       setWhatProduced(this, &ESTrivialConditionRetriever::produceESWeightStripGroups );
00062       setWhatProduced(this, &ESTrivialConditionRetriever::produceESTBWeights );
00063     }
00064 
00065   if (producedESADCToGeVConstant_)
00066     setWhatProduced(this, &ESTrivialConditionRetriever::produceESADCToGeVConstant );
00067 
00068   // intercalibration constants
00069   producedESIntercalibConstants_ = ps.getUntrackedParameter<bool>("producedESIntercalibConstants",true);
00070   intercalibConstantsFile_ = ps.getUntrackedParameter<std::string>("intercalibConstantsFile","") ;
00071 
00072   if (producedESIntercalibConstants_) { // user asks to produce constants
00073     if(intercalibConstantsFile_ == "") {  // if file provided read constants
00074       //    setWhatProduced (this, &ESTrivialConditionRetriever::getIntercalibConstantsFromConfiguration ) ;
00075       //  } else { // set all constants to 1. or smear as specified by user
00076         setWhatProduced (this, &ESTrivialConditionRetriever::produceESIntercalibConstants ) ;
00077     }
00078     findingRecord<ESIntercalibConstantsRcd> () ;
00079   }
00080 
00081   // intercalibration constants errors
00082   /*  producedESIntercalibErrors_ = ps.getUntrackedParameter<bool>("producedESIntercalibErrors",true);
00083       intercalibErrorsFile_ = ps.getUntrackedParameter<std::string>("intercalibErrorsFile","") ;
00084       
00085       if (producedESIntercalibErrors_) { // user asks to produce constants
00086       if(intercalibErrorsFile_ != "") {  // if file provided read constants
00087       setWhatProduced (this, &ESTrivialConditionRetriever::getIntercalibErrorsFromConfiguration ) ;
00088       } else { // set all constants to 1. or smear as specified by user
00089       setWhatProduced (this, &ESTrivialConditionRetriever::produceESIntercalibErrors ) ;
00090       }
00091       findingRecord<ESIntercalibErrorsRcd> () ;
00092       }
00093   */
00094 
00095   // channel status
00096   producedESChannelStatus_ = ps.getUntrackedParameter<bool>("producedESChannelStatus",true);
00097   channelStatusFile_ = ps.getUntrackedParameter<std::string>("channelStatusFile","");
00098 
00099   if ( producedESChannelStatus_ ) {
00100           if ( channelStatusFile_ != "" ) { // if file provided read channel map
00101                   setWhatProduced( this, &ESTrivialConditionRetriever::getChannelStatusFromConfiguration );
00102           } else { // set all channels to working -- FIXME might be changed
00103                   setWhatProduced( this, &ESTrivialConditionRetriever::produceESChannelStatus );
00104           }
00105           findingRecord<ESChannelStatusRcd>();
00106   }
00107 
00108   //Tell Finder what records we find
00109   if (producedESPedestals_)  findingRecord<ESPedestalsRcd>();
00110 
00111   if (producedESWeights_) {
00112       findingRecord<ESWeightStripGroupsRcd>();
00113       findingRecord<ESTBWeightsRcd>();
00114     }
00115 
00116   if (producedESADCToGeVConstant_)  findingRecord<ESADCToGeVConstantRcd>();
00117 
00118 }
00119 
00120 ESTrivialConditionRetriever::~ESTrivialConditionRetriever()
00121 {
00122 }
00123 
00124 //
00125 // member functions
00126 //
00127 void
00128 ESTrivialConditionRetriever::setIntervalFor( const edm::eventsetup::EventSetupRecordKey& rk,
00129                                                const edm::IOVSyncValue& iTime,
00130                                                edm::ValidityInterval& oValidity)
00131 {
00132   if(verbose_>=1) std::cout << "ESTrivialConditionRetriever::setIntervalFor(): record key = " << rk.name() << "\ttime: " << iTime.time().value() << std::endl;
00133   //For right now, we will just use an infinite interval of validity
00134   oValidity = edm::ValidityInterval( edm::IOVSyncValue::beginOfTime(),edm::IOVSyncValue::endOfTime() );
00135 }
00136 
00137 //produce methods
00138 std::auto_ptr<ESPedestals>
00139 ESTrivialConditionRetriever::produceESPedestals( const ESPedestalsRcd& ) {
00140   std::cout<< " producing pedestals"<< std::endl;
00141   std::auto_ptr<ESPedestals>  peds = std::auto_ptr<ESPedestals>( new ESPedestals() );
00142   ESPedestals::Item ESitem;
00143   ESitem.mean  = ESpedMean_;
00144   ESitem.rms   = ESpedRMS_;
00145   
00146   for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
00147     for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++){
00148       for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++){
00149         for ( int iplane=1; iplane<=2; iplane++){
00150           for(int izeta=-1; izeta<=1 ;++izeta) {
00151             if(izeta==0) continue;
00152             try {
00153               //ESDetId Plane iplane Zside izeta 
00154               ESDetId aPositiveId(istrip,ix,iy,iplane,izeta);
00155               peds->insert(std::make_pair(aPositiveId.rawId(),ESitem));
00156             }
00157             catch ( cms::Exception &e ) 
00158               { 
00159               }
00160           }
00161         }
00162       }
00163     }
00164   }
00165   //return std::auto_ptr<ESPedestals>( peds );
00166   std::cout<< " produced pedestals"<< std::endl;
00167   return peds;
00168 }
00169 
00170 std::auto_ptr<ESWeightStripGroups>
00171 ESTrivialConditionRetriever::produceESWeightStripGroups( const ESWeightStripGroupsRcd& )
00172 {
00173   std::auto_ptr<ESWeightStripGroups> xtalGroups = std::auto_ptr<ESWeightStripGroups>( new ESWeightStripGroups() );
00174   ESStripGroupId defaultGroupId(1);
00175   std::cout << "entering produce weight groups"<< std::endl;
00176   for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
00177     for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++){
00178       for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++){
00179         for ( int iplane=1; iplane<=2; iplane++){
00180           for(int izeta=-1; izeta<=1 ;++izeta) {
00181             if(izeta==0) continue;
00182             try {
00183               //ESDetId Plane iplane Zside izeta 
00184               ESDetId anESId(istrip,ix,iy,iplane,izeta);
00185               //          xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
00186               xtalGroups->setValue(anESId.rawId(), defaultGroupId ); // define rings in eta
00187             }
00188             catch ( cms::Exception &e ) 
00189               { 
00190               }
00191           }
00192         }
00193       }
00194     }
00195   }
00196   std::cout << "done with produce weight groups"<< std::endl;
00197 
00198   return xtalGroups;
00199 }
00200 
00201 std::auto_ptr<ESIntercalibConstants>
00202 ESTrivialConditionRetriever::produceESIntercalibConstants( const ESIntercalibConstantsRcd& )
00203 {
00204   std::auto_ptr<ESIntercalibConstants>  ical = std::auto_ptr<ESIntercalibConstants>( new ESIntercalibConstants() );
00205   std::cout << "entring produce intercalib "<< std::endl;
00206 
00207   for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
00208     for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++){
00209       for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++){
00210         for ( int iplane=1; iplane<=2; iplane++){
00211           for(int izeta=-1; izeta<=1 ;++izeta) {
00212             if(izeta==0) continue;
00213             try {
00214               //ESDetId Plane iplane Zside izeta 
00215               ESDetId anESId(istrip,ix,iy,iplane,izeta);
00216               double r = (double)std::rand()/( double(RAND_MAX)+double(1) );
00217               ical->setValue( anESId.rawId(), intercalibConstantMean_ + r*intercalibConstantSigma_ );
00218             }
00219             catch ( cms::Exception &e )
00220               {
00221               }
00222           }
00223         }
00224       }
00225     }
00226   }
00227   std::cout << "done produce intercalib"<< std::endl;
00228 
00229   return ical;
00230 }
00231 
00232 
00233 std::auto_ptr<ESADCToGeVConstant>
00234 ESTrivialConditionRetriever::produceESADCToGeVConstant( const ESADCToGeVConstantRcd& )
00235 {
00236   return std::auto_ptr<ESADCToGeVConstant>( new ESADCToGeVConstant(adcToGeVLowConstant_,adcToGeVHighConstant_) );
00237 }
00238 
00239 std::auto_ptr<ESTBWeights>
00240 ESTrivialConditionRetriever::produceESTBWeights( const ESTBWeightsRcd& )
00241 {
00242   // create weights for the test-beam
00243   std::auto_ptr<ESTBWeights> tbwgt = std::auto_ptr<ESTBWeights>( new ESTBWeights() );
00244 
00245   int igrp=1;
00246   ESWeightSet wgt = ESWeightSet(amplWeights_);
00247   //  ESWeightSet::ESWeightMatrix& mat1 = wgt.getWeights();
00248   
00249   tbwgt->setValue(igrp,wgt);
00250 
00251 
00252   return tbwgt;
00253 }
00254 
00255 
00256   
00257 void ESTrivialConditionRetriever::getWeightsFromConfiguration(const edm::ParameterSet& ps)
00258 {
00259 
00260   ESWeightSet::ESWeightMatrix vampl;
00261 
00262   if (!getWeightsFromFile_ )
00263     {
00264       
00265       //      vampl.set(1.);
00266 
00267       // amplwgtv[0]= ps.getUntrackedParameter< std::vector<double> >("amplWeights", vampl);
00268     }
00269   else if (getWeightsFromFile_)
00270     {
00271       edm::LogInfo("ESTrivialConditionRetriever") << "Reading amplitude weights from file " << edm::FileInPath(amplWeightsFile_).fullPath().c_str() ;
00272       std::ifstream amplFile(edm::FileInPath(amplWeightsFile_).fullPath().c_str());
00273       while (!amplFile.eof() ) 
00274         {
00275           for(int j = 0; j < 2; ++j) {
00276             std::vector<float> vec(3) ;
00277             for(int k = 0; k < 3; ++k) {
00278               float ww;
00279               amplFile >> ww;
00280                     vec[k]=ww;
00281             }
00282             // vampl.putRow(vec);
00283           }
00284         }
00285     }
00286   else
00287     {
00288       //Not supported
00289       edm::LogError("ESTrivialConditionRetriever") << "Configuration not supported. Exception is raised ";
00290       throw cms::Exception("WrongConfig");
00291     }
00292 
00293   
00294   amplWeights_=ESWeightSet(vampl);
00295 
00296 }
00297 
00298 // --------------------------------------------------------------------------------
00299 
00300 std::auto_ptr<ESChannelStatus>
00301 ESTrivialConditionRetriever::getChannelStatusFromConfiguration (const ESChannelStatusRcd&)
00302 {
00303   std::auto_ptr<ESChannelStatus> ecalStatus = std::auto_ptr<ESChannelStatus>( new ESChannelStatus() );
00304   
00305 
00306   // start by setting all statuses to 0
00307   
00308 
00309   for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
00310     for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++){
00311       for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++){
00312         for ( int iplane=1; iplane<=2; iplane++){
00313           for(int izeta=-1; izeta<=1 ;++izeta) {
00314             if(izeta==0) continue;
00315             try {
00316               //ESDetId Plane iplane Zside izeta 
00317               ESDetId anESId(istrip,ix,iy,iplane,izeta);
00318               //          xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
00319               ecalStatus->setValue( anESId, 0 );
00320             }
00321             catch ( cms::Exception &e ) 
00322               { 
00323               }
00324           }
00325         }
00326       }
00327     }
00328   }
00329   
00330   // overwrite the statuses which are in the file
00331   
00332   edm::LogInfo("ESTrivialConditionRetriever") << "Reading channel statuses from file " << edm::FileInPath(channelStatusFile_).fullPath().c_str() ;
00333   std::ifstream statusFile(edm::FileInPath(channelStatusFile_).fullPath().c_str());
00334   if ( !statusFile.good() ) {
00335     edm::LogError ("ESTrivialConditionRetriever") 
00336       << "*** Problems opening file: " << channelStatusFile_ ;
00337     throw cms::Exception ("Cannot open ECAL channel status file") ;
00338   }
00339   
00340   std::string ESSubDet;
00341   std::string str;
00342   int hashIndex(0);
00343   int status(0);
00344   
00345   while (!statusFile.eof()) 
00346     {
00347       statusFile >> ESSubDet;
00348       if (ESSubDet!=std::string("ES") )
00349         {
00350           std::getline(statusFile,str);
00351           continue;
00352         }
00353       else
00354         {
00355           statusFile>> hashIndex >> status;
00356         }
00357       // std::cout << ESSubDet << " " << hashIndex << " " << status;
00358       
00359       if(ESSubDet==std::string("ES"))
00360         {
00361           ESDetId esid = ESDetId::unhashIndex(hashIndex);
00362           ecalStatus->setValue( esid, status );
00363         }
00364       else
00365         {
00366           edm::LogError ("ESTrivialConditionRetriever") 
00367             << " *** " << ESSubDet << " is not ES ";
00368         }
00369     }
00370   // the file is supposed to be in the form 
00371   // ES hashed_index status 
00372   // ES 132332 1  --> higher than 0  means bad 
00373   
00374   statusFile.close();
00375   return ecalStatus;
00376 }
00377 
00378 
00379 
00380 std::auto_ptr<ESChannelStatus>
00381 ESTrivialConditionRetriever::produceESChannelStatus( const ESChannelStatusRcd& )
00382 {
00383 
00384   std::auto_ptr<ESChannelStatus>  ical = std::auto_ptr<ESChannelStatus>( new ESChannelStatus() );
00385   for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
00386     for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++){
00387       for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++){
00388         for ( int iplane=1; iplane<=2; iplane++){
00389           for(int izeta=-1; izeta<=1 ;++izeta) {
00390             if(izeta==0) continue;
00391             try {
00392               //ESDetId Plane iplane Zside izeta 
00393               ESDetId anESId(istrip,ix,iy,iplane,izeta);
00394               //          xtalGroups->setValue(ebid.rawId(), ESStripGroupId(ieta) ); // define rings in eta
00395               ical->setValue( anESId, 0 );
00396             }
00397             catch ( cms::Exception &e ) 
00398               { 
00399               }
00400           }
00401         }
00402       }
00403     }
00404   }
00405   
00406   return ical;
00407 }
00408 
00409 
00410 
00411 // --------------------------------------------------------------------------------
00412 
00413 
00414