CMS 3D CMS Logo

SiStripGainESProducerTemplate.h
Go to the documentation of this file.
1 #ifndef CalibTracker_SiStripESProducers_SiStripGainESProducerTemplate_h
2 #define CalibTracker_SiStripESProducers_SiStripGainESProducerTemplate_h
3 
4 // system include files
5 #include <memory>
6 #include <utility>
7 
8 // user include files
12 
16 
21 //
22 // class declaration
23 //
24 template<typename TDependentRecord, typename TInputRecord>
26  public:
29 
30  std::unique_ptr<SiStripGain> produce(const TDependentRecord&);
31 
32  private:
33 
34  SiStripGain* SiStripGainNormalizationFunction(const TDependentRecord& iRecord);
35  double getNFactor(const int apvGainIndex);
36 
37  std::vector<edm::ParameterSet> apvGainLabels_;
38  std::vector<std::pair<std::string, std::string> > apvgain_;
39  std::vector<double> norm_;
43  std::vector<edm::ESHandle<SiStripApvGain> > pDD;
44 
45  void fillApvGain( const SiStripGainRcd & a, const std::pair<std::string, std::string> & recordLabelPair, std::vector<edm::ESHandle<SiStripApvGain> >& pDD );
46 };
47 
48 template<typename TDependentRecord, typename TInputRecord>
50 {
51  setWhatProduced(this);
52 
53  automaticMode_ = iConfig.getParameter<bool>("AutomaticNormalization");
54  printdebug_ = iConfig.getUntrackedParameter<bool>("printDebug", false);
55  apvGainLabels_ = iConfig.getParameter<std::vector<edm::ParameterSet> >("APVGain");
56 
57  // Fill the vector of apv labels
58  std::vector<edm::ParameterSet>::const_iterator gainPSetIt = apvGainLabels_.begin();
59  for( ; gainPSetIt != apvGainLabels_.end(); ++gainPSetIt ) {
60  apvgain_.push_back( std::make_pair(gainPSetIt->getParameter<std::string>("Record"), gainPSetIt->getUntrackedParameter<std::string>("Label", "")) );
61  norm_.push_back(gainPSetIt->getUntrackedParameter<double>("NormalizationFactor", 1.));
62  }
63  bool badNorm = false;
64  std::vector<double>::const_iterator it = norm_.begin();
65  for( ; it != norm_.end(); ++it ) {
66  if( *it <= 0 ) badNorm = true;
67  }
68 
69  if(!automaticMode_ && badNorm ){
70  edm::LogError("SiStripGainESProducer") << "[SiStripGainESProducer] - ERROR: negative or zero Normalization factor provided. Assuming 1 for such factor" << std::endl;
71  norm_ = std::vector<double>(norm_.size(), 1.);
72  }
73 }
74 
75 template<typename TDependentRecord, typename TInputRecord>
76 std::unique_ptr<SiStripGain> SiStripGainESProducerTemplate<TDependentRecord,TInputRecord>::produce(const TDependentRecord& iRecord)
77 {
78  std::unique_ptr<SiStripGain> ptr(SiStripGainNormalizationFunction(iRecord));
79  return ptr;
80 }
81 
82 template<typename TDependentRecord, typename TInputRecord>
83 void SiStripGainESProducerTemplate<TDependentRecord, TInputRecord>::fillApvGain( const SiStripGainRcd & a, const std::pair<std::string, std::string> & recordLabelPair, std::vector<edm::ESHandle<SiStripApvGain> >& pDD )
84 {
85  // Put in an empty ApvGain and fill it
87  std::string recordName( recordLabelPair.first );
88  std::string labelName( recordLabelPair.second );
89  if( recordName == "SiStripApvGainRcd" ) a.getRecord<SiStripApvGainRcd>().get( labelName, pDD.back() );
90  else if( recordName == "SiStripApvGain2Rcd" ) a.getRecord<SiStripApvGain2Rcd>().get( labelName, pDD.back() );
91  else if( recordName == "SiStripApvGain3Rcd" ) a.getRecord<SiStripApvGain3Rcd>().get( labelName, pDD.back() );
92  else edm::LogError("SiStripGainESProducer::SiStripGainNormalizationFunction") << "ERROR: unrecognized record name " << recordName << std::endl
93  << "please specify one of: SiStripApvGainRcd, SiStripApvGain2Rcd, SiStripApvGain3Rcd" << std::endl;
94 }
95 
96 template<typename TDependentRecord, typename TInputRecord>
98 {
99  // First clean up the pDD vector otherwise it will contain old handles referring to products no more in the es
100  pDD.clear();
101 
102  if(typeid(TDependentRecord)==typeid(SiStripGainRcd) && typeid(TInputRecord)==typeid(SiStripApvGainRcd)){
103 
104  const SiStripGainRcd& a = dynamic_cast<const SiStripGainRcd&>(iRecord);
105 
106  fillApvGain( a, apvgain_[0], pDD );
107  // Create a new gain object and insert the ApvGain
108  SiStripGain * gain = new SiStripGain( *(pDD[0].product()), getNFactor(0), apvgain_[0] );
109 
110  if( apvgain_.size() > 1 ) {
111  for( unsigned int i=1; i<apvgain_.size(); ++i ) {
112  fillApvGain( a, apvgain_[i], pDD );
113  // Add the new ApvGain to the gain object
114  gain->multiply(*(pDD[i].product()), getNFactor(i), apvgain_[i]);
115  }
116  }
117  return gain;
118 
119  }else if(typeid(TDependentRecord)==typeid(SiStripGainSimRcd) && typeid(TInputRecord)==typeid(SiStripApvGainSimRcd)){
120 
121  const SiStripGainSimRcd& a = dynamic_cast<const SiStripGainSimRcd&>(iRecord);
122 
123  pDD.push_back(edm::ESHandle<SiStripApvGain>());
125  SiStripGain * gain = new SiStripGain( *(pDD[0].product()), getNFactor(0), apvgain_[0] );
126 
127  if( apvgain_.size() > 1 ) {
128  for( unsigned int i=1; i<apvgain_.size(); ++i ) {
129  pDD.push_back(edm::ESHandle<SiStripApvGain>());
130  a.getRecord<SiStripApvGainSimRcd>().get(apvgain_[i].second, pDD[i]);
131  gain->multiply(*(pDD[i].product()), getNFactor(i), apvgain_[i]);
132  }
133  }
134  return gain;
135  }
136 
137  edm::LogError("SiStripGainESProducer") << "[SiStripGainNormalizationFunction] - ERROR: asking for a pair of records different from <SiStripGainRcd,SiStripApvGainRcd> and <SiStripGainSimRcd,SiStripApvGainSimRcd>" << std::endl;
138  return new SiStripGain();
139 }
140 
141 template<typename TDependentRecord, typename TInputRecord>
143  double NFactor=0.;
144 
145  if(automaticMode_ || printdebug_ ){
146 
147  std::vector<uint32_t> DetIds;
148  pDD[apvGainIndex]->getDetIds(DetIds);
149 
150  double SumOfGains=0.;
151  int NGains=0;
152 
153  for(std::vector<uint32_t>::const_iterator detit=DetIds.begin(); detit!=DetIds.end(); detit++){
154 
155  SiStripApvGain::Range detRange = pDD[apvGainIndex]->getRange(*detit);
156 
157  int iComp=0;
158 
159  for(std::vector<float>::const_iterator apvit=detRange.first; apvit!=detRange.second; apvit++){
160 
161  SumOfGains+=(*apvit);
162  NGains++;
163  if (printdebug_)
164  edm::LogInfo("SiStripGainESProducer::produce()")<< "detid/component: " << *detit <<"/"<<iComp<< " gain factor " <<*apvit ;
165  iComp++;
166  }
167  }
168 
169  if(automaticMode_){
170  if(SumOfGains>0 && NGains>0){
171  NFactor=SumOfGains/NGains;
172  }
173  else{
174  edm::LogError("SiStripGainESProducer::produce() - ERROR: empty set of gain values received. Cannot compute normalization factor. Assuming 1 for such factor") << std::endl;
175  NFactor=1.;
176  }
177  }
178  }
179 
180  if(!automaticMode_){
181  NFactor=norm_[apvGainIndex];
182  }
183 
184  if (printdebug_) edm::LogInfo("SiStripGainESProducer")<< " putting A SiStrip Gain object in eventSetup with normalization factor " << NFactor ;
185  return NFactor;
186 }
187 #endif
void multiply(const SiStripApvGain &apvgain, const double &factor, const std::pair< std::string, std::string > &recordLabelPair)
Used to input additional gain values that will be multiplied to the first one.
Definition: SiStripGain.cc:17
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
std::vector< edm::ESHandle< SiStripApvGain > > pDD
void fillApvGain(const SiStripGainRcd &a, const std::pair< std::string, std::string > &recordLabelPair, std::vector< edm::ESHandle< SiStripApvGain > > &pDD)
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
U second(std::pair< T, U > const &p)
std::vector< edm::ParameterSet > apvGainLabels_
constexpr int NGains
double getNFactor(const int apvGainIndex)
std::pair< ContainerIterator, ContainerIterator > Range
SiStripGainESProducerTemplate(const edm::ParameterSet &)
std::vector< std::pair< std::string, std::string > > apvgain_
double a
Definition: hdecay.h:121
std::unique_ptr< SiStripGain > produce(const TDependentRecord &)
SiStripGain * SiStripGainNormalizationFunction(const TDependentRecord &iRecord)