CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripZeroSuppression.cc
Go to the documentation of this file.
2 
12 #include <memory>
13 
16  : inputTags(conf.getParameter<std::vector<edm::InputTag> >("RawDigiProducersList")),
17  algorithms(SiStripRawProcessingFactory::create(conf.getParameter<edm::ParameterSet>("Algorithms"))),
18  storeCM(conf.getParameter<bool>("storeCM")),
19  mergeCollections(conf.getParameter<bool>("mergeCollections"))
20 
21 {
22 
23  produceRawDigis = conf.getParameter<bool>("produceRawDigis");
24  produceCalculatedBaseline = conf.getParameter<bool>("produceCalculatedBaseline");
25  produceBaselinePoints = conf.getParameter<bool>("produceBaselinePoints");
26  storeInZScollBadAPV = conf.getParameter<bool>("storeInZScollBadAPV");
27  fixCM = conf.getParameter<bool>("fixCM");
28 
29  if(mergeCollections){
30  storeCM = false;
31  produceRawDigis = false;
32  DigisToMergeZS = consumes< edm::DetSetVector<SiStripDigi> >(conf.getParameter<edm::InputTag>("DigisToMergeZS"));
33  DigisToMergeVR = consumes< edm::DetSetVector<SiStripRawDigi> >(conf.getParameter<edm::InputTag>("DigisToMergeVR"));
34  produces< edm::DetSetVector<SiStripDigi> > ("ZeroSuppressed");
35  }
36 
37  for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag ){
38  produces< edm::DetSetVector<SiStripDigi> > (inputTag->instance());
39  if(produceRawDigis) produces< edm::DetSetVector<SiStripRawDigi> > (inputTag->instance());
40  if(produceCalculatedBaseline) produces< edm::DetSetVector<SiStripProcessedRawDigi> > ("BADAPVBASELINE"+inputTag->instance());
41  if(produceBaselinePoints) produces< edm::DetSetVector<SiStripDigi> > ("BADAPVBASELINEPOINTS"+inputTag->instance());
42  if(storeCM) produces< edm::DetSetVector<SiStripProcessedRawDigi> > ("APVCM"+inputTag->instance());
43 // tokens consumes<reco::BeamSpot>(
44  }
45  inputTokens = edm::vector_transform( inputTags, [this](edm::InputTag const & tag) { return consumes< edm::DetSetVector<SiStripRawDigi> >(tag);} );
46 
47 
48 
49 }
50 
53 
54  algorithms->initialize(es, e);
55 
56  if(mergeCollections){
58  }else{
59  this->StandardZeroSuppression(e);
60  }
61 }
62 
64 
65  token_iterator_t inputToken = inputTokens.begin();
66  for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag,++inputToken ) {
67 
69  e.getByToken(*inputToken,input);
70 
71  if (input->size())
72  processRaw(*inputTag, *input);
73 
74  std::auto_ptr< edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(output_base) );
75  e.put( output, inputTag->instance() );
76 
77  if(produceRawDigis){
78  std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > outputraw(new edm::DetSetVector<SiStripRawDigi>(output_base_raw) );
79  e.put(outputraw, inputTag->instance() );
80  }
81 
83  std::auto_ptr< edm::DetSetVector<SiStripProcessedRawDigi> > outputbaseline(new edm::DetSetVector<SiStripProcessedRawDigi>(output_baseline) );
84  e.put(outputbaseline, "BADAPVBASELINE"+inputTag->instance() );
85  }
86 
88  std::auto_ptr< edm::DetSetVector<SiStripDigi> > outputbaselinepoints(new edm::DetSetVector<SiStripDigi>(output_baseline_points) );
89  e.put(outputbaselinepoints, "BADAPVBASELINEPOINTS"+inputTag->instance() );
90  }
91 
92  if(storeCM){
93  std::auto_ptr< edm::DetSetVector<SiStripProcessedRawDigi> > outputAPVCM(new edm::DetSetVector<SiStripProcessedRawDigi>(output_apvcm) );
94  e.put( outputAPVCM,"APVCM"+inputTag->instance());
95  }
96 
97  }
98 }
99 
100 
101 
102 
103 inline
106 
107  output_apvcm.clear();
108  output_baseline.clear();
109  output_baseline_points.clear();
110  output_base.clear();
111  output_base_raw.clear();
112 
113  if(storeCM) output_apvcm.reserve(16000);
114  if(produceCalculatedBaseline) output_baseline.reserve(16000);
116  if(produceRawDigis) output_base_raw.reserve(16000);
117  output_base.reserve(16000);
118 
119 
121  rawDigis = input.begin(); rawDigis != input.end(); ++rawDigis) {
122 
123  edm::DetSet<SiStripDigi> suppressedDigis(rawDigis->id);
124  int16_t nAPVflagged = 0;
125 
126  if ( "ProcessedRaw" == inputTag.instance()) nAPVflagged = algorithms->SuppressProcessedRawData(*rawDigis, suppressedDigis);
127  else if ( "VirginRaw" == inputTag.instance()) nAPVflagged = algorithms->SuppressVirginRawData(*rawDigis, suppressedDigis);
128  else
129  throw cms::Exception("Unknown input type")
130  << inputTag.instance() << " unknown. SiStripZeroZuppression can only process types \"VirginRaw\" and \"ProcessedRaw\" ";
131 
132  //here storing the output
133  this->storeExtraOutput(rawDigis->id, nAPVflagged);
134  if (suppressedDigis.size() && (storeInZScollBadAPV || nAPVflagged ==0))
135  output_base.push_back(suppressedDigis);
136 
137  if (produceRawDigis && nAPVflagged > 0){
138  edm::DetSet<SiStripRawDigi> outRawDigis(rawDigis->id);
139  this->formatRawDigis(rawDigis, outRawDigis);
140  output_base_raw.push_back(outRawDigis);
141  }
142 
143  }
144 
145 }
146 
147 inline
149  rawDigis, edm::DetSet<SiStripRawDigi>& outRawDigis){
150 
151  const std::vector<bool>& apvf = algorithms->GetAPVFlags();
152  edm::DetSet<SiStripRawDigi>::const_iterator itRawDigis = rawDigis->begin();
153 
154  uint32_t strip=0;
155  for (; itRawDigis != rawDigis->end(); ++itRawDigis){
156  int16_t APVn = strip/128;
157  if(apvf[APVn]) outRawDigis.push_back(*itRawDigis);
158  else outRawDigis.push_back(SiStripRawDigi(0));
159  ++strip;
160  }
161 
162 }
163 
164 
165 inline
166 void SiStripZeroSuppression::storeExtraOutput(uint32_t id, int16_t nAPVflagged){
167 
168  const std::vector< std::pair<short,float> >& vmedians = algorithms->getAPVsCM();
169  if(storeCM) this->storeCMN(id, vmedians);
170  if(nAPVflagged > 0){
171  if(produceCalculatedBaseline) this->storeBaseline(id, vmedians);
173  }
174 }
175 
176 
177 inline
178 void SiStripZeroSuppression::storeBaseline(uint32_t id, const std::vector< std::pair<short,float> >& vmedians){
179 
180  std::map< uint16_t, std::vector < int16_t> >& baselinemap = algorithms->GetBaselineMap();
181 
182  edm::DetSet<SiStripProcessedRawDigi> baselineDetSet(id);
183  std::map< uint16_t, std::vector < int16_t> >::iterator itBaselineMap;
184 
185  for(size_t i=0;i<vmedians.size();++i){
186  uint16_t APVn = vmedians[i].first;
187  float median = vmedians[i].second;
188  itBaselineMap = baselinemap.find(APVn);
189  if(itBaselineMap==baselinemap.end()){
190  for(size_t strip=0; strip < 128; ++strip) baselineDetSet.push_back(SiStripProcessedRawDigi(median));
191  } else {
192  for(size_t strip=0; strip < 128; ++strip) baselineDetSet.push_back(SiStripProcessedRawDigi((itBaselineMap->second)[strip]));
193  }
194 
195  }
196 
197  if(baselineDetSet.size())
198  output_baseline.push_back(baselineDetSet);
199 
200 }
201 
202 inline
204 
205  std::map< uint16_t, std::map< uint16_t, int16_t> >& BasPointVec = algorithms->GetSmoothedPoints();
206  std::map< uint16_t, std::map< uint16_t, int16_t> >::iterator itBasPointVect = BasPointVec.begin() ;
207  std::map< uint16_t, int16_t>::iterator itBaselinePointMap;
208 
209  edm::DetSet<SiStripDigi> baspointDetSet(id);
210 
211  for(; itBasPointVect != BasPointVec.end(); ++itBasPointVect){
212  uint16_t APVn= itBasPointVect->first;
213  itBaselinePointMap =itBasPointVect->second.begin();
214  for(;itBaselinePointMap != itBasPointVect->second.end(); ++itBaselinePointMap){
215  uint16_t bpstrip = (itBaselinePointMap->first) + APVn*128;
216  int16_t bp = itBaselinePointMap->second;
217  baspointDetSet.push_back(SiStripDigi(bpstrip,bp+128));
218 
219  }
220  }
221 
222 
223  if(baspointDetSet.size())
224  output_baseline_points.push_back(baspointDetSet);
225 
226 }
227 
228 inline
229 void SiStripZeroSuppression::storeCMN(uint32_t id, const std::vector< std::pair<short,float> >& vmedians){
230 
232  short apvNb=0;
233 
234  std::vector<bool> apvf;
235  apvf.clear();
236  apvf.insert(apvf.begin(), 6, false);
237 
238  if(fixCM){
239  std::vector<bool>& apvFlagged = algorithms->GetAPVFlags();
240  for(uint16_t it=0; it< apvFlagged.size(); ++it) apvf[it] = apvFlagged[it];
241  }
242 
243  for(size_t i=0;i<vmedians.size();++i){
244  if(vmedians[i].first>apvNb){
245  for(int i=0;i<vmedians[i].first-apvNb;++i){
246  apvDetSet.push_back(SiStripProcessedRawDigi(-999.));
247  apvNb++;
248  }
249  }
250 
251  if(fixCM&&apvf[vmedians[i].first]){
252  apvDetSet.push_back(SiStripProcessedRawDigi(-999.));
253  }else{
254  apvDetSet.push_back(SiStripProcessedRawDigi(vmedians[i].second));
255  }
256  apvNb++;
257  }
258 
259  if(apvDetSet.size())
260  output_apvcm.push_back(apvDetSet);
261 
262 }
263 
264 
266 
267  std::cout<< "starting Merging" << std::endl;
270  e.getByToken(DigisToMergeZS,inputdigi);
271  e.getByToken(DigisToMergeVR,inputraw);
272 
273  std::cout << inputdigi->size() << " " << inputraw->size() << std::endl;
274 
275  if (inputraw->size()){
276 
277  std::vector<edm::DetSet<SiStripDigi> > outputdigi;
278  outputdigi.clear();
279 
280  //std::cout << "copying the input ZS to the output ZS collection" << std::endl;
281  for ( edm::DetSetVector<SiStripDigi>::const_iterator Digis = inputdigi->begin(); Digis != inputdigi->end(); ++Digis) outputdigi.push_back(*Digis);
282 
283 
284  std::cout << "looping over the raw data collection" << std::endl;
285  for ( edm::DetSetVector<SiStripRawDigi>::const_iterator rawDigis = inputraw->begin(); rawDigis != inputraw->end(); ++rawDigis) {
286 
287  edm::DetSet<SiStripRawDigi>::const_iterator itRawDigis = rawDigis->begin();
288  uint16_t nAPV = rawDigis->size()/128;
289  uint32_t rawDetId = rawDigis->id;
290 
291  std::vector<bool> restoredAPV;
292  restoredAPV.clear();
293  restoredAPV.insert(restoredAPV.begin(), nAPV, false);
294 
295 
296  bool isModuleRestored = false;
297  for( uint16_t strip =0; strip < rawDigis->size();++strip){
298  if(itRawDigis[strip].adc()!=0){
299  restoredAPV[strip/128] = true;
300  isModuleRestored = true;
301  }
302  }
303 
304 
305  if(isModuleRestored){
306  std::cout << "apply the ZS to the raw data collection" << std::endl;
307  edm::DetSet<SiStripDigi> suppressedDigis(rawDetId);
308  std::vector<int16_t> processedRawDigis(rawDigis->size());
309  algorithms->SuppressVirginRawData(*rawDigis, suppressedDigis);
310 
311  if(suppressedDigis.size()){
312  std::cout << "looking for the detId with the new ZS in the collection of the zero suppressed data" << std::endl;
313  std::vector<edm::DetSet<SiStripDigi> >::iterator zsModule = outputdigi.begin();
314  //std::vector<edm::DetSet<SiStripDigi> >::iterator LastLowerIdDigis = zsModule;
315 
316  uint32_t zsDetId = zsModule->id;
317  bool isModuleInZscollection = false;
318  while((zsDetId <= rawDetId)&&(zsModule != outputdigi.end())&&(!isModuleInZscollection)){
319  //std::cout << rawDetId << " ==== " << zsDetId << std::endl;
320  if( zsDetId == rawDetId){
321  isModuleInZscollection = true;
322  }else{
323  ++zsModule;
324  zsDetId = zsModule->id;
325  }
326  }
327  std::cout << "after the look " << rawDetId << " ==== " << zsDetId << std::endl;
328  std::cout << "exiting looking for the detId with the new ZS in the collection of the zero suppressed data" << std::endl;
329 
330  //creating the map containing the digis (in rawdigi format) merged
331  std::vector<uint16_t> MergedRawDigis;
332  MergedRawDigis.clear();
333  MergedRawDigis.insert(MergedRawDigis.begin(), nAPV*128, 0);
334 
335  uint32_t count=0; // to be removed...
336 
337  edm::DetSet<SiStripDigi> newDigiToIndert(rawDetId);
338  if(!isModuleInZscollection){
339  std::cout << "WE HAVE A PROBLEM, THE MODULE IS NTOT FOUND" << std::endl;
340  outputdigi.insert(zsModule, newDigiToIndert);
341  --zsModule;
342  std::cout << "new module id -1 " << zsModule->id << std::endl;
343  ++zsModule;
344  std::cout << "new module id " << zsModule->id << std::endl;
345  ++zsModule;
346  std::cout << "new module id +1 " << zsModule->id << std::endl;
347  --zsModule;
348 
349  } else {
350  std::cout << "inserting only the digis for not restored APVs" << std::endl;
351  std::cout << "size : " << zsModule->size() << std::endl;
352  edm::DetSet<SiStripDigi>::iterator itZsModule = zsModule->begin();
353  for(; itZsModule != zsModule->end(); ++itZsModule){
354  uint16_t adc = itZsModule->adc();
355  uint16_t strip = itZsModule->strip();
356  if(!restoredAPV[strip/128]){
357  MergedRawDigis[strip] = adc;
358  ++count;
359  std::cout << "original count: "<< count << " strip: " << strip << " adc: " << adc << std::endl;
360  }
361  }
362 
363  }
364 
365  std::cout << "size of digis to keep: " << count << std::endl;
366  std::cout << "inserting only the digis for the restored APVs" << std::endl;
367  std::cout << "size : " << suppressedDigis.size() << std::endl;
368  edm::DetSet<SiStripDigi>::iterator itSuppDigi = suppressedDigis.begin();
369  for(; itSuppDigi != suppressedDigis.end(); ++itSuppDigi){
370  uint16_t adc = itSuppDigi->adc();
371  uint16_t strip = itSuppDigi->strip();
372  if(restoredAPV[strip/128]){
373  MergedRawDigis[strip] = adc;
374  std::cout << "new suppressed strip: " << strip << " adc: " << adc << std::endl;
375  }
376  }
377 
378 
379 
380  std::cout << "suppressing the raw digis" << std::endl;
381  zsModule->clear();
382  for(uint16_t strip=0; strip < MergedRawDigis.size(); ++strip){
383  uint16_t adc = MergedRawDigis[strip];
384  if(adc) zsModule->push_back(SiStripDigi(strip, adc));
385  }
386  std::cout << "size zsModule after the merging: " << zsModule->size() << std::endl;
387  if((count + suppressedDigis.size()) != zsModule->size()) std::cout << "WE HAVE A PROBLEM!!!! THE NUMBER OF DIGIS IS NOT RIGHT==============" << std::endl;
388  std::cout << "exiting suppressing the raw digis" << std::endl;
389  }//if new ZS digis size
390  } //if module restored
391  }//loop over raw data collection
392 
393  uint32_t oldid =0;
394  for(edm::DetSetVector<SiStripDigi>::const_iterator dg = outputdigi.begin(); dg != outputdigi.end(); ++dg){
395  uint32_t iddg = dg->id;
396  if(iddg < oldid){
397  std::cout<< "NOT IN THE RIGHT ORGER" << std:: endl;
398  std::cout<< "======================="<< std:: endl;
399  }
400  oldid = iddg;
401  }
402 
403 
404  std::cout << "write the output vector" << std::endl;
405  std::auto_ptr< edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(outputdigi) );
406  e.put( output, "ZeroSuppressed" );
407 
408 
409  }//if inputraw.size
410 
411 
412 
413 }
414 
416 
417  for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag ) {
418 
421  e.getByLabel(*inputTag,inputdigi);
422  e.getByLabel(*inputTag,inputraw);
423 
424  std::vector<edm::DetSet<SiStripDigi> > outputdigi;
425  std::vector<edm::DetSet<SiStripRawDigi> > outputraw;
426 
427 
428  if (inputraw->size())
429  processRaw(*inputTag, *inputraw);
430 
431 
432  for ( std::vector<edm::DetSet<SiStripDigi> >::const_iterator itinputdigi = inputdigi->begin(); itinputdigi !=inputdigi->end(); ++itinputdigi) {
433  output_base.push_back(*itinputdigi);
434  }
435 
436  std::auto_ptr< edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(output_base) );
437  e.put( output, inputTag->instance() );
438 
439  }
440 
441 }
int adc(sample_type sample)
get the ADC sample (12 bits)
iterator end()
Definition: DetSet.h:60
std::vector< edm::InputTag > inputTags
T getParameter(std::string const &) const
virtual void produce(edm::Event &, const edm::EventSetup &)
int i
Definition: DBlmapReader.cc:9
void formatRawDigis(edm::DetSetVector< SiStripRawDigi >::const_iterator, edm::DetSet< SiStripRawDigi > &)
void push_back(const T &t)
Definition: DetSet.h:68
std::vector< edm::DetSet< SiStripDigi > > output_base
void CollectionMergedZeroSuppression(edm::Event &)
collection_type::iterator iterator
Definition: DetSet.h:32
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
A signed Digi for the silicon strip detector, containing only adc information, and suitable for stori...
void processRaw(const edm::InputTag &, const edm::DetSetVector< SiStripRawDigi > &)
std::vector< edm::DetSet< SiStripDigi > > output_baseline_points
void StandardZeroSuppression(edm::Event &)
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
static std::string const input
Definition: EdmProvDump.cc:43
edm::EDGetTokenT< edm::DetSetVector< SiStripDigi > > DigisToMergeZS
size_type size() const
Definition: DetSet.h:63
U second(std::pair< T, U > const &p)
std::auto_ptr< SiStripRawProcessingAlgorithms > algorithms
void storeExtraOutput(uint32_t, int16_t)
void storeCMN(uint32_t, const std::vector< std::pair< short, float > > &)
void MergeCollectionsZeroSuppression(edm::Event &)
std::vector< edm::DetSet< SiStripProcessedRawDigi > > output_apvcm
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
std::vector< edm::DetSet< SiStripProcessedRawDigi > > output_baseline
A Digi for the silicon strip detector, containing both strip and adc information, and suitable for st...
Definition: SiStripDigi.h:12
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:365
tuple conf
Definition: dbtoconf.py:185
void storeBaseline(uint32_t, const std::vector< std::pair< short, float > > &)
iterator begin()
Definition: DetSet.h:59
std::vector< edm::DetSet< SiStripRawDigi > > output_base_raw
edm::EDGetTokenT< edm::DetSetVector< SiStripRawDigi > > DigisToMergeVR
SiStripZeroSuppression(const edm::ParameterSet &)
token_v::const_iterator token_iterator_t
tuple cout
Definition: gather_cfg.py:121
iterator begin()
Return an iterator to the first DetSet.
Definition: DetSetVector.h:350
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:108
A Digi for the silicon strip detector, containing only adc information, and suitable for storing raw ...
std::string const & instance() const
Definition: InputTag.h:43
SurfaceDeformation * create(int type, const std::vector< double > &params)
std::vector< edm::InputTag >::const_iterator tag_iterator_t