00001 #include "CalibTracker/SiStripAPVAnalysis/interface/ApvAnalysisFactory.h"
00002
00003
00004 #include "CalibTracker/SiStripAPVAnalysis/interface/TT6NTPedestalCalculator.h"
00005
00006 using namespace std;
00007 ApvAnalysisFactory::ApvAnalysisFactory(string theAlgorithmType, int theNumCMstripsInGroup, int theMaskCalcFlag, float theMaskNoiseCut,
00008 float theMaskDeadCut,
00009 float theMaskTruncCut,
00010 float theCutToAvoidSignal,
00011 int theEventInitNumber,
00012 int theEventIterNumber){
00013
00014 theAlgorithmType_ = theAlgorithmType;
00015 theNumCMstripsInGroup_ = theNumCMstripsInGroup;
00016 theMaskCalcFlag_ = theMaskCalcFlag;
00017 theMaskNoiseCut_ = theMaskNoiseCut;
00018 theMaskDeadCut_ = theMaskDeadCut;
00019 theMaskTruncCut_ = theMaskTruncCut;
00020 theCutToAvoidSignal_ = theCutToAvoidSignal;
00021 theEventInitNumber_ = theEventInitNumber;
00022 theEventIterNumber_ = theEventIterNumber;
00023
00024 }
00025
00026 ApvAnalysisFactory::ApvAnalysisFactory(const edm::ParameterSet& pset){
00027
00028 theCMType_ = pset.getParameter<string>("CMType");
00029 useDB_ = pset.getParameter<bool>("useDB");
00030
00031 theAlgorithmType_ = pset.getParameter<string>("CalculatorAlgorithm");
00032 theNumCMstripsInGroup_ = pset.getParameter<int>("NumCMstripsInGroup");
00033 theMaskCalcFlag_ = pset.getParameter<int>("MaskCalculationFlag");
00034
00035 theMaskNoiseCut_ = pset.getParameter<double>("MaskNoiseCut");
00036 theMaskDeadCut_ = pset.getParameter<double>("MaskDeadCut");
00037 theMaskTruncCut_ = pset.getParameter<double>("MaskTruncationCut");
00038 theCutToAvoidSignal_ = pset.getParameter<double>("CutToAvoidSignal");
00039
00040 theEventInitNumber_ = pset.getParameter<int>("NumberOfEventsForInit");
00041 theEventIterNumber_ = pset.getParameter<int>("NumberOfEventsForIteration");
00042 apvMap_.clear();
00043
00044
00045 }
00046
00047 ApvAnalysisFactory::~ApvAnalysisFactory(){
00048 ApvAnalysisFactory::ApvAnalysisMap::iterator it = apvMap_.begin();
00049 for(;it!=apvMap_.end();it++)
00050 {
00051 vector<ApvAnalysis*>::iterator myApv = (*it).second.begin();
00052 for(;myApv!=(*it).second.end();myApv++)
00053 deleteApv(*myApv);
00054 }
00055 apvMap_.clear();
00056 }
00057
00058
00059
00060 bool ApvAnalysisFactory::instantiateApvs(uint32_t detId, int numberOfApvs){
00061
00062
00063 ApvAnalysisFactory::ApvAnalysisMap::iterator CPos = apvMap_.find(detId);
00064 if(CPos != apvMap_.end()) {
00065 cout << " APVs for Detector Id " << detId << " already created !!!" << endl;;
00066 return false;
00067 }
00068 vector< ApvAnalysis* > temp;
00069 for(int i=0;i<numberOfApvs;i++)
00070 {
00071 ApvAnalysis* apvTmp = new ApvAnalysis(theEventIterNumber_);
00072
00073 constructAuxiliaryApvClasses(apvTmp,detId,i);
00074 temp.push_back(apvTmp);
00075 }
00076 apvMap_.insert(pair< uint32_t, vector< ApvAnalysis* > >(detId, temp));
00077 return true;
00078 }
00079
00080 std::vector<ApvAnalysis *> ApvAnalysisFactory::getApvAnalysis( const uint32_t nDET_ID)
00081 {
00082 ApvAnalysisMap::const_iterator _apvAnalysisIter = apvMap_.find( nDET_ID);
00083
00084 return apvMap_.end() != _apvAnalysisIter ? _apvAnalysisIter->second : std::vector<ApvAnalysis *>();
00085 }
00086
00087 void ApvAnalysisFactory::constructAuxiliaryApvClasses( ApvAnalysis* theAPV,
00088 uint32_t detId,
00089 int thisApv )
00090 {
00091
00092
00093
00094
00095
00096
00097
00098 TkPedestalCalculator* thePedestal =0;
00099 TkNoiseCalculator* theNoise=0;
00100 TkApvMask* theMask=0;
00101 TkCommonModeCalculator* theCM=0;
00102
00103 TkCommonMode* theCommonMode = new TkCommonMode();
00104 TkCommonModeTopology* theTopology = new TkCommonModeTopology(128, theNumCMstripsInGroup_);
00105 theCommonMode->setTopology(theTopology);
00106
00107
00108 if( theAlgorithmType_ == "TT6") {
00109 theMask = new TT6ApvMask(theMaskCalcFlag_,theMaskNoiseCut_,theMaskDeadCut_,theMaskTruncCut_);
00110 theNoise = new TT6NoiseCalculator(theEventInitNumber_, theEventIterNumber_, theCutToAvoidSignal_);
00111 thePedestal = new TT6PedestalCalculator(theEventInitNumber_, theEventIterNumber_, theCutToAvoidSignal_);
00112 theCM = new TT6CommonModeCalculator (theNoise, theMask, theCutToAvoidSignal_);
00113 } else if( "TT6NT" == theAlgorithmType_) {
00114 theMask = new TT6ApvMask( theMaskCalcFlag_,
00115 theMaskNoiseCut_,
00116 theMaskDeadCut_,
00117 theMaskTruncCut_);
00118 theNoise = new TT6NoiseCalculator( theEventInitNumber_,
00119 theEventIterNumber_,
00120 theCutToAvoidSignal_);
00121 thePedestal = new TT6NTPedestalCalculator;
00122 theCM = new TT6CommonModeCalculator( theNoise,
00123 theMask,
00124 theCutToAvoidSignal_);
00125 } else if (theAlgorithmType_ == "MIX") {
00126
00127 theMask = new TT6ApvMask(theMaskCalcFlag_,theMaskNoiseCut_,theMaskDeadCut_,theMaskTruncCut_);
00128
00129 thePedestal = new SimplePedestalCalculator(theEventInitNumber_);
00130
00131 theNoise = new SimpleNoiseCalculator(theEventInitNumber_, useDB_);
00132
00133 if (theCMType_ == "Median"){
00134 theCM = new MedianCommonModeCalculator ();
00135 } else {
00136 cout << "Sorry Only Median is available for now, Mean and FastLinear are coming soon" << endl;
00137 }
00138 }
00139
00140
00141 if(theCommonMode)
00142 theCM->setCM(theCommonMode);
00143 if(thePedestal)
00144 theAPV->setPedestalCalculator(*thePedestal);
00145 if(theNoise)
00146 theAPV->setNoiseCalculator(*theNoise);
00147 if(theMask)
00148 theAPV->setMask(*theMask);
00149 if(theCM)
00150 theAPV->setCommonModeCalculator(*theCM);
00151
00152
00153
00154 }
00155
00156
00157 void ApvAnalysisFactory::updatePair(uint32_t detId, int pairNumber, const edm::DetSet<SiStripRawDigi>& in)
00158 {
00159 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00160 if(apvAnalysisIt != apvMap_.end())
00161 {
00162 int iter=0;
00163
00164 for(vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin(); apvIt != (apvAnalysisIt->second).end(); apvIt++)
00165 {
00166
00167 if (iter==pairNumber*2 || iter==(2*pairNumber+1)){
00168
00169
00170
00171
00172 edm::DetSet<SiStripRawDigi> tmpRawDigi;
00173 tmpRawDigi.data.reserve(128);
00174
00175 int startStrip = 128*(iter%2);
00176 int stopStrip = startStrip + 128;
00177
00178 for( int istrip = startStrip; istrip < stopStrip;istrip++)
00179 {
00180 if( static_cast<int>( in.data.size()) <= istrip) tmpRawDigi.data.push_back( 0);
00181 else tmpRawDigi.data.push_back(in.data[istrip]);
00182 }
00183
00184 (*apvIt)->newEvent();
00185 (*apvIt)->updateCalibration(tmpRawDigi);
00186
00187 }
00188
00189 iter++;
00190 }
00191 }
00192
00193 }
00194
00195
00196
00197
00198
00199 void ApvAnalysisFactory::update(uint32_t detId, const edm::DetSet<SiStripRawDigi>& in)
00200 {
00201 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00202 if(apvAnalysisIt != apvMap_.end())
00203 {
00204 int i=0;
00205 for(vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin(); apvIt != (apvAnalysisIt->second).end(); apvIt++)
00206 {
00207 edm::DetSet<SiStripRawDigi> tmpRawDigi;
00208
00209 tmpRawDigi.data.reserve(128);
00210 int startStrip = 128*i;
00211 int stopStrip = startStrip + 128;
00212
00213 for( int istrip = startStrip; istrip < stopStrip;istrip++)
00214 {
00215 if( static_cast<int>( in.data.size()) <= istrip) tmpRawDigi.data.push_back(0);
00216 else tmpRawDigi.data.push_back(in.data[istrip]);
00217 }
00218
00219 (*apvIt)->newEvent();
00220 (*apvIt)->updateCalibration(tmpRawDigi);
00221 i++;
00222 }
00223 }
00224
00225 }
00226
00227
00228
00229 void ApvAnalysisFactory::getPedestal(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& peds)
00230 {
00231
00232 peds.clear();
00233 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00234 if(apvAnalysisIt != apvMap_.end())
00235 {
00236 vector<ApvAnalysis*> myApvs = apvAnalysisIt->second;
00237 peds = myApvs[apvNumber]->pedestalCalculator().pedestal();
00238
00239 }
00240 }
00241
00242 void ApvAnalysisFactory::getPedestal(uint32_t detId, ApvAnalysis::PedestalType& peds)
00243 {
00244
00245 peds.clear();
00246 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00247 if(apvAnalysisIt != apvMap_.end())
00248 {
00249 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00250 for(vector<ApvAnalysis*>::const_iterator it = theApvs.begin(); it != theApvs.end();it++)
00251 {
00252 ApvAnalysis::PedestalType tmp = (*it)->pedestalCalculator().pedestal();
00253 for(ApvAnalysis::PedestalType::const_iterator pit =tmp.begin(); pit!=tmp.end(); pit++)
00254 peds.push_back(*pit);
00255 }
00256 }
00257 }
00258 float ApvAnalysisFactory::getStripPedestal(uint32_t detId, int stripNumber)
00259 {
00260
00261 ApvAnalysis::PedestalType temp;
00262 int apvNumb = int(stripNumber / 128.);
00263 int stripN = (stripNumber - apvNumb*128);
00264
00265 getPedestal(detId, apvNumb, temp);
00266 return temp[stripN];
00267
00268 }
00269 void ApvAnalysisFactory::getNoise(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& noise)
00270 {
00271
00272 noise.clear();
00273 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00274 if(apvAnalysisIt != apvMap_.end())
00275 {
00276 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00277
00278 noise = theApvs[apvNumber]->noiseCalculator().noise();
00279 }
00280 }
00281
00282 float ApvAnalysisFactory::getStripNoise(uint32_t detId, int stripNumber)
00283 {
00284
00285 ApvAnalysis::PedestalType temp;
00286 int apvNumb = int(stripNumber / 128.);
00287 int stripN = (stripNumber - apvNumb*128);
00288
00289 getNoise(detId, apvNumb, temp);
00290 return temp[stripN];
00291
00292 }
00293
00294 void ApvAnalysisFactory::getNoise(uint32_t detId, ApvAnalysis::PedestalType& peds)
00295 {
00296
00297 peds.clear();
00298 map<uint32_t, vector<ApvAnalysis* > >::const_iterator theApvs_map = apvMap_.find(detId);
00299 if(theApvs_map != apvMap_.end())
00300 {
00301 vector<ApvAnalysis*>::const_iterator theApvs = (theApvs_map->second).begin();
00302 for(; theApvs != (theApvs_map->second).end();theApvs++)
00303 {
00304 ApvAnalysis::PedestalType tmp = (*theApvs)->noiseCalculator().noise();
00305 for(ApvAnalysis::PedestalType::const_iterator pit =tmp.begin(); pit!=tmp.end(); pit++)
00306 peds.push_back(*pit);
00307 }
00308 }
00309 }
00310
00311
00312 void ApvAnalysisFactory::getRawNoise(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& noise)
00313 {
00314
00315 noise.clear();
00316 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00317 if(apvAnalysisIt != apvMap_.end())
00318 {
00319 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00320
00321 noise = theApvs[apvNumber]->pedestalCalculator().rawNoise();
00322 }
00323 }
00324
00325 float ApvAnalysisFactory::getStripRawNoise(uint32_t detId, int stripNumber)
00326 {
00327
00328 ApvAnalysis::PedestalType temp;
00329 int apvNumb = int(stripNumber / 128.);
00330 int stripN = (stripNumber - apvNumb*128);
00331
00332 getRawNoise(detId, apvNumb, temp);
00333 return temp[stripN];
00334
00335 }
00336
00337 void ApvAnalysisFactory::getRawNoise(uint32_t detId, ApvAnalysis::PedestalType& peds)
00338 {
00339
00340 peds.clear();
00341 map<uint32_t, vector<ApvAnalysis* > >::const_iterator theApvs_map = apvMap_.find(detId);
00342 if(theApvs_map != apvMap_.end())
00343 {
00344 vector<ApvAnalysis*>::const_iterator theApvs = (theApvs_map->second).begin();
00345 for(; theApvs != (theApvs_map->second).end();theApvs++)
00346 {
00347 ApvAnalysis::PedestalType tmp = (*theApvs)->pedestalCalculator().rawNoise();
00348 for(ApvAnalysis::PedestalType::const_iterator pit =tmp.begin(); pit!=tmp.end(); pit++)
00349 peds.push_back(*pit);
00350 }
00351 }
00352 }
00353
00354
00355
00356 vector<float> ApvAnalysisFactory::getCommonMode(uint32_t detId, int apvNumber)
00357 {
00358 vector<float> tmp;
00359 tmp.clear();
00360 map<uint32_t, vector<ApvAnalysis* > >::const_iterator theApvs_map = apvMap_.find(detId);
00361 if(theApvs_map != apvMap_.end())
00362 {
00363 vector<ApvAnalysis* > theApvs = theApvs_map->second;
00364
00365 tmp = theApvs[apvNumber]->commonModeCalculator().commonMode()->returnAsVector();
00366 }
00367 return tmp;
00368 }
00369 void ApvAnalysisFactory::getCommonMode(uint32_t detId,ApvAnalysis::PedestalType& tmp)
00370 {
00371
00372 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00373 if(apvAnalysisIt != apvMap_.end())
00374 {
00375 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00376 for( unsigned int i=0; i< theApvs.size(); i++)
00377 {
00378
00379 vector<float> tmp_cm = theApvs[i]->commonModeCalculator().commonMode()->returnAsVector();
00380 for( unsigned int it = 0; it < tmp_cm.size(); it++)
00381 tmp.push_back( tmp_cm[it]);
00382 }
00383 }
00384 }
00385
00386 void ApvAnalysisFactory::getMask(uint32_t det_id, TkApvMask::MaskType& tmp)
00387 {
00388
00389 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(det_id);
00390 if(apvAnalysisIt != apvMap_.end()) {
00391 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00392 for( unsigned int i=0; i< theApvs.size(); i++)
00393 {
00394 TkApvMask::MaskType theMaskType = ( theApvs[i]->mask()).mask();
00395
00396
00397 for( unsigned int ii=0;ii<theMaskType.size();ii++)
00398 {
00399 tmp.push_back(theMaskType[ii]);
00400
00401 }
00402 }
00403 }
00404 }
00405 bool ApvAnalysisFactory::isUpdating(uint32_t detId)
00406 {
00407 bool updating = true;
00408 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00409 if(apvAnalysisIt != apvMap_.end())
00410 {
00411 for(vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin(); apvIt != (apvAnalysisIt->second).end(); apvIt++)
00412 {
00413 if(!( (*apvIt)->pedestalCalculator().status()->isUpdating() ))
00414 updating = false;
00415 }
00416 }
00417 return updating;
00418
00419 }
00420
00421 void ApvAnalysisFactory::deleteApv(ApvAnalysis* apv){
00422 delete &(apv->pedestalCalculator());
00423 delete &(apv->noiseCalculator());
00424 delete &(apv->mask());
00425 delete &(apv->commonModeCalculator().commonMode()->topology());
00426 delete (apv->commonModeCalculator().commonMode());
00427 delete &(apv->commonModeCalculator());
00428 delete apv;
00429
00430 }
00431
00432
00433
00434 float ApvAnalysisFactory::getCommonModeSlope(uint32_t detId, int apvNumber){
00435 map<uint32_t, vector<ApvAnalysis* > >::const_iterator theApvs_map = apvMap_.find(detId);
00436 float tmp = -100.0;
00437 if(theApvs_map != apvMap_.end()) {
00438 vector<ApvAnalysis* > theApvs = theApvs_map->second;
00439 tmp = theApvs[apvNumber]->commonModeCalculator().getCMSlope();
00440 return tmp;
00441 }
00442 return tmp;
00443 }
00444 void ApvAnalysisFactory::getCommonModeSlope(uint32_t detId,ApvAnalysis::PedestalType& tmp)
00445 {
00446 tmp.clear();
00447 map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
00448 if(apvAnalysisIt != apvMap_.end()) {
00449 vector<ApvAnalysis* > theApvs = apvAnalysisIt->second;
00450 for( unsigned int i=0; i< theApvs.size(); i++) {
00451 tmp.push_back(theApvs[i]->commonModeCalculator().getCMSlope());
00452 }
00453 }
00454 }