CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/SimCalorimetry/EcalTrigPrimAlgos/interface/EcalTrigPrimFunctionalAlgo.h

Go to the documentation of this file.
00001 #ifndef EcalTrigPrimFunctionalAlgo_h
00002 #define EcalTrigPrimFunctionalAlgo_h
00003 
00018 #include <sys/time.h>
00019 #include <iostream>
00020 #include <vector>
00021 
00022 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
00023 
00024 #include "SimCalorimetry/EcalTrigPrimAlgos/interface/EcalFenixStrip.h"
00025 #include "SimCalorimetry/EcalTrigPrimAlgos/interface/EcalFenixTcp.h"
00026 
00027 #include "DataFormats/Common/interface/SortedCollection.h"
00028 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
00029 
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/ESHandle.h"
00032 
00033 #include <map>
00034 #include <utility>
00035 
00039 class EcalTrigTowerDetId;
00040 class ETPCoherenceTest;
00041 class EcalTriggerPrimitiveSample;
00042 class CaloSubdetectorGeometry;
00043 class EBDataFrame;
00044 class EEDataFrame;
00045 class EcalElectronicsMapping;
00046 
00047  
00048 class EcalTrigPrimFunctionalAlgo
00049 {  
00050  public:
00051   
00052   explicit EcalTrigPrimFunctionalAlgo(const edm::EventSetup & setup, int binofmax, bool tcpFormat, bool barrelOnly, bool debug, bool famos);
00053 
00054   virtual ~EcalTrigPrimFunctionalAlgo();
00055 
00056   void run(const edm::EventSetup &, const EBDigiCollection *col, EcalTrigPrimDigiCollection & result, EcalTrigPrimDigiCollection & resultTcp);
00057   void run(const edm::EventSetup&,  const EEDigiCollection *col, EcalTrigPrimDigiCollection & result, EcalTrigPrimDigiCollection & resultTcp);
00058   void run_part1_EB(EBDigiCollection const * col);
00059   void run_part1_EE(EEDigiCollection const * col);
00060   template <class Coll> 
00061     void run_part2(const edm::EventSetup &, Coll const * col, 
00062                    std::vector<std::vector<std::pair<int,std::vector<typename Coll::Digi> > > > &towerMap,
00063                    EcalTrigPrimDigiCollection & result,
00064                    EcalTrigPrimDigiCollection & resultTcp);
00065 
00066   void setPointers(const EcalTPGLinearizationConst *ecaltpLin,const EcalTPGPedestals *ecaltpPed,const EcalTPGSlidingWindow * ecaltpgSlidW,const EcalTPGWeightIdMap * ecaltpgWeightMap,const EcalTPGWeightGroup * ecaltpgWeightGroup,const EcalTPGFineGrainStripEE * ecaltpgFgStripEE, const EcalTPGCrystalStatus * ecaltpgBadX)  {
00067     estrip_->setPointers(ecaltpPed,ecaltpLin,ecaltpgWeightMap,ecaltpgWeightGroup,ecaltpgSlidW,ecaltpgFgStripEE,ecaltpgBadX);
00068 
00069   }
00070   void setPointers2(  const EcalTPGFineGrainEBGroup * ecaltpgFgEBGroup,
00071                       const EcalTPGLutGroup * ecaltpgLutGroup,
00072                       const EcalTPGLutIdMap * ecaltpgLut,
00073                       const EcalTPGFineGrainEBIdMap * ecaltpgFineGrainEB,
00074                       const EcalTPGFineGrainTowerEE * ecaltpgFineGrainTowerEE,
00075                       const EcalTPGTowerStatus * ecaltpgBadTT){
00076    
00077   etcp_->setPointers(ecaltpgFgEBGroup,ecaltpgLutGroup,ecaltpgLut,ecaltpgFineGrainEB,ecaltpgFineGrainTowerEE,ecaltpgBadTT);
00078   }
00079 
00080  private:
00081 
00082   void init(const edm::EventSetup&);
00083   template <class T>  
00084     void initStructures(std::vector<std::vector<std::pair<int,std::vector<T> > > > & towMap);
00085   template <class T> 
00086     void clean(std::vector<std::vector<std::pair<int,std::vector<T> > > > &towerMap);
00087   template <class Coll> 
00088     void fillMap(Coll const * col, std::vector<std::vector<std::pair<int,std::vector<typename Coll::Digi> > > > &towerMap);
00089   int findStripNr(const EBDetId &id);
00090   int findStripNr(const EEDetId &id);
00091 
00092   // FIXME: temporary until hashedIndex works alsom for endcap
00093   int getIndex(const  EBDigiCollection *, EcalTrigTowerDetId& id) {return id.hashedIndex();}
00094   // mind that eta is continuous between barrel+endcap
00095   int getIndex(const  EEDigiCollection *, EcalTrigTowerDetId& id) {
00096     int ind=(id.ietaAbs()-18)*72 + id.iphi();
00097     if (id.zside()<0) ind+=792;
00098     return ind;
00099   }
00100 
00101   EcalFenixStrip * estrip_;
00102   EcalFenixTcp * etcp_;
00103 
00104   edm::ESHandle<EcalTrigTowerConstituentsMap> eTTmap_;
00105   const CaloSubdetectorGeometry *theEndcapGeometry;
00106   const EcalElectronicsMapping* theMapping_;
00107 
00108   float threshold;
00109 
00110   int binOfMaximum_;
00111   int maxNrSamples_;
00112 
00113   bool tcpFormat_;
00114   bool barrelOnly_;
00115   bool debug_;  
00116   bool famos_;  
00117 
00118   static const unsigned int nrSamples_; //nr samples to write, should not be changed since by convention the size means that it is coming from simulation
00119   static const unsigned int maxNrSamplesOut_; //to be placed in the intermediate samples
00120   static const unsigned int maxNrTowers_; //would be better to get from somewhere..
00121   static const unsigned int maxNrTPs_; //would be better to get from somewhere..
00122 
00123   int nrTowers_;   // nr of towers found by fillmap method
00124 
00125   // data structures kept during the whole run
00126   std::vector<std::vector<int> > striptp_;
00127   std::vector<std::vector<std::pair<int,std::vector<EBDataFrame> > > > towerMapEB_;
00128   std::vector<std::vector<std::pair<int,std::vector<EEDataFrame> > > > towerMapEE_;
00129   std::vector<std::pair<int,EcalTrigTowerDetId> > hitTowers_;
00130   std::vector<EcalTriggerPrimitiveSample> towtp_;
00131   std::vector<EcalTriggerPrimitiveSample> towtp2_;
00132 
00133   enum {nbMaxStrips_=5};
00134   enum {nbMaxXtals_=5};
00135 };
00136 
00137 //=================================== implementations =============================================
00138 
00139 template <class Coll> 
00140 void EcalTrigPrimFunctionalAlgo::run_part2(const edm::EventSetup &setup,Coll const * col, std::vector<std::vector<std::pair<int,std::vector<typename Coll::Digi> > > > &towerMap,
00141                                            EcalTrigPrimDigiCollection & result,
00142                                            EcalTrigPrimDigiCollection & resultTcp)
00143 {
00144   typedef typename Coll::Digi Digi;
00145  
00146   // prepare writing of TP-s
00147 
00148   int firstSample = binOfMaximum_-1 -nrSamples_/2;
00149   int lastSample = binOfMaximum_-1 +nrSamples_/2;
00150   int nrTP=0;
00151   std::vector<typename Coll::Digi> dummy;
00152   EcalTriggerPrimitiveDigi tptow[2];
00153   EcalTriggerPrimitiveDigi tptowTcp[2];
00154 
00155   for(int itow=0;itow<nrTowers_;++itow) 
00156     {
00157 
00158       int index=hitTowers_[itow].first;
00159       const EcalTrigTowerDetId &thisTower=hitTowers_[itow].second;
00160 
00161       // loop over all strips assigned to this trigger tower
00162       int nstr=0;
00163       for(unsigned int i = 0; i < towerMap[itow].size();++i)
00164         {
00165           std::vector<Digi> &df = (towerMap[index])[i].second;//vector of dataframes for this strip, size; nr of crystals/strip
00166 
00167           if ((towerMap[index])[i].first > 0) {  
00168             estrip_->process(setup,df,(towerMap[index])[i].first,striptp_[nstr++]);
00169           }
00170         }//loop over strips in one tower
00171 
00172       bool isInInnerRings=false;
00173       if (thisTower.subDet()==EcalEndcap && (thisTower.ietaAbs()==27 || thisTower.ietaAbs()==28 )) isInInnerRings=true;
00174       etcp_->process(setup,dummy,striptp_,nstr,towtp_,towtp2_,isInInnerRings,thisTower);
00175 
00176       // prepare TP-s
00177       // special treatment for 2 inner endcap rings
00178       int nrTowers;
00179       if (isInInnerRings)
00180         {
00181           nrTowers=2;
00182           int phi=2*((thisTower.iphi()-1)/2);
00183           tptow[0]=EcalTriggerPrimitiveDigi(EcalTrigTowerDetId(thisTower.zside(),thisTower.subDet(),thisTower.ietaAbs(),phi+1));
00184           tptow[1]=EcalTriggerPrimitiveDigi(EcalTrigTowerDetId(thisTower.zside(),thisTower.subDet(),thisTower.ietaAbs(),phi+2));
00185 
00186           if (tcpFormat_){
00187             tptowTcp[0]=EcalTriggerPrimitiveDigi(EcalTrigTowerDetId(thisTower.zside(),thisTower.subDet(),thisTower.ietaAbs(),phi+1));
00188             tptowTcp[1]=EcalTriggerPrimitiveDigi(EcalTrigTowerDetId(thisTower.zside(),thisTower.subDet(),thisTower.ietaAbs(),phi+2));
00189           }
00190         }else {
00191           nrTowers=1;
00192           tptow[0]=EcalTriggerPrimitiveDigi(thisTower);
00193           if (tcpFormat_)   tptowTcp[0]=EcalTriggerPrimitiveDigi(thisTower);
00194         }
00195 
00196       // now fill in
00197       for (int nrt=0;nrt<nrTowers;nrt++) {
00198         (tptow[nrt]).setSize(nrSamples_);
00199         if (towtp_.size()<nrSamples_)  {  //FIXME: only once
00200           edm::LogWarning("") <<"Too few samples produced, nr is "<<towtp_.size();
00201           break;
00202         }
00203         int isam=0;
00204         for (int i=firstSample;i<=lastSample;++i) {
00205           tptow[nrt].setSample(isam++,EcalTriggerPrimitiveSample(towtp_[i]));
00206         }
00207         nrTP++; 
00208         LogDebug("EcalTPG") <<" For tower "<<itow<<" created TP nr "<<nrTP<<" with Et "<<tptow[nrt].compressedEt();
00209         result.push_back(tptow[nrt]);
00210       }
00211 
00212       if (tcpFormat_) {
00213 
00214         for (int nrt=0;nrt<nrTowers;nrt++) {
00215           tptowTcp[nrt].setSize(nrSamples_);
00216           if (towtp2_.size()<nrSamples_)  {  //FIXME: only once
00217             edm::LogWarning("") <<"Too few samples produced, nr is "<<towtp2_.size();
00218             break;
00219           }
00220           int isam=0;
00221           for (int i=firstSample;i<=lastSample;++i) {
00222             if (nrTowers<=1)  tptowTcp[nrt].setSample(isam++,EcalTriggerPrimitiveSample(towtp2_[i]));
00223             else {
00224               int et=towtp2_[i].compressedEt()/2;
00225               tptowTcp[nrt].setSample(isam++,EcalTriggerPrimitiveSample(et,towtp2_[i].fineGrain(),towtp2_[i].ttFlag()));
00226             }
00227           }
00228           resultTcp.push_back(tptowTcp[nrt]);
00229         }
00230       }
00231     } 
00232   return;
00233 }
00234 
00235 template <class Coll> 
00236 void EcalTrigPrimFunctionalAlgo::fillMap(Coll const * col, 
00237                                          std::vector<std::vector<std::pair<int,std::vector<typename Coll::Digi> > > > &towerMap)
00238 {
00239   typedef typename Coll::Digi Digi;
00240 
00241   // implementation for Barrel and Endcap
00242 
00243   if (col) {
00244     nrTowers_=0;
00245     LogDebug("EcalTPG") <<"Fill mapping, Collection size = "<<col->size();
00246     for(unsigned int i = 0; i < col->size() ; ++i) {
00247       Digi samples((*col)[i]); 
00248       EcalTrigTowerDetId coarser=(*eTTmap_).towerOf(samples.id());
00249       int index=getIndex(col,coarser);
00250       int stripnr=findStripNr(samples.id());
00251 
00252       int filled=0;
00253       for (unsigned int ij=0;ij<towerMap[index].size();++ij) filled+=towerMap[index][ij].first;
00254       if (!filled) {
00255         hitTowers_[nrTowers_++]=std::pair <int,EcalTrigTowerDetId>(index,coarser);
00256       }
00257 
00258       //FIXME: temporary protection
00259       int ncryst=towerMap[index][stripnr-1].first;
00260       if (ncryst>=nbMaxXtals_ ) {
00261         edm::LogError("EcalTrigPrimFunctionAlgo")<<"! Too many xtals for TT "<<coarser<<" stripnr "<<stripnr<<" xtalid "<<samples.id() ;
00262         continue;
00263       }
00264       ((towerMap[index])[stripnr-1].second)[ncryst]=samples;
00265       (towerMap[index])[stripnr-1].first++;
00266     }
00267   
00268     LogDebug("EcalTPG")<<"fillMap"<<"[EcalTrigPrimFunctionalAlgo] (found " 
00269                        << col->size() << " frames in "<< towerMap.size() << " towers) ";
00270   }
00271   else {
00272     LogDebug("EcalTPG")<<"FillMap - FillMap Collection size=0 !!!!";
00273   }
00274 }
00275 
00276 template <class T> 
00277 void EcalTrigPrimFunctionalAlgo::clean( std::vector<std::vector<std::pair<int,std::vector<T> > > > & towMap) {  
00278   // clean internal data structures
00279   for (unsigned int i=0;i<maxNrTowers_;++i) 
00280     for (int j=0;j<nbMaxStrips_ ;++j) (towMap[i])[j].first=0;
00281   return;
00282 }
00283  
00284 template <class T> 
00285 void EcalTrigPrimFunctionalAlgo::initStructures( std::vector<std::vector<std::pair<int,std::vector<T> > > > & towMap) {  
00286   //initialise internal data structures
00287 
00288   std::vector <T> vec0(nbMaxXtals_ );
00289   std::vector<std::pair<int,std::vector<T> > > vec1(nbMaxStrips_);
00290   for (int i=0;i<nbMaxStrips_ ;++i) vec1[i]=std::pair<int,std::vector<T> >(0,vec0);
00291   towMap.resize(maxNrTowers_); 
00292   for (unsigned int i=0;i<maxNrTowers_;++i) towMap[i]=vec1;
00293   
00294   std::vector<int> vecint(maxNrSamples_);
00295   striptp_.resize(nbMaxStrips_);
00296   for (int i=0;i<nbMaxStrips_;++i) striptp_[i]=vecint;
00297   
00298 }
00299 
00300 #endif