CMS 3D CMS Logo

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