test
CMS 3D CMS Logo

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