CMS 3D CMS Logo

Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes

EcalDccWeightBuilder Class Reference

#include <EcalDccWeightBuilder.h>

Inheritance diagram for EcalDccWeightBuilder:
edm::EDAnalyzer

List of all members.

Public Member Functions

void analyze (const edm::Event &event, const edm::EventSetup &es)
 EcalDccWeightBuilder (edm::ParameterSet const &ps)
virtual ~EcalDccWeightBuilder ()

Private Types

enum  mode_t { WEIGHTS_FROM_CONFIG, COMPUTE_WEIGHTS }

Private Member Functions

void computeAllWeights (bool withIntercalib)
void computeWeights (const EcalShapeBase &shape, int binOfMax, double timePhase, int iFirst0, int nWeights, int iSkip0, std::vector< double > &result)
void dbId (const DetId &detId, int &fedId, int &smId, int &ruId, int &xtalId) const
double decodeWeight (int W)
int encodeWeight (double w)
double intercalib (const DetId &detId)
template<class T >
void sort (const std::vector< T > &a, std::vector< int > &s, bool decreasingOrder=false)
void unbiasWeights (std::vector< double > &weights, std::vector< int32_t > *encodedWeigths)
void writeWeightToAsciiFile ()
void writeWeightToDB ()
void writeWeightToRootFile ()

Private Attributes

std::string asciiOutputFileName_
EcalIntercalibConstantMapcalibMap_
std::string dbPassword_
std::string dbSid_
std::string dbTag_
std::string dbUser_
int dbVersion_
int dcc1stSample_
bool dccWeightsWithIntercalib_
const EcalElectronicsMappingecalElectronicsMap_
EcalIntercalibConstantMap emptyCalibMap_
std::map< DetId, std::vector
< int > > 
encodedWeights_
edm::ESHandle< CaloGeometrygeom_
mode_t imode_
std::vector< double > inputWeights_
std::string mode_
int nDccWeights_
std::string rootOutputFileName_
int sampleToSkip_
bool sqlMode_
bool writeToAsciiFile_
bool writeToDB_
bool writeToRootFile_

Static Private Attributes

static const int ecalDccFedIdMax = 654
static const int ecalDccFedIdMin = 601
static const int nDccs = ecalDccFedIdMax-ecalDccFedIdMin+1
static const double weightScale_ = 1024.

Detailed Description

Definition at line 28 of file EcalDccWeightBuilder.h.


Member Enumeration Documentation

Enumerator:
WEIGHTS_FROM_CONFIG 
COMPUTE_WEIGHTS 

Definition at line 30 of file EcalDccWeightBuilder.h.


Constructor & Destructor Documentation

EcalDccWeightBuilder::EcalDccWeightBuilder ( edm::ParameterSet const &  ps)

Constructs an EcalDccWeightBuilder

Parameters:
psCMSSW mondule configuration

Definition at line 45 of file EcalDccWeightBuilder.cc.

References COMPUTE_WEIGHTS, Exception, imode_, inputWeights_, mode_, nDccWeights_, and WEIGHTS_FROM_CONFIG.

                                                                   :
  dcc1stSample_(ps.getParameter<int>("dcc1stSample")),
  sampleToSkip_(ps.getParameter<int>("sampleToSkip")),
  nDccWeights_(ps.getParameter<int>("nDccWeights")),
  inputWeights_(ps.getParameter<vector<double> >("inputWeights")),
  mode_(ps.getParameter<string>("mode")),
  dccWeightsWithIntercalib_(ps.getParameter<bool>("dccWeightsWithIntercalib")),
  writeToDB_(ps.getParameter<bool>("writeToDB")),
  writeToAsciiFile_(ps.getParameter<bool>("writeToAsciiFile")),
  writeToRootFile_(ps.getParameter<bool>("writeToRootFile")),
  asciiOutputFileName_(ps.getParameter<string>("asciiOutputFileName")),
  rootOutputFileName_(ps.getParameter<string>("rootOutputFileName")),
  dbSid_(ps.getParameter<string>("dbSid")),
  dbUser_(ps.getParameter<string>("dbUser")),
  dbPassword_(ps.getUntrackedParameter<string>("dbPassword","")),
  dbTag_(ps.getParameter<string>("dbTag")),
  dbVersion_(ps.getParameter<int>("dbVersion")),
  sqlMode_(ps.getParameter<bool>("sqlMode")),
  calibMap_(emptyCalibMap_)
{
  if(mode_=="weightsFromConfig"){
    imode_ = WEIGHTS_FROM_CONFIG;
    if(inputWeights_.size()!=(unsigned)nDccWeights_){
      throw cms::Exception("Config")
        << "Inconsistent configuration. 'nDccWeights' parameters indicated "
        << nDccWeights_ << " weights while parameter 'inputWeights_' contains "
        << inputWeights_.size() << " weight values!\n";
    }
  } else if(mode_=="computeWeights"){
    imode_ = COMPUTE_WEIGHTS;
  } else{
    throw cms::Exception("Config")
      << "Invalid value ('" << mode_ << "') for parameter mode. "
      << "Valid values are: 'weightsFromConfig' and 'computeWeights'\n";
  }
}
virtual EcalDccWeightBuilder::~EcalDccWeightBuilder ( ) [inline, virtual]

Destructor

Definition at line 41 of file EcalDccWeightBuilder.h.

{};

Member Function Documentation

void EcalDccWeightBuilder::analyze ( const edm::Event event,
const edm::EventSetup es 
) [virtual]
void EcalDccWeightBuilder::computeAllWeights ( bool  withIntercalib) [private]

Definition at line 112 of file EcalDccWeightBuilder.cc.

References CaloSimParameters::binOfMaximum(), COMPUTE_WEIGHTS, computeWeights(), filterCSVwithJSON::copy, gather_cfg::cout, dcc1stSample_, DetId::Ecal, EcalBarrel, EcalEndcap, encodedWeights_, exception, Exception, geom_, i, imode_, inputWeights_, intercalib(), min, nDccWeights_, sampleToSkip_, EcalSimParameterMap::simParameters(), CaloSimParameters::timePhase(), unbiasWeights(), and WEIGHTS_FROM_CONFIG.

Referenced by analyze().

                                                               {
  const int nw = nDccWeights_;
  int iSkip0_ = sampleToSkip_>=0?(sampleToSkip_-dcc1stSample_):-1;

  EcalSimParameterMap parameterMap;
  const vector<DetId>& ebDetIds
    = geom_->getValidDetIds(DetId::Ecal, EcalBarrel);

  //   cout << __FILE__ << ":" << __LINE__ << ": "
  //        <<  "Number of EB det IDs: " << ebDetIds.size() << "\n";
  
  const vector<DetId>& eeDetIds
    = geom_->getValidDetIds(DetId::Ecal, EcalEndcap);

  //  cout << __FILE__ << ":" << __LINE__ << ": "
  //        <<  "Number of EE det IDs: " << eeDetIds.size() << "\n";
  
  
  vector<DetId> detIds(ebDetIds.size()+eeDetIds.size());
  copy(ebDetIds.begin(), ebDetIds.end(), detIds.begin());
  copy(eeDetIds.begin(), eeDetIds.end(), detIds.begin()+ebDetIds.size());
  
  vector<double> baseWeights(nw); //weight obtained from signal shape
  vector<double> w(nw); //weight*intercalib
  vector<int> W(nw);    //weight in hw encoding (integrer)
  double prevPhase = numeric_limits<double>::min();


  if(imode_==WEIGHTS_FROM_CONFIG){
    assert(inputWeights_.size()==baseWeights.size());
    copy(inputWeights_.begin(), inputWeights_.end(), baseWeights.begin());
  }
  
  for(vector<DetId>::const_iterator it = detIds.begin();
      it != detIds.end(); ++it){
    
    double phase = parameterMap.simParameters(*it).timePhase();
    int binOfMax = parameterMap.simParameters(*it).binOfMaximum();
    
#if 0
    //for debugging...
    cout << __FILE__ << ":" << __LINE__ << ": ";
    if(it->subdetId()==EcalBarrel){
      cout << "ieta = " << setw(4) << ((EBDetId)(*it)).ieta()
           << " iphi = " << setw(4) << ((EBDetId)(*it)).iphi() << " ";
    } else if(it->subdetId()==EcalEndcap){
      cout << "ix = " << setw(3) << ((EEDetId)(*it)).ix()
           << " iy = " << setw(3) << ((EEDetId)(*it)).iy()
           << " iz = " << setw(1) << ((EEDetId)(*it)).iy() << " ";
    } else{
      throw cms::Exception("EcalDccWeightBuilder")
        << "Bug found in " << __FILE__ << ":" << __LINE__ << ": "
        << "Got a detId which is neither tagged as ECAL Barrel "
        << "not ECAL endcap while looping on ECAL cell detIds\n";
    }
    cout << " -> phase: "  << phase << "\n";
    cout << " -> binOfMax: " << binOfMax << "\n";
#endif
    
    try{
      EBShape ebShape;
      EEShape eeShape;
      EcalShapeBase* pShape;      

      if(it->subdetId()==EcalBarrel){
        pShape = &ebShape;
      } else if(it->subdetId()==EcalEndcap){
        pShape = &eeShape;
      } else{
        throw cms::Exception("EcalDccWeightBuilder")
          << "Bug found in " << __FILE__ << ":" << __LINE__ << ": "
          << "Got a detId which is neither tagged as ECAL Barrel "
          << "not ECAL endcap while looping on ECAL cell detIds\n";
      }
      
      if(phase!=prevPhase){
        if(imode_==COMPUTE_WEIGHTS){
          if(it->subdetId()==EcalBarrel){
            computeWeights(*pShape, binOfMax, phase, 
                           dcc1stSample_-1, nDccWeights_, iSkip0_,
                           baseWeights);
          } 
          prevPhase = phase;
        }
      }
      for(int i = 0; i < nw; ++i){
        w[i] = baseWeights[i];
        if(withIntercalib) w[i]*= intercalib(*it);
      }
      unbiasWeights(w, &W);
      encodedWeights_[*it] = W;
    } catch(std::exception& e){
      cout << __FILE__ << ":" << __LINE__ << ": ";
      if(it->subdetId()==EcalBarrel){
        cout << "ieta = " << setw(4) << ((EBDetId)(*it)).ieta()
             << " iphi = " << setw(4) << ((EBDetId)(*it)).iphi() << " ";
      } else if(it->subdetId()==EcalEndcap){
        cout << "ix = " << setw(3) << ((EEDetId)(*it)).ix()
             << " iy = " << setw(3) << ((EEDetId)(*it)).iy()
             << " iz = " << setw(1) << ((EEDetId)(*it)).iy() << " ";
      } else{
        cout << "DetId " << (uint32_t) (*it);
      }
      cout <<  "phase: "  << phase << "\n";
      throw;
    }
  }
}
void EcalDccWeightBuilder::computeWeights ( const EcalShapeBase shape,
int  binOfMax,
double  timePhase,
int  iFirst0,
int  nWeights,
int  iSkip0,
std::vector< double > &  result 
) [private]

Weight computation

Parameters:
shapesignal shape to use for weight computation
binOfMaxtime bin which shall contain the pulse peak
timePhasesignal phase. First sample corresponds to t = ((binOfMax-1)*25+timePhase) ns in the time reference of EcalShapeBase.
iFirstfirst sample the weights must be applied to
nWeightsnumber of weights
iSkip0if greater than 0, the corresponding sample will not be used (weights forced to 0).
result[out] vector filled with computed weights. The vector is resized to the number of weights

Referenced by computeAllWeights().

void EcalDccWeightBuilder::dbId ( const DetId detId,
int &  fedId,
int &  smId,
int &  ruId,
int &  xtalId 
) const [private]

Definition at line 637 of file EcalDccWeightBuilder.cc.

References gather_cfg::cout, EcalElectronicsId::dccId(), EcalBarrel, ecalElectronicsMap_, EcalElectronicsMapping::getElectronicsId(), EcalElectronicsId::stripId(), DetId::subdetId(), EcalElectronicsId::towerId(), and EcalElectronicsId::xtalId().

Referenced by writeWeightToAsciiFile(), writeWeightToDB(), and writeWeightToRootFile().

                                                  {
  const EcalElectronicsId& elecId
    = ecalElectronicsMap_->getElectronicsId(detId);
  
  fedId = 600 + elecId.dccId();
  ruId =  ecalElectronicsMap_->getElectronicsId(detId).towerId();
  
  if(detId.subdetId()==EcalBarrel) {
    smId=((EBDetId)detId).ism();
  } else{
    smId = 10000-fedId; //no SM in EE. Use some unique value to satisfy
    //              current DB PK constraints.
  }
  const int stripLength = 5; //1 strip = 5 crystals in a row
  xtalId = (elecId.stripId()-1)*stripLength  + elecId.xtalId();

#if 0
  cout << __FILE__ << ":" << __LINE__ << ": FED ID "
       <<  fedId << "\n";

  cout << __FILE__ << ":" << __LINE__ << ": SM logical ID "
       <<  smId << "\n";
  
  cout << __FILE__ << ":" << __LINE__ << ": RU ID (TT or SC): "
       <<  ruId << "\n";
  
  cout << __FILE__ << ":" << __LINE__ << ": strip:"
       <<  elecId.stripId() << "\n";
  
  cout << __FILE__ << ":" << __LINE__ << ": xtal in strip: "
       <<  elecId.xtalId() << "\n";

  cout << __FILE__ << ":" << __LINE__ << ": xtalId in RU: "
       <<  xtalId << "\n";
#endif
}
double EcalDccWeightBuilder::decodeWeight ( int  W) [private]

Definition at line 260 of file EcalDccWeightBuilder.cc.

References weightScale_.

Referenced by unbiasWeights().

                                              {
  return ((double) W) / weightScale_;
}
int EcalDccWeightBuilder::encodeWeight ( double  w) [private]

Definition at line 256 of file EcalDccWeightBuilder.cc.

References weightScale_.

Referenced by unbiasWeights().

                                              {
  return lround(w * weightScale_);
}
double EcalDccWeightBuilder::intercalib ( const DetId detId) [private]

Retrieve intercalibration coefficent for channel detId.

Parameters:
detIdID of the channel the intercalibration coef. must be retrieved for.

Definition at line 368 of file EcalDccWeightBuilder.cc.

References calibMap_, gather_cfg::cout, EcalBarrel, EcalCondObjectContainer< T >::end(), EcalCondObjectContainer< T >::find(), DetId::rawId(), and DetId::subdetId().

Referenced by analyze(), and computeAllWeights().

                                                         {
  // get current intercalibration coeff
  double coef;
  EcalIntercalibConstantMap::const_iterator itCalib
    = calibMap_.find(detId.rawId());
  if(itCalib != calibMap_.end()){
    coef = (*itCalib);
  } else{
    coef = 1.;
    std::cout << (uint32_t) detId
              << " not found in EcalIntercalibConstantMap"<<std::endl ;
  }
#if 0
  cout << __FILE__ << ":" << __LINE__ << ": ";
  if(detId.subdetId()==EcalBarrel){
    cout <<  "ieta = " << ((EBDetId)detId).ieta()
         << " iphi = " << ((EBDetId)detId).iphi();
  } else{
    cout << "ix = " << ((EEDetId)detId).ix()
         << " iy = " << ((EEDetId)detId).iy()
         << " iz = " << ((EEDetId)detId).zside();
  }
  cout << " coef = " <<  coef << "\n";
#endif
  return coef;
}
template<class T >
void EcalDccWeightBuilder::sort ( const std::vector< T > &  a,
std::vector< int > &  s,
bool  decreasingOrder = false 
) [private]

Vector index sorting.

Parameters:
vectorof values
[out]sortedindex of the vector
decreasingOrderswitch to use decreasing order instead of default increasing order

Definition at line 266 of file EcalDccWeightBuilder.cc.

References i, j, and swap().

Referenced by unbiasWeights().

                                                     {
  //   cout << __FILE__ << ":" << __LINE__ << ": "
  //        << "sort input array:" ;
  //   for(unsigned i=0; i<a.size(); ++i){
  //     cout << "\t" << a[i];
  //   }
  //   cout << "\n";
  
  //performs a bubble sort: adjacent elements are successively swapped 2 by 2
  //until the list is finally sorted.
  bool changed = false;
  s.resize(a.size());
  for(unsigned i=0; i<a.size(); ++i) s[i] = i;
  if(a.size() == 0) return;
  do {
    changed = false;
    for(unsigned i = 0; i < a.size()-1; ++i){
      const int j = s[i];
      const int nextj = s[i+1];
      if((decreasingOrder && (a[j] < a[nextj]))
         || (!decreasingOrder && (a[j] > a[nextj]))){
        std::swap(s[i], s[i+1]);
        changed = true;
      }
    }
  } while(changed);
  
  //   cout << __FILE__ << ":" << __LINE__ << ": "
  //        << "sorted list of indices:" ;
  //   for(unsigned i=0; i < s.size(); ++i){
  //     cout << "\t" << s[i];
  //   }
  //   cout << "\n";
}
void EcalDccWeightBuilder::unbiasWeights ( std::vector< double > &  weights,
std::vector< int32_t > *  encodedWeigths 
) [private]

Definition at line 303 of file EcalDccWeightBuilder.cc.

References decodeWeight(), encodeWeight(), i, and sort().

Referenced by computeAllWeights().

                                                                        {
  const unsigned nw = weights.size();
  
  //computes integer weights, weights residuals and weight sum residual:
  vector<double> dw(nw); //weight residuals due to interger encoding
  vector<int> W(nw); //integer weights
  int wsum = 0;
  for(unsigned i = 0; i < nw; ++i){
    W[i] = encodeWeight(weights[i]);
    dw[i] = decodeWeight(W[i]) - weights[i];
    wsum += W[i];
  }

//   cout << __FILE__ << ":" << __LINE__ << ": "
//        <<  "weights before bias correction: ";
//   for(unsigned i=0; i<weights.size(); ++i){
//     const double w = weights[i];
//     cout << "\t" << encodeWeight(w) << "(" << w << ", dw = " << dw[i] << ")";
//   }
//   cout << "\t sum: " << wsum << "\n";
  
  //sorts weight residuals in decreasing order:
  vector<int> iw(nw);
  sort(dw, iw, true);

  //compensates weight sum residual by adding or substracting 1 to weights
  //starting from:
  // 1) the weight with the minimal signed residual if the correction
  // is positive (wsum<0)
  // 2) the weight with the maximal signed residual if the correction
  // is negative (wsum>0)
  int wsumSign = wsum>0?1:-1;
  int i = wsum>0?0:(nw-1);
  while(wsum!=0){
    W[iw[i]] -= wsumSign;
    wsum -= wsumSign;
    i += wsumSign;
    if(i<0 || i>=(int)nw){ //recompute the residuals if a second iteration is
      // needed (in principle, it is not expected with usual input weights), : 
      for(unsigned i = 0; i < nw; ++i){
        dw[i] = decodeWeight(W[i]) - weights[i];
        sort(dw, iw, true);
      }
    }
    if(i<0) i = nw-1;
    if(i>=(int)nw) i = 0;
  }

//   cout << __FILE__ << ":" << __LINE__ << ": "
//        <<  "weights after bias correction: ";
//   for(unsigned i=0; i<weights.size(); ++i){
//     cout << "\t" << W[i] << "(" << decodeWeight(W[i]) << ", dw = "
//       << (decodeWeight(W[i])-weights[i]) << ")";
//   }
//   cout << "\n";
  
  //copy result
  if(encodedWeights!=0) encodedWeights->resize(nw);
  for(unsigned i = 0; i < nw; ++i){
    weights[i] = decodeWeight(W[i]);
    if(encodedWeights) (*encodedWeights)[i] = W[i];
  }
}
void EcalDccWeightBuilder::writeWeightToAsciiFile ( ) [private]

Definition at line 395 of file EcalDccWeightBuilder.cc.

References asciiOutputFileName_, comment, dbId(), dbTag_, dbVersion_, dcc1stSample_, ecalDccFedIdMax, ecalDccFedIdMin, encodedWeights_, Exception, mergeVDriftHistosByStation::file, i, DetId::rawId(), and sqlMode_.

Referenced by analyze().

                                                 {
  string fName = asciiOutputFileName_.size()!=0?
    asciiOutputFileName_.c_str()
    :"dccWeights.txt";
  ofstream file(fName.c_str());
  if(!file.good()){
    throw cms::Exception("Output")
      << "Failed to open file '"
      << fName
      << "'for writing DCC weights\n";
  }

  const char* comment = sqlMode_?"-- ":"# ";
  
  file << comment << "List of weights for amplitude estimation to be used in DCC for\n"
       << comment << "zero suppresssion.\n\n";
  if(!sqlMode_){
    file << comment << "Note: RU: trigger tower in EB, supercrystal in EE\n"
         << comment << "      xtl: crystal electronic channel id in RU, from 1 to 25\n\n"
         << comment << " DetId    SM  FED RU xtl weights[0..5]...\n";
  }
  
  if(sqlMode_){
    file << "variable recid number;\n"
      "exec select COND2CONF_INFO_SQ.NextVal into :recid from DUAL;\n"
      "insert into weights_info (rec_id,tag,version) values (:recid,'"
         << dbTag_ << "'," << dbVersion_ << ");\n";
    file << "\n" << comment
         << "index of first sample used in the weighting sum\n"
      "begin\n"
      "  for fedid in " << ecalDccFedIdMin << ".." << ecalDccFedIdMax
         << " loop\n"
      "    insert into dcc_weightsample_dat (rec_id, logic_id, sample_id, \n"
      "    weight_number)\n"
      "    values(:recid,fedid," << dcc1stSample_ << ",1);\n"
      "  end loop;\n"
      "end;\n"
      "/\n";
  } else{
    file << "1st DCC sample: " << dcc1stSample_ << "\n";
  }

  file << "\n" << comment << "list of weights per crystal channel\n";
  
  for(map<DetId, std::vector<int32_t> >::const_iterator it
        = encodedWeights_.begin();
      it !=  encodedWeights_.end();
      ++it){
    const DetId& detId = it->first;
    
    int fedId;
    int smId;
    int ruId;
    int xtalId;
    
    //detId ->  fedId, smId, ruId, xtalId
    dbId(detId, fedId, smId, ruId, xtalId);

    char delim = sqlMode_?',':' ';

    if(sqlMode_) file << "-- detId " << detId.rawId() << "\n"
                      << "insert into dcc_weights_dat(rec_id,sm_id,fed_id,"
                   "tt_id, cry_id,\n"
                   "weight_0,weight_1,weight_2,weight_3,weight_4,weight_5) \n"
                   "values ("
                   ":recid";
    
    const vector<int>& weights = it->second;
    if(!sqlMode_) file << setw(10) << detId.rawId();
    file << delim << setw(2) << smId;
    file << delim << setw(3) << fedId;
    file << delim << setw(2) << ruId;
    file << delim << setw(2) << xtalId;
      
    for(unsigned i=0; i<weights.size(); ++i){
      file << delim << setw(5) << weights[i];
    }
    if(sqlMode_) file << ");";
    file << "\n";
  }
  if(!file.good()){
    throw cms::Exception("Output") << "Error while writing DCC weights to '"
                                   << fName << "' file.";
  }
}
void EcalDccWeightBuilder::writeWeightToDB ( ) [private]

Definition at line 543 of file EcalDccWeightBuilder.cc.

References benchmark_cfg::cerr, gather_cfg::cout, dbId(), dbPassword_, dbSid_, dbTag_, dbUser_, dbVersion_, dcc1stSample_, ecalDccFedIdMax, ecalDccFedIdMin, encodedWeights_, cmsRelvalreport::exit, convertXMLtoSQLite_cfg::fileName, lut2db_cfg::filePrefix, ODFEWeightsInfo::getId(), i, EcalCondDBInterface::insertConfigDataArraySet(), EcalCondDBInterface::insertConfigSet(), nDccs, PasswordReader::readPassword(), IODConfig::setConfigTag(), ODWeightsDat::setCrystalId(), ODWeightsDat::setFedId(), ODWeightsDat::setId(), ODWeightsDat::setSMId(), ODWeightsDat::setTTId(), ODFEWeightsInfo::setVersion(), ODWeightsDat::setWeight0(), ODWeightsDat::setWeight1(), ODWeightsDat::setWeight2(), ODWeightsDat::setWeight3(), ODWeightsDat::setWeight4(), and ODWeightsDat::setWeight5().

Referenced by analyze().

                                          {
  cout << "going to write to the online DB "<<dbSid_<<" user "<<dbUser_<<endl;;
  EcalCondDBInterface* econn;

  try {
    cout << "Making connection..." << flush;
    const string& filePrefix = string("file:");
    if(dbPassword_.find(filePrefix)==0){ //password must be read for a file
      string fileName = dbPassword_.substr(filePrefix.size());
      //substitute dbPassword_ value by the password read from the file
      PasswordReader pr;
      pr.readPassword(fileName, dbUser_, dbPassword_);
    }

    //     cout << __FILE__ << ":" << __LINE__ << ": "
    //           <<  "Password: " << dbPassword_ << "\n";

    econn = new EcalCondDBInterface( dbSid_, dbUser_, dbPassword_ );
    cout << "Done." << endl;
  } catch (runtime_error &e) {
    cerr << e.what() << endl;
    exit(-1);
  }
  
  ODFEWeightsInfo weight_info;
  weight_info.setConfigTag(dbTag_);
  weight_info.setVersion(dbVersion_);
  cout << "Inserting in DB..." << endl;

  econn->insertConfigSet(&weight_info);
  
  int weight_id=weight_info.getId();
  cout << "WeightInfo inserted with ID "<< weight_id<< endl;

  vector<ODWeightsDat> datadel;
  datadel.reserve(encodedWeights_.size());

  vector<ODWeightsSamplesDat> dcc1stSampleConfig(nDccs);
  for(int i = ecalDccFedIdMin; i <= ecalDccFedIdMax; ++i){
    dcc1stSampleConfig[i].setId(weight_id);
    dcc1stSampleConfig[i].setFedId(601+i);
    dcc1stSampleConfig[i].setSampleId(dcc1stSample_);
    dcc1stSampleConfig[i].setWeightNumber(-1); //not used.
  }
  econn->insertConfigDataArraySet(dcc1stSampleConfig, &weight_info);
  
  for(map<DetId, std::vector<int32_t> >::const_iterator it
        = encodedWeights_.begin();
      it !=  encodedWeights_.end();
      ++it){
    const DetId& detId = it->first;
    const unsigned nWeights = 6;
    vector<int> weights(nWeights);

    for(unsigned i=0; i<weights.size(); ++i){
      //completing the weight vector with zeros in case it has
      //less than 6 elements:
      const vector<int>& w = it->second;
      weights[i] = i<w.size()?w[i]:0;
    }

    ODWeightsDat one_dat;
    one_dat.setId(weight_id);

    int fedId;
    int smId;
    int ruId;
    int xtalId;
    
    //detId ->  fedId, smId, ruId, xtalId
    dbId(detId, fedId, smId, ruId, xtalId);
      
    one_dat.setSMId(smId);
    one_dat.setFedId(fedId);
    one_dat.setTTId(ruId);
    one_dat.setCrystalId(xtalId);
    
    one_dat.setWeight0(weights[0]);
    one_dat.setWeight1(weights[1]);
    one_dat.setWeight2(weights[2]);
    one_dat.setWeight3(weights[3]);
    one_dat.setWeight4(weights[4]);
    one_dat.setWeight5(weights[5]);
    
    datadel.push_back(one_dat);
  }
  econn->insertConfigDataArraySet(datadel,&weight_info);
  std::cout<< " .. done insertion in DB "<< endl;
  delete econn;
  cout<< "closed DB connection ... done"  << endl;
}
void EcalDccWeightBuilder::writeWeightToRootFile ( ) [private]

Definition at line 480 of file EcalDccWeightBuilder.cc.

References filterCSVwithJSON::copy, dbId(), encodedWeights_, Exception, mergeVDriftHistosByStation::file, rootOutputFileName_, and matplotRender::t.

Referenced by analyze().

                                                {
  string fName = rootOutputFileName_.size()!=0?
    rootOutputFileName_.c_str()
    :"dccWeights.root";
  TFile file(fName.c_str(), "RECREATE");
  if(file.IsZombie()){
    throw cms::Exception("Output")
      << "Failed to open file '"
      << fName
      << "'for writing DCC weights\n";
  }
  TTree t("dccWeights", "Weights for DCC ZS filter");
  const int nWeightMax = 20; //normally n_weights = 6. A different might be used
  //                           used for test purposes.
  struct {
    Int_t detId;
    Int_t fedId;
    Int_t smId;
    Int_t ruId;
    Int_t xtalId;
    Int_t n_weights;
    Int_t weights[nWeightMax];
  } buf;
  t.Branch("weights", &buf,
           "rawDetId/I:"
           "feId/I:"
           "smSlotId/I:"
           "ruId/I:"
           "xtalInRuId/I:"
           "n_weights/I:"
           "weights[n_weights]/I");
  for(map<DetId, std::vector<int32_t> >::const_iterator it
        = encodedWeights_.begin();
      it !=  encodedWeights_.end();
      ++it){
    buf.detId = it->first.rawId();
    buf.n_weights = it->second.size();

    //detId ->  fedId, smId, ruId, xtalId
    dbId(buf.detId, buf.fedId, buf.smId, buf.ruId, buf.xtalId);

    if(buf.n_weights>nWeightMax){
      throw cms::Exception("EcalDccWeight")
        << "Number of weights (" << buf.n_weights
        << ") for DetId " << buf.detId
        << " exceeded maximum limit (" << nWeightMax
        << ") of root output format. ";
    }
    copy(it->second.begin(), it->second.end(), buf.weights);
    t.Fill();
  }
  t.Write();
  file.Close();
}

Member Data Documentation

Definition at line 123 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile().

Definition at line 134 of file EcalDccWeightBuilder.h.

Referenced by analyze(), and intercalib().

std::string EcalDccWeightBuilder::dbPassword_ [private]

Definition at line 127 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToDB().

std::string EcalDccWeightBuilder::dbSid_ [private]

Definition at line 125 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToDB().

std::string EcalDccWeightBuilder::dbTag_ [private]

Definition at line 128 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile(), and writeWeightToDB().

std::string EcalDccWeightBuilder::dbUser_ [private]

Definition at line 126 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToDB().

Definition at line 129 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile(), and writeWeightToDB().

Definition at line 119 of file EcalDccWeightBuilder.h.

Referenced by analyze().

const int EcalDccWeightBuilder::ecalDccFedIdMax = 654 [static, private]

Definition at line 142 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile(), and writeWeightToDB().

const int EcalDccWeightBuilder::ecalDccFedIdMin = 601 [static, private]

Definition at line 141 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile(), and writeWeightToDB().

Definition at line 139 of file EcalDccWeightBuilder.h.

Referenced by analyze(), and dbId().

Definition at line 135 of file EcalDccWeightBuilder.h.

std::map<DetId, std::vector<int> > EcalDccWeightBuilder::encodedWeights_ [private]

Definition at line 132 of file EcalDccWeightBuilder.h.

Referenced by analyze(), and computeAllWeights().

Definition at line 118 of file EcalDccWeightBuilder.h.

Referenced by computeAllWeights(), and EcalDccWeightBuilder().

std::vector<double> EcalDccWeightBuilder::inputWeights_ [private]

weights used in weightFromConfig mode.

Definition at line 116 of file EcalDccWeightBuilder.h.

Referenced by computeAllWeights(), and EcalDccWeightBuilder().

std::string EcalDccWeightBuilder::mode_ [private]

Definition at line 117 of file EcalDccWeightBuilder.h.

Referenced by EcalDccWeightBuilder().

Definition at line 143 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToDB().

Definition at line 113 of file EcalDccWeightBuilder.h.

Referenced by computeAllWeights(), and EcalDccWeightBuilder().

Definition at line 124 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToRootFile().

Definition at line 112 of file EcalDccWeightBuilder.h.

Referenced by computeAllWeights().

Definition at line 130 of file EcalDccWeightBuilder.h.

Referenced by writeWeightToAsciiFile().

const double EcalDccWeightBuilder::weightScale_ = 1024. [static, private]

Definition at line 138 of file EcalDccWeightBuilder.h.

Referenced by decodeWeight(), and encodeWeight().

Definition at line 121 of file EcalDccWeightBuilder.h.

Referenced by analyze().

Definition at line 120 of file EcalDccWeightBuilder.h.

Referenced by analyze().

Definition at line 122 of file EcalDccWeightBuilder.h.

Referenced by analyze().