CMS 3D CMS Logo

SiStripFEDEmulator.cc
Go to the documentation of this file.
4 
5 #include "boost/bind.hpp"
6 
7 using edm::LogError;
8 using edm::LogInfo;
9 using edm::LogWarning;
10 
11 namespace sistrip{
12 
13  const char* FEDEmulator::messageLabel_ = "SiStripFEDEmulator";
14 
15 
17  byModule_ = false;
18  minStrip_ = 0;
19  maxStrip_ = 0;
20  pedestals_.clear();
21  noises_.clear();
22  medians_.clear();
23 
24  detId_ = 0;
25  nPairs_ = 0;
26  pair_ = 0;
27 
28  }
29 
30 
31 
33 
34  }
35 
36  void FEDEmulator::initialise(const bool byModule)
37  {
38  byModule_ = byModule;
39  pedestals_.clear();
40  noises_.clear();
41  medians_.clear();
42  }
43 
44  void FEDEmulator::initialiseModule(const uint32_t aDetId,
45  const uint32_t aNPairs,
46  const uint32_t aPair)
47  {
48  detId_ = aDetId;
49  nPairs_ = aNPairs;
50  pair_ = aPair;
51 
52  minStrip_ = 0;
54  if (!byModule_) {
55  minStrip_ = sistrip::STRIPS_PER_FEDCH*pair_;
56  maxStrip_ = sistrip::STRIPS_PER_FEDCH*(pair_+1);
57  }
58 
59  //need resize because it will not be filled by push_back...
60  pedestals_.clear();
61  pedestals_.resize(nPairs_*sistrip::STRIPS_PER_FEDCH);
62 
63  noises_.clear();
64  noises_.resize(nPairs_*sistrip::STRIPS_PER_FEDCH);
65 
66  medians_.clear();
67  medians_.reserve(nPairs_*2);
68 
69  }
70 
72  try {
73  SiStripPedestals::Range pedestalsRange = aHandle->getRange(detId_);
74  aHandle->allPeds(pedestals_,pedestalsRange);
75  }
76  catch(const cms::Exception& e) {
77  LogError(messageLabel_) << " Something wrong whilst getting pedestals: size = "
78  << pedestals_.size() << " detId = "
79  << detId_ << std::endl << e.what();
80  //throw cms::Exception("ProductNotFound") << "Problem with pedestals..." ;
81  }
82  }//end of retrievePedestals method.
83 
85  try {
86  SiStripNoises::Range noisesRange = aHandle->getRange(detId_);
87  aHandle->allNoises(noises_,noisesRange);
88  }
89  catch(const cms::Exception& e) {
90  LogError(messageLabel_) << " Something wrong whilst getting noises: size = "
91  << noises_.size() << " detId = "
92  << detId_ << std::endl << e.what();
93  //throw cms::Exception("ProductNotFound") << "Problem with noises..." ;
94  }
95  }//end of retrieveNoises method.
96 
98  std::vector<SiStripRawDigi> & pedsDetSetData,
99  std::vector<SiStripProcessedRawDigi> & noiseDetSetData,
100  std::vector<SiStripRawDigi> & pedSubtrDetSetData,
101  std::vector<uint32_t> & medsDetSetData,
102  const bool fillApvsForCM)
103  {
104 
105  edm::DetSet<SiStripRawDigi>::const_iterator digi_it = inputChannel->begin();
107  uint32_t lCount = 0;
108  std::vector<std::vector<uint16_t> > apvs;
109  apvs.reserve(nPairs_*2);
110 
111  //subtract pedestals and fill apv vector
112  //for median calculation
113  //in a module, 256*nPairs, but here we are dealing
114  //channel by channel, so 256 values at a time in the pedestal object.
115  //reordered = 0-127 APV0 128-255 APV1
116 
117  std::vector<uint16_t> apvVec;;
118  apvVec.reserve(sistrip::STRIPS_PER_APV);
119 
120  for (uint32_t st = minStrip_; st < maxStrip_; st++) {
121  uint16_t ped = static_cast<uint16_t>(pedestals_[st]);
122  pedsDetSetData.push_back(SiStripRawDigi(ped));
123 
124  float noise = noises_[st];
125  noiseDetSetData.push_back(SiStripProcessedRawDigi(noise));
126 
127  if (digi_it == inputChannel->end()) {
128  LogError(messageLabel_) << " Error, end of inputchannel reached for detid " << detId_ << "! Processed " << lCount << " strips." << std::endl;
129  break;
130  }
131 
132  int lVal = digi_it->adc() - ped;
133  if (lVal < 0) {
134  if (digi_it->adc()>0) LogDebug(messageLabel_) << " Negative value after pedestal subtraction ! Detid = " << detId_ << ", strip " << st << ", digi = " << digi_it->adc() << ", ped = " << ped << std::endl;
135  lVal = 0;
136  }
137  pedSubtrDetSetData.push_back( SiStripRawDigi(static_cast<uint16_t>(lVal)) );
138 
139 
140  if (fillApvsForCM) {
141  apvVec.push_back( static_cast<uint16_t>(lVal) );
142  }
143 
144  lCount++;
145  ++digi_it;
146 
147  if (lCount%sistrip::STRIPS_PER_APV == 0 && fillApvsForCM) {
148  if (apvVec.size()) apvs.push_back(apvVec);
149  apvVec.clear();
150  apvVec.reserve(sistrip::STRIPS_PER_APV);
151  }
152 
153  }//end of loop over strips
154 
155  if (fillApvsForCM){
156  if (apvs.size() != nPairs_*2) {
157  LogError(messageLabel_) << " Error, wrong output size for median calculation for detid " << detId_ << "! Processed " << apvs.size() << " APVs." << std::endl;
158  }
159 
160  //calculate common mode values
161  for (uint32_t iapv(0); iapv<apvs.size(); iapv++){
162  std::vector<uint16_t> lVec = apvs[iapv];
163 
164  std::vector<uint16_t>::iterator mid = lVec.begin() + (lVec.size()/2 - 1);
165  std::nth_element(lVec.begin(), mid, lVec.end());
166 
167  //std::sort(lVec.begin(),lVec.end());
168  //get the 64th element
169  //if (*mid != lVec.at(63)) exit(0);
170  //medians_.push_back(lVec.at(63));
171  //medsDetSetData.push_back(lVec.at(63));
172  medians_.push_back(*mid);
173  medsDetSetData.push_back(*mid);
174 
175  }
176  }
177 
178  }//end of FEDEmulator::subtractPedestals method.
179 
180 
181  void FEDEmulator::subtractCM(const std::vector<SiStripRawDigi> & pedSubtrDetSetData,
182  std::vector<SiStripRawDigi> & cmSubtrDetSetData)
183  {
184  //subtract Medians
185  std::vector<SiStripRawDigi>::const_iterator lDigi = pedSubtrDetSetData.begin();
186  uint32_t lCount = 0;
187 
188  for (uint32_t st = minStrip_; st < maxStrip_; st++) {
189  uint32_t lApvNum = static_cast<uint32_t>(lCount*1./sistrip::STRIPS_PER_APV);
190 
191  int value = lDigi->adc() - medians_.at(lApvNum);
192  if (value < 0) {
193  //FED doesn't handle negative values
194  value = 0;
195  }
196  cmSubtrDetSetData.push_back(SiStripRawDigi(static_cast<uint16_t>(value)));
197 
198  ++lDigi;
199  lCount++;
200  }
201 
202  }
203 
204  void FEDEmulator::zeroSuppress(const std::vector<SiStripRawDigi> & cmSubtrDetSetData,
205  edm::DetSet<SiStripDigi> & zsDetSetData,
206  const std::unique_ptr<SiStripRawProcessingAlgorithms> & algorithms)
207  {
208  //transform the input digis to a vector of integers
209  std::vector<int16_t> cmSubtrRawDigis;
210  transform(cmSubtrDetSetData.begin(),
211  cmSubtrDetSetData.end(),
212  back_inserter(cmSubtrRawDigis),
213  boost::bind(&SiStripRawDigi::adc ,_1)
214  );
215  algorithms->suppressor->suppress(cmSubtrRawDigis, 0,
216  zsDetSetData);
217 
218  }//end of FEDEmulator::zeroSuppress method.
219 
220 
221 
224  iApv != peds->end(); ++iApv) {
225  pedestals_.push_back(iApv->adc());
226  }
227  }
228 
231  iApv != noises->end(); ++iApv) {
232  noises_.push_back(iApv->adc());
233  }
234  }
235 
236  void FEDEmulator::fillMedians(const std::map<uint32_t,std::vector<uint32_t> >::const_iterator & meds){
237  std::vector<uint32_t>::const_iterator iApv = (meds->second).begin();
238  for ( ; iApv != (meds->second).end(); ++iApv) {
239  medians_.push_back(*iApv);
240  }
241  }
242 
243  void FEDEmulator::print(std::ostream & aOs) {
244  aOs << "===============================" << std::endl
245  << " ===== FEDEmulator::print =====" << std::endl
246  << " === byModule = " << byModule_ << std::endl
247  << " === minStrip = " << minStrip_ << std::endl
248  << " === maxStrip = " << maxStrip_ << std::endl
249  << " === size of pedestals = " << pedestals_.size() << std::endl
250  << " === size of noises = " << noises_.size() << std::endl
251  << " === size of medians = " << medians_.size() << std::endl
252  << " === detId = " << detId_ << std::endl
253  << " === nPairs = " << nPairs_ << std::endl
254  << " === pair = " << pair_ << std::endl
255  << "===============================" << std::endl;
256  }
257 
258 
259  void FEDEmulator::printPeds(std::ostream & aOs) {
260  aOs << "=========================================" << std::endl
261  << " ===== FEDEmulator::printPedestals =====" << std::endl;
262  for (unsigned int i(0); i<pedestals_.size(); i++){
263  if (i%32 == 0) aOs << std::endl;
264  aOs << pedestals_[i] << " " ;
265  }
266  aOs << "=========================================" << std::endl;
267  }
268 
269  void FEDEmulator::printNoises(std::ostream & aOs) {
270  aOs << "=========================================" << std::endl
271  << " ===== FEDEmulator::printNoises =====" << std::endl;
272  for (unsigned int i(0); i<noises_.size(); i++){
273  if (i%32 == 0) aOs << std::endl;
274  aOs << noises_[i] << " " ;
275  }
276  aOs << "=========================================" << std::endl;
277  }
278 
279  void FEDEmulator::printMeds(std::ostream & aOs) {
280  aOs << "=========================================" << std::endl
281  << " ===== FEDEmulator::printMedians =====" << std::endl;
282  for (unsigned int i(0); i<medians_.size(); i++){
283  if (i%32 == 0) aOs << std::endl;
284  aOs << medians_[i] << " " ;
285  }
286  aOs << "=========================================" << std::endl;
287  }
288 
289 
290 
291 
292 }//namespace sistrip
#define LogDebug(id)
virtual char const * what() const
Definition: Exception.cc:141
const uint16_t & adc() const
void push_back(const T &t)
Definition: DetSet.h:68
void subtractPedestals(const edm::DetSetVector< SiStripRawDigi >::const_iterator &inputChannel, std::vector< SiStripRawDigi > &pedsDetSetData, std::vector< SiStripProcessedRawDigi > &noiseDetSetData, std::vector< SiStripRawDigi > &pedSubtrDetSetData, std::vector< uint32_t > &medsDetSetData, const bool fillApvsForCM)
void printPeds(std::ostream &aOs)
A signed Digi for the silicon strip detector, containing only adc information, and suitable for stori...
void fillPeds(const edm::DetSetVector< SiStripRawDigi >::const_iterator &peds)
std::pair< ContainerIterator, ContainerIterator > Range
sistrip classes
void retrieveNoises(const edm::ESHandle< SiStripNoises > &aHandle)
std::vector< float > noises_
void fillMedians(const std::map< uint32_t, std::vector< uint32_t > >::const_iterator &meds)
void initialiseModule(const uint32_t aDetId, const uint32_t aNPairs, const uint32_t aPair)
#define end
Definition: vmac.h:37
Definition: value.py:1
std::vector< int > pedestals_
void subtractCM(const std::vector< SiStripRawDigi > &pedSubtrDetSetData, std::vector< SiStripRawDigi > &cmSubtrDetSetData)
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:361
static const uint16_t STRIPS_PER_FEDCH
void initialise(const bool byModule)
void allPeds(std::vector< int > &pefs, const Range &range) const
void zeroSuppress(const std::vector< SiStripRawDigi > &cmSubtrDetSetData, edm::DetSet< SiStripDigi > &zsDetSetData, const std::unique_ptr< SiStripRawProcessingAlgorithms > &algorithms)
void printNoises(std::ostream &aOs)
void print(std::ostream &aOs)
void printMeds(std::ostream &aOs)
std::vector< uint32_t > medians_
#define begin
Definition: vmac.h:30
static const uint16_t STRIPS_PER_APV
const Range getRange(const uint32_t detID) const
iterator begin()
Return an iterator to the first DetSet.
Definition: DetSetVector.h:346
void retrievePedestals(const edm::ESHandle< SiStripPedestals > &aHandle)
const Range getRange(const uint32_t &detID) const
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripNoises.h:48
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:104
A Digi for the silicon strip detector, containing only adc information, and suitable for storing raw ...
void allNoises(std::vector< float > &noises, const Range &range) const
void fillNoises(const edm::DetSetVector< SiStripProcessedRawDigi >::const_iterator &noise)
static const char * messageLabel_