00001 #include "RecoLocalTracker/SiStripZeroSuppression/plugins/SiStripZeroSuppression.h"
00002
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "DataFormats/Common/interface/DetSetVector.h"
00008 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00009 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
00010 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingFactory.h"
00011 #include <memory>
00012
00013 SiStripZeroSuppression::
00014 SiStripZeroSuppression(edm::ParameterSet const& conf)
00015 : inputTags(conf.getParameter<std::vector<edm::InputTag> >("RawDigiProducersList")),
00016 algorithms(SiStripRawProcessingFactory::create(conf.getParameter<edm::ParameterSet>("Algorithms"))),
00017 storeCM(conf.getParameter<bool>("storeCM")),
00018 doAPVRestore(conf.getParameter<bool>("doAPVRestore")),
00019 mergeCollections(conf.getParameter<bool>("mergeCollections"))
00020
00021 {
00022
00023 if(doAPVRestore){
00024 produceRawDigis = conf.getParameter<bool>("produceRawDigis");
00025 produceCalculatedBaseline = conf.getParameter<bool>("produceCalculatedBaseline");
00026 produceBaselinePoints = conf.getParameter<bool>("produceBaselinePoints");
00027 storeInZScollBadAPV = conf.getParameter<bool>("storeInZScollBadAPV");
00028 useCMMeanMap = conf.getParameter<bool>("useCMMeanMap");
00029 fixCM = conf.getParameter<bool>("fixCM");
00030 } else {
00031 produceRawDigis = false;
00032 produceCalculatedBaseline = false;
00033 produceBaselinePoints = false;
00034 storeInZScollBadAPV = false;
00035 useCMMeanMap = false;
00036 fixCM = false;
00037 }
00038
00039 if(mergeCollections){
00040 storeCM = false;
00041 produceRawDigis = false;
00042 }
00043
00044 for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag ){
00045 produces< edm::DetSetVector<SiStripDigi> > (inputTag->instance());
00046 if(produceRawDigis)
00047 produces< edm::DetSetVector<SiStripRawDigi> > (inputTag->instance());
00048 }
00049
00050 if(produceCalculatedBaseline)
00051 produces< edm::DetSetVector<SiStripProcessedRawDigi> > ("BADAPVBASELINE");
00052
00053 if(produceBaselinePoints)
00054 produces< edm::DetSetVector<SiStripDigi> > ("BADAPVBASELINEPOINTS");
00055
00056 if(storeCM)
00057 produces< edm::DetSetVector<SiStripProcessedRawDigi> > ("APVCM");
00058
00059
00060 }
00061
00062 void SiStripZeroSuppression::
00063 produce(edm::Event& e, const edm::EventSetup& es) {
00064
00065
00066 algorithms->initialize(es);
00067 if( doAPVRestore && useCMMeanMap) algorithms->restorer->LoadMeanCMMap( e );
00068
00069 if(mergeCollections)
00070 this->CollectionMergedZeroSuppression(e);
00071 else
00072 this->StandardZeroSuppression(e);
00073
00074 }
00075
00076 inline void SiStripZeroSuppression::StandardZeroSuppression(edm::Event& e){
00077
00078 for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag ) {
00079
00080 edm::Handle< edm::DetSetVector<SiStripRawDigi> > input;
00081 e.getByLabel(*inputTag,input);
00082
00083 std::vector<edm::DetSet<SiStripDigi> > output_base;
00084 std::vector<edm::DetSet<SiStripRawDigi> > output_base_raw;
00085
00086 if (input->size())
00087 processRaw(*inputTag, *input, output_base, output_base_raw );
00088
00089 std::auto_ptr< edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(output_base) );
00090 e.put( output, inputTag->instance() );
00091
00092
00093 if(produceRawDigis){
00094 std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > outputraw(new edm::DetSetVector<SiStripRawDigi>(output_base_raw) );
00095 e.put(outputraw, inputTag->instance() );
00096
00097
00098 }
00099 }
00100
00101
00102 if(produceCalculatedBaseline){
00103 std::auto_ptr< edm::DetSetVector<SiStripProcessedRawDigi> > outputbaseline(new edm::DetSetVector<SiStripProcessedRawDigi>(output_baseline) );
00104 e.put(outputbaseline, "BADAPVBASELINE" );
00105 }
00106
00107 if(produceBaselinePoints){
00108 std::auto_ptr< edm::DetSetVector<SiStripDigi> > outputbaselinepoints(new edm::DetSetVector<SiStripDigi>(output_baseline_points) );
00109 e.put(outputbaselinepoints, "BADAPVBASELINEPOINTS" );
00110 }
00111
00112 if(storeCM){
00113 std::auto_ptr< edm::DetSetVector<SiStripProcessedRawDigi> > outputAPVCM(new edm::DetSetVector<SiStripProcessedRawDigi>(output_apvcm) );
00114 e.put( outputAPVCM,"APVCM");
00115 }
00116 }
00117
00118
00119 inline void SiStripZeroSuppression::CollectionMergedZeroSuppression(edm::Event& e){
00120
00121 for(tag_iterator_t inputTag = inputTags.begin(); inputTag != inputTags.end(); ++inputTag ) {
00122
00123 edm::Handle< edm::DetSetVector<SiStripDigi> > inputdigi;
00124 edm::Handle< edm::DetSetVector<SiStripRawDigi> > inputraw;
00125 e.getByLabel(*inputTag,inputdigi);
00126 e.getByLabel(*inputTag,inputraw);
00127
00128 std::vector<edm::DetSet<SiStripDigi> > outputdigi;
00129 std::vector<edm::DetSet<SiStripRawDigi> > outputraw;
00130
00131 if (inputraw->size())
00132 processRaw(*inputTag, *inputraw, outputdigi, outputraw );
00133
00134
00135 for ( std::vector<edm::DetSet<SiStripDigi> >::const_iterator itinputdigi = inputdigi->begin(); itinputdigi !=inputdigi->end(); ++itinputdigi) {
00136 outputdigi.push_back(*itinputdigi);
00137 }
00138
00139 std::auto_ptr< edm::DetSetVector<SiStripDigi> > output(new edm::DetSetVector<SiStripDigi>(outputdigi) );
00140 e.put( output, inputTag->instance() );
00141
00142 }
00143
00144 }
00145
00146 inline
00147 void SiStripZeroSuppression::
00148 processRaw(const edm::InputTag& inputTag, const edm::DetSetVector<SiStripRawDigi>& input, std::vector<edm::DetSet<SiStripDigi> >& output, std::vector<edm::DetSet<SiStripRawDigi> >& outputraw) {
00149
00150 if(storeCM){
00151 output_apvcm.clear();
00152 output_apvcm.reserve(16000);
00153 }
00154
00155 if(produceCalculatedBaseline){
00156 output_baseline.clear();
00157 output_baseline.reserve(16000);
00158 }
00159
00160 if(produceBaselinePoints){
00161 output_baseline_points.clear();
00162 output_baseline_points.reserve(16000);
00163 }
00164
00165 output.reserve(10000);
00166 outputraw.reserve(10000);
00167 for ( edm::DetSetVector<SiStripRawDigi>::const_iterator
00168 rawDigis = input.begin(); rawDigis != input.end(); ++rawDigis) {
00169
00170 edm::DetSet<SiStripDigi> suppressedDigis(rawDigis->id);
00171
00172 int16_t nAPVflagged = 0;
00173
00174 if ( "ProcessedRaw" == inputTag.instance()) {
00175 std::vector<int16_t> processedRawDigis, processedRawDigisCopy ;
00176 transform(rawDigis->begin(), rawDigis->end(), back_inserter(processedRawDigis), boost::bind(&SiStripRawDigi::adc , _1));
00177 if( doAPVRestore ){
00178 processedRawDigisCopy.assign(processedRawDigis.begin(), processedRawDigis.end());
00179 }
00180 algorithms->subtractorCMN->subtract( rawDigis->id, processedRawDigis);
00181 if( doAPVRestore ){
00182 nAPVflagged = algorithms->restorer->inspect( rawDigis->id, processedRawDigisCopy, algorithms->subtractorCMN->getAPVsCM() );
00183 algorithms->restorer->restore( processedRawDigis );
00184 }
00185 algorithms->suppressor->suppress( processedRawDigis, suppressedDigis );
00186
00187 const std::vector< std::pair<short,float> >& vmedians= algorithms->subtractorCMN->getAPVsCM();
00188 if(storeCM) this->storeCMN(rawDigis->id, vmedians);
00189 if(produceCalculatedBaseline&& nAPVflagged > 0){
00190 std::map< uint16_t, std::vector < int16_t> >& baselinemap = algorithms->restorer->GetBaselineMap();
00191 this->storeBaseline(rawDigis->id, vmedians, baselinemap);
00192 }
00193
00194
00195
00196
00197 } else if ( "VirginRaw" == inputTag.instance()) {
00198 std::vector<int16_t> processedRawDigis(rawDigis->size()), processedRawDigisCopy;
00199 algorithms->subtractorPed->subtract( *rawDigis, processedRawDigis);
00200 if( doAPVRestore ){
00201 processedRawDigisCopy.assign(processedRawDigis.begin(), processedRawDigis.end());
00202 }
00203 algorithms->subtractorCMN->subtract( rawDigis->id, processedRawDigis);
00204 if( doAPVRestore ){
00205 nAPVflagged = algorithms->restorer->inspect( rawDigis->id, processedRawDigisCopy, algorithms->subtractorCMN->getAPVsCM() );
00206 algorithms->restorer->restore( processedRawDigis );
00207 }
00208 algorithms->suppressor->suppress( processedRawDigis, suppressedDigis );
00209
00210 const std::vector< std::pair<short,float> >& vmedians = algorithms->subtractorCMN->getAPVsCM();
00211 if(storeCM) this->storeCMN(rawDigis->id, vmedians);
00212 if(produceCalculatedBaseline&& nAPVflagged > 0){
00213 std::map< uint16_t, std::vector < int16_t> >& baselinemap = algorithms->restorer->GetBaselineMap();
00214 this->storeBaseline(rawDigis->id, vmedians, baselinemap);
00215 }
00216
00217
00218
00219
00220
00221 } else
00222
00223 throw cms::Exception("Unknown input type")
00224 << inputTag.instance() << " unknown. SiStripZeroZuppression can only process types \"VirginRaw\" and \"ProcessedRaw\" ";
00225
00226 if (suppressedDigis.size() && (storeInZScollBadAPV || nAPVflagged ==0))
00227 output.push_back(suppressedDigis);
00228
00229 if (produceRawDigis && nAPVflagged > 0)
00230 outputraw.push_back(*rawDigis);
00231
00232 }
00233
00234 }
00235
00236
00237 inline
00238 void SiStripZeroSuppression::storeBaseline(uint32_t id, const std::vector< std::pair<short,float> >& vmedians, std::map< uint16_t, std::vector < int16_t> >& baselinemap){
00239
00240 edm::DetSet<SiStripProcessedRawDigi> baselineDetSet(id);
00241 std::map< uint16_t, std::vector < int16_t> >::iterator itBaselineMap;
00242
00243 for(size_t i=0;i<vmedians.size();++i){
00244 uint16_t APVn = vmedians[i].first;
00245 float median = vmedians[i].second;
00246 itBaselineMap = baselinemap.find(APVn);
00247 if(itBaselineMap==baselinemap.end()){
00248 for(size_t strip=0; strip < 128; ++strip) baselineDetSet.push_back(SiStripProcessedRawDigi(median));
00249 } else {
00250 for(size_t strip=0; strip < 128; ++strip) baselineDetSet.push_back(SiStripProcessedRawDigi((itBaselineMap->second)[strip]));
00251 }
00252
00253 }
00254
00255 if(baselineDetSet.size())
00256 output_baseline.push_back(baselineDetSet);
00257
00258 }
00259
00260 inline
00261 void storeBaselinePoints(uint32_t id, std::vector< std::map< uint16_t, int16_t> >& BasPointVec){
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 }
00282
00283 inline
00284 void SiStripZeroSuppression::storeCMN(uint32_t id, const std::vector< std::pair<short,float> >& vmedians){
00285
00286 edm::DetSet<SiStripProcessedRawDigi> apvDetSet(id);
00287 short apvNb=0;
00288 for(size_t i=0;i<vmedians.size();++i){
00289 if(vmedians[i].first>apvNb){
00290 for(int i=0;i<vmedians[i].first-apvNb;++i){
00291 apvDetSet.push_back(SiStripProcessedRawDigi(0.));
00292 apvNb++;
00293 }
00294 }
00295 apvDetSet.push_back(SiStripProcessedRawDigi(vmedians[i].second));
00296
00297 apvNb++;
00298 }
00299 if(fixCM) algorithms->restorer->fixAPVsCM( apvDetSet );
00300 if(apvDetSet.size())
00301 output_apvcm.push_back(apvDetSet);
00302
00303 }