CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/Calibration/IsolatedParticles/src/MatrixECALDetIds.cc

Go to the documentation of this file.
00001 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00002 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00003 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00004 
00005 #include "Calibration/IsolatedParticles/interface/MatrixECALDetIds.h"
00006 #include "Calibration/IsolatedParticles/interface/FindDistCone.h"
00007 #include "Calibration/IsolatedParticles/interface/DebugInfo.h"
00008 
00009 #include <algorithm>
00010 #include <iostream>
00011 
00012 namespace spr{
00013 
00014   void matrixECALIds(const DetId& det, int ieta, int iphi,
00015                      const CaloGeometry* geo, const CaloTopology* caloTopology,
00016                      std::vector<DetId>& vdets, bool debug) {
00017 
00018     const CaloSubdetectorTopology *barrelTopo = (caloTopology->getSubdetectorTopology(DetId::Ecal,EcalBarrel));
00019     const CaloSubdetectorTopology *endcapTopo = (caloTopology->getSubdetectorTopology(DetId::Ecal,EcalEndcap));
00020     const EcalBarrelGeometry *barrelGeom = (dynamic_cast< const EcalBarrelGeometry *> (geo->getSubdetectorGeometry(DetId::Ecal,EcalBarrel)));
00021     const EcalEndcapGeometry *endcapGeom = (dynamic_cast< const EcalEndcapGeometry *> (geo->getSubdetectorGeometry(DetId::Ecal,EcalEndcap)));
00022 
00023     if (debug) {
00024       std::cout << "matrixECALIds::Add " << ieta << " rows and " << iphi 
00025                 << " columns of cells for 1 cell" << std::endl;
00026       spr::debugEcalDets(0, det, true);
00027     }
00028 
00029     std::vector<DetId> dets(1,det);
00030     std::vector<CaloDirection> dirs(1,NORTH);
00031     vdets = spr::newECALIdNS(dets, 0, ieta,iphi, dirs, *barrelTopo,*endcapTopo,
00032                              *barrelGeom,*endcapGeom,debug);
00033     dirs[0] = SOUTH;
00034     std::vector<DetId> vdetS = spr::newECALIdNS(dets, 0, ieta, iphi, dirs,
00035                                                 *barrelTopo,*endcapTopo, 
00036                                                 *barrelGeom,*endcapGeom,debug);
00037     for (unsigned int i1=0; i1<vdetS.size(); i1++) {
00038       if (std::count(vdets.begin(),vdets.end(),vdetS[i1]) == 0)
00039         vdets.push_back(vdetS[i1]);
00040     }
00041     unsigned int ndet = (2*ieta+1)*(2*iphi+1);
00042     if (vdets.size() != ndet) {
00043       std::vector<DetId> vdetExtra;
00044       spr::extraIds(det, vdets, ieta, ieta, iphi, iphi, 
00045                     *barrelGeom, *endcapGeom, vdetExtra, debug);
00046       if (vdetExtra.size() > 0) 
00047         vdets.insert(vdets.end(), vdetExtra.begin(), vdetExtra.end());
00048     }
00049 
00050     if (debug) {
00051       std::cout << "matrixECALIds::Total number of cells found is " 
00052                 << vdets.size() << std::endl;
00053       spr::debugEcalDets(0, vdets);
00054     }
00055   }
00056 
00057   std::vector<DetId> matrixECALIds(const DetId& det,int ieta,int iphi, 
00058                                    const CaloGeometry* geo, 
00059                                    const CaloTopology* caloTopology,
00060                                    bool debug) {
00061 
00062     std::vector<DetId> vdets;
00063     spr::matrixECALIds(det, ieta, iphi, geo, caloTopology, vdets, debug);
00064     return vdets;
00065   }
00066 
00067   std::vector<DetId> matrixECALIds(const DetId& det, double dR, 
00068                                    const GlobalVector& trackMom,
00069                                    const CaloGeometry* geo, 
00070                                    const CaloTopology* caloTopology,
00071                                    bool debug) {
00072 
00073     GlobalPoint core;
00074     if (det.subdetId() == EcalEndcap) {
00075       EEDetId EEid = EEDetId(det);
00076       core = geo->getPosition(EEid);
00077     } else {
00078       EBDetId EBid = EBDetId(det);
00079       core = geo->getPosition(EBid);
00080     }
00081     int ietaphi = (int)(dR/2.0)+1;
00082     std::vector<DetId> vdets, vdetx;
00083     spr::matrixECALIds(det, ietaphi, ietaphi, geo, caloTopology, vdets, debug);
00084     for (unsigned int i=0; i<vdets.size(); ++i) {
00085       GlobalPoint rpoint;
00086       if (vdets[i].subdetId() == EcalEndcap) {
00087         EEDetId EEid = EEDetId(vdets[i]);
00088         rpoint = geo->getPosition(EEid);
00089       } else {
00090         EBDetId EBid = EBDetId(vdets[i]);
00091         rpoint = geo->getPosition(EBid);
00092       }
00093       if (spr::getDistInPlaneTrackDir(core, trackMom, rpoint)<dR) {
00094         vdetx.push_back(vdets[i]);
00095       }
00096     }
00097 
00098     if (debug) {
00099       std::cout << "matrixECALIds::Final List of cells for dR " << dR
00100                 << " is with " << vdetx.size() << " from original list of " 
00101                 << vdets.size() << std::endl;
00102       spr::debugEcalDets(0, vdetx);
00103     }
00104     return vdetx;
00105   }
00106 
00107   void matrixECALIds(const DetId& det, int ietaE,int ietaW,int iphiN,int iphiS,
00108                      const CaloGeometry* geo, const CaloTopology* caloTopology,
00109                      std::vector<DetId>& vdets, bool debug) {
00110 
00111     const CaloSubdetectorTopology *barrelTopo = (caloTopology->getSubdetectorTopology(DetId::Ecal,EcalBarrel));
00112     const CaloSubdetectorTopology *endcapTopo = (caloTopology->getSubdetectorTopology(DetId::Ecal,EcalEndcap));
00113     const EcalBarrelGeometry *barrelGeom = (dynamic_cast< const EcalBarrelGeometry *> (geo->getSubdetectorGeometry(DetId::Ecal,EcalBarrel)));
00114     const EcalEndcapGeometry *endcapGeom = (dynamic_cast< const EcalEndcapGeometry *> (geo->getSubdetectorGeometry(DetId::Ecal,EcalEndcap)));
00115 
00116     if (debug) {
00117       std::cout << "matrixECALIds::Add " << ietaE << "|" << ietaW
00118                 << " rows and " << iphiN << "|" << iphiS
00119                 << " columns of cells for 1 cell" << std::endl;
00120       debugEcalDets(0, det, true);
00121     }
00122 
00123     std::vector<DetId> dets(1,det);
00124     std::vector<CaloDirection> dirs(1,NORTH);
00125     std::vector<int> jetaE(1,ietaE), jetaW(1,ietaW);
00126     std::vector<int> jphiN(1,iphiN), jphiS(1,iphiS);
00127     vdets = spr::newECALIdNS(dets, 0, jetaE, jetaW, jphiN, jphiS, dirs, 
00128                              *barrelTopo, *endcapTopo, *barrelGeom,
00129                              *endcapGeom, debug);
00130     dirs[0] = SOUTH;
00131     std::vector<DetId> vdetS = spr::newECALIdNS(dets, 0, jetaE, jetaW, jphiN,
00132                                                 jphiS, dirs, *barrelTopo,
00133                                                 *endcapTopo, *barrelGeom,
00134                                                 *endcapGeom, debug);
00135     for (unsigned int i1=0; i1<vdetS.size(); i1++) {
00136       if (std::count(vdets.begin(),vdets.end(),vdetS[i1]) == 0)
00137         vdets.push_back(vdetS[i1]);
00138     }
00139 
00140     unsigned int ndet = (ietaE+ietaW+1)*(iphiN+iphiS+1);
00141     if (vdets.size() != ndet) {
00142       std::vector<DetId> vdetExtra;
00143       spr::extraIds(det, vdets, ietaE, ietaW, iphiN, iphiS, 
00144                     *barrelGeom, *endcapGeom, vdetExtra, debug);
00145       if (vdetExtra.size() > 0) 
00146         vdets.insert(vdets.end(), vdetExtra.begin(), vdetExtra.end());
00147     }
00148 
00149     if (debug) {
00150       std::cout << "matrixECALIds::Total number of cells found is " 
00151                 << vdets.size() << std::endl;
00152       spr::debugEcalDets(0, vdets);
00153     }
00154   }
00155 
00156   std::vector<DetId> matrixECALIds(const DetId& det, int ietaE, int ietaW,
00157                                    int iphiN, int iphiS,
00158                                    const CaloGeometry* geo, 
00159                                    const CaloTopology* caloTopology,
00160                                    bool debug) {
00161 
00162     std::vector<DetId> vdets;
00163     spr::matrixECALIds(det, ietaE, ietaW, iphiN, iphiS, geo, caloTopology,
00164                        vdets, debug);
00165     return vdets;
00166   }
00167 
00168   std::vector<DetId> newECALIdNS(std::vector<DetId>& dets, unsigned int last,
00169                                  int ieta, int iphi, 
00170                                  std::vector<CaloDirection>& dir,
00171                                  const CaloSubdetectorTopology& barrelTopo,
00172                                  const CaloSubdetectorTopology& endcapTopo,
00173                                  const EcalBarrelGeometry& barrelGeom, 
00174                                  const EcalEndcapGeometry& endcapGeom,
00175                                  bool debug) {
00176 
00177     if (debug) {
00178       std::cout << "newECALIdNS::Add " << iphi << " columns of cells for " 
00179                 << (dets.size()-last) << " cells (last " << last << ")"
00180                 << std::endl;
00181       spr::debugEcalDets(last, dets, dir);
00182     }
00183     
00184     std::vector<DetId> vdets;
00185     std::vector<CaloDirection> dirs;
00186     vdets.insert(vdets.end(), dets.begin(), dets.end());
00187     dirs.insert(dirs.end(), dir.begin(), dir.end());
00188 
00189     std::vector<DetId> vdetE, vdetW;
00190     if (last == 0) {
00191       unsigned int ndet = vdets.size();
00192       std::vector<CaloDirection> dirE(ndet,EAST), dirW(ndet,WEST);
00193       vdetE = spr::newECALIdEW(dets, last, ieta, dirE, barrelTopo, endcapTopo, 
00194                                barrelGeom, endcapGeom, debug);
00195       vdetW = spr::newECALIdEW(dets, last, ieta, dirW, barrelTopo, endcapTopo,
00196                                barrelGeom, endcapGeom, debug);
00197       for (unsigned int i1=0; i1<vdetW.size(); i1++) {
00198         if (std::count(vdets.begin(),vdets.end(),vdetW[i1]) == 0) {
00199           vdets.push_back(vdetW[i1]);
00200           dirs.push_back(dir[0]);
00201         }
00202       }
00203       for (unsigned int i1=0; i1<vdetE.size(); i1++) {
00204         if (std::count(vdets.begin(),vdets.end(),vdetE[i1]) == 0) {
00205           vdets.push_back(vdetE[i1]);
00206           dirs.push_back(dir[0]);
00207         }
00208       }
00209       if (debug) {
00210         std::cout <<"newECALIdNS::With Added cells along E/W results a set of "
00211                   << (vdets.size()-dets.size()) << " new  cells" << std::endl;
00212         spr::debugEcalDets(dets.size(), vdets, dirs);
00213       }
00214     }
00215 
00216     unsigned int last0 = vdets.size();
00217     std::vector<DetId> vdetnew;
00218     std::vector<CaloDirection> dirnew;
00219     if (iphi > 0) {
00220       std::vector<DetId> vdetn(1);
00221       std::vector<CaloDirection> dirn(1);
00222       std::vector<CaloDirection> dirnE(1,EAST), dirnW(1,WEST);
00223       int flag=0;
00224       for (unsigned int i1=last; i1<dets.size(); i1++) {
00225         std::vector<DetId> cells;
00226         spr::simpleMove(dets[i1], dir[i1], barrelTopo, endcapTopo,
00227                         barrelGeom, endcapGeom, cells, flag, debug);
00228         if (flag != 0) {
00229           if (std::count(vdets.begin(),vdets.end(),cells[0]) == 0) {
00230             vdetn[0] = cells[0];
00231             vdetnew.push_back(vdetn[0]);
00232             dirn[0] = dir[i1];
00233             if (flag < 0) {
00234               if (dirn[0] == NORTH) dirn[0] = SOUTH;
00235               else                  dirn[0] = NORTH;
00236             }
00237             dirnew.push_back(dirn[0]);
00238             vdetE = spr::newECALIdEW(vdetn, 0, ieta, dirnE, barrelTopo, 
00239                                      endcapTopo, barrelGeom, endcapGeom,debug);
00240             vdetW = spr::newECALIdEW(vdetn, 0, ieta, dirnW, barrelTopo, 
00241                                      endcapTopo, barrelGeom, endcapGeom,debug);
00242             for (unsigned int i2=0; i2<vdetW.size(); i2++) {
00243               if (std::count(vdets.begin(),vdets.end(),vdetW[i2]) == 0 &&
00244                   std::count(vdetnew.begin(),vdetnew.end(),vdetW[i2]) == 0) {
00245                 vdets.push_back(vdetW[i2]);
00246                 dirs.push_back(dirn[0]);
00247               }
00248             }
00249             for (unsigned int i2=0; i2<vdetE.size(); i2++) {
00250               if (std::count(vdets.begin(),vdets.end(),vdetE[i2]) == 0 &&
00251                   std::count(vdetnew.begin(),vdetnew.end(),vdetE[i2]) == 0) {
00252                 vdets.push_back(vdetE[i2]);
00253                 dirs.push_back(dirn[0]);
00254               }
00255             }
00256           }
00257         }
00258       }
00259       iphi--;
00260       last = vdets.size();
00261       for (unsigned int i2=0; i2<vdetnew.size(); i2++) {
00262         if (std::count(vdets.begin(),vdets.end(),vdetnew[i2]) == 0) {
00263           vdets.push_back(vdetnew[i2]);
00264           dirs.push_back(dirnew[i2]);
00265         }
00266       }
00267       if (debug) {
00268         std::cout << "newECALIdNS::Addition results a set of " 
00269                   << (vdets.size()-last0)  << " new  cells (last " << last0
00270                   << ", iphi " << iphi << ")" << std::endl;
00271         spr::debugEcalDets(last0, vdets, dirs);
00272       }
00273       last0 = last;
00274     }
00275 
00276     if (iphi > 0) {
00277       last = last0;
00278       return spr::newECALIdNS(vdets,last,ieta,iphi,dirs,barrelTopo,endcapTopo,barrelGeom,endcapGeom,debug);
00279     } else {
00280       if (debug) {
00281         std::cout << "newECALIdNS::Final list consists of " << vdets.size()
00282                   << " cells" << std::endl;
00283         spr::debugEcalDets(0, vdets);
00284       }
00285       return vdets;
00286     }
00287   }
00288 
00289   std::vector<DetId> newECALIdNS(std::vector<DetId>& dets, unsigned int last,
00290                                  std::vector<int>& ietaE, 
00291                                  std::vector<int>& ietaW, 
00292                                  std::vector<int>& iphiN, 
00293                                  std::vector<int>& iphiS,
00294                                  std::vector<CaloDirection>& dir,
00295                                  const CaloSubdetectorTopology& barrelTopo,
00296                                  const CaloSubdetectorTopology& endcapTopo,
00297                                  const EcalBarrelGeometry& barrelGeom, 
00298                                  const EcalEndcapGeometry& endcapGeom,
00299                                  bool debug) {
00300 
00301     if (debug) {
00302       std::cout << "newECALIdNS::Add columns of cells for " 
00303                 << (dets.size()-last) << " cells (last) " << last << std::endl;
00304       for (unsigned int i1=last; i1<dets.size(); i1++) {
00305         spr::debugEcalDets (i1, dets[i1], false);
00306         std::cout << " along " << dir[i1] << " # " << iphiN[i1] << "|" 
00307                   << iphiS[i1] << std::endl;
00308       }
00309     }
00310 
00311     std::vector<DetId> vdets;
00312     std::vector<CaloDirection> dirs;
00313     std::vector<int> jetaE, jetaW, jphiN, jphiS;
00314     vdets.insert(vdets.end(), dets.begin(), dets.end());
00315     dirs.insert(dirs.end(), dir.begin(), dir.end());
00316     jetaE.insert(jetaE.end(), ietaE.begin(), ietaE.end());
00317     jetaW.insert(jetaW.end(), ietaW.begin(), ietaW.end());
00318     jphiN.insert(jphiN.end(), iphiN.begin(), iphiN.end());
00319     jphiS.insert(jphiS.end(), iphiS.begin(), iphiS.end());
00320     std::vector<DetId> vdetE, vdetW;
00321     if (last == 0) {
00322       unsigned int ndet = vdets.size();
00323       std::vector<CaloDirection> dirE(ndet,EAST), dirW(ndet,WEST);
00324       vdetE = spr::newECALIdEW(dets, last, ietaE, ietaW, dirE, barrelTopo, 
00325                                endcapTopo, barrelGeom, endcapGeom, debug);
00326       vdetW = spr::newECALIdEW(dets, last, ietaE, ietaW, dirW, barrelTopo,
00327                                endcapTopo, barrelGeom, endcapGeom, debug);
00328       for (unsigned int i1=0; i1<vdetW.size(); i1++) {
00329         if (std::count(vdets.begin(),vdets.end(),vdetW[i1]) == 0) {
00330           vdets.push_back(vdetW[i1]);
00331           dirs.push_back(dir[0]);
00332           jetaE.push_back(0);
00333           jetaW.push_back(0);
00334           jphiN.push_back(iphiN[0]);
00335           jphiS.push_back(iphiS[0]);
00336         }
00337       }
00338       for (unsigned int i1=0; i1<vdetE.size(); i1++) {
00339         if (std::count(vdets.begin(),vdets.end(),vdetE[i1]) == 0) {
00340           vdets.push_back(vdetE[i1]);
00341           dirs.push_back(dir[0]);
00342           jetaE.push_back(0);
00343           jetaW.push_back(0);
00344           jphiN.push_back(iphiN[0]);
00345           jphiS.push_back(iphiS[0]);
00346         }
00347       }
00348       if (debug) {
00349         std::cout <<"newECALIdNS::With Added cells along E/W results a set of "
00350                   << (vdets.size()-dets.size()) << " new  cells" << std::endl;
00351         spr::debugEcalDets(dets.size(), vdets, dirs);
00352       }
00353     }
00354 
00355     unsigned int last0 = vdets.size();
00356     std::vector<DetId> vdetnew;
00357     std::vector<CaloDirection> dirnew;
00358     std::vector<int> kphiN, kphiS, ketaE, ketaW;
00359     int kphi = 0;
00360     for (unsigned int i1=last; i1<dets.size(); i1++) {
00361       int iphi = iphiS[i1];
00362       if (dir[i1] == NORTH) iphi = iphiN[i1];
00363       if (iphi > 0) {
00364         std::vector<DetId>         vdetn(1);
00365         std::vector<CaloDirection> dirn(1);
00366         std::vector<CaloDirection> dirnE(1,EAST), dirnW(1,WEST);
00367         int flag=0;
00368         std::vector<DetId> cells;
00369         spr::simpleMove(dets[i1], dir[i1], barrelTopo, endcapTopo,
00370                         barrelGeom, endcapGeom, cells, flag, debug);
00371         iphi--;
00372         if (iphi > kphi) kphi = iphi;
00373         if (dir[i1] == NORTH) jphiN[i1] = iphi;
00374         else                  jphiS[i1] = iphi;
00375         if (flag != 0) {
00376           if (std::count(vdets.begin(),vdets.end(),cells[0]) == 0) {
00377             int kfiN = iphiN[i1];
00378             int kfiS = iphiS[i1];
00379             vdetn[0] = cells[0];
00380             vdetnew.push_back(vdetn[0]);
00381             dirn[0] = dir[i1];
00382             if (dir[i1] == NORTH) kfiN = iphi;
00383             else                  kfiS = iphi;
00384             if (flag < 0) {
00385               int ktmp = kfiS; kfiS = kfiN; kfiN = ktmp;
00386               if (dirn[0] == NORTH) dirn[0] = SOUTH;
00387               else                  dirn[0] = NORTH;
00388             }
00389             dirnew.push_back(dirn[0]);
00390             kphiN.push_back(kfiN); ketaE.push_back(ietaE[i1]);
00391             kphiS.push_back(kfiS); ketaW.push_back(ietaW[i1]);
00392             std::vector<int>       ietE(1,ietaE[i1]), ietW(1,ietaW[i1]);
00393             vdetE = spr::newECALIdEW(vdetn, 0, ietE, ietW, dirnE, barrelTopo,
00394                                      endcapTopo, barrelGeom, endcapGeom,debug);
00395             vdetW = spr::newECALIdEW(vdetn, 0, ietE, ietW, dirnW, barrelTopo,
00396                                      endcapTopo, barrelGeom, endcapGeom,debug);
00397             for (unsigned int i2=0; i2<vdetW.size(); i2++) {
00398               if (std::count(vdets.begin(),vdets.end(),vdetW[i2]) == 0 &&
00399                   std::count(vdetnew.begin(),vdetnew.end(),vdetW[i2]) == 0) {
00400                 vdets.push_back(vdetW[i2]);
00401                 dirs.push_back(dirn[0]);
00402                 jetaE.push_back(0); jphiN.push_back(kfiN);
00403                 jetaW.push_back(0); jphiS.push_back(kfiS);
00404               }
00405             }
00406             for (unsigned int i2=0; i2<vdetE.size(); i2++) {
00407               if (std::count(vdets.begin(),vdets.end(),vdetE[i2]) == 0 &&
00408                   std::count(vdetnew.begin(),vdetnew.end(),vdetE[i2]) == 0) {
00409                 vdets.push_back(vdetE[i2]);
00410                 dirs.push_back(dirn[0]);
00411                 jetaE.push_back(0); jphiN.push_back(kfiN);
00412                 jetaW.push_back(0); jphiS.push_back(kfiS);
00413               }
00414             }
00415           } 
00416         }
00417       }
00418     }
00419     last = vdets.size();
00420     for (unsigned int i2=0; i2<vdetnew.size(); i2++) {
00421       if (std::count(vdets.begin(),vdets.end(),vdetnew[i2]) == 0) {
00422         vdets.push_back(vdetnew[i2]);
00423         dirs.push_back(dirnew[i2]);
00424         jetaE.push_back(ketaE[i2]);
00425         jetaW.push_back(ketaW[i2]);
00426         jphiN.push_back(kphiN[i2]);
00427         jphiS.push_back(kphiS[i2]);
00428       }
00429     }
00430     if (debug) {
00431       std::cout << "newECALIdNS::Addition results a set of " 
00432                 << (vdets.size()-last0)  << " new  cells (last " << last0
00433                 << ", iphi " << kphi << ")" << std::endl;
00434       for (unsigned int i1=last0; i1<vdets.size(); i1++) {
00435         spr::debugEcalDets (i1, vdets[i1], false);
00436         std::cout << " along " << dirs[i1] << " iphi " << jphiN[i1] << "|" 
00437                   << jphiS[i1] << " ieta " << jetaE[i1] << "|" << jetaW[i1] 
00438                   << std::endl;
00439       }
00440     }
00441     last0 = last;
00442       
00443     if (kphi > 0) {
00444       last = last0;
00445       return spr::newECALIdNS(vdets, last, jetaE, jetaW, jphiN, jphiS, dirs,
00446                               barrelTopo, endcapTopo, barrelGeom, endcapGeom,
00447                               debug);
00448     } else {
00449       if (debug) {
00450         std::cout << "newECALIdNS::Final list consists of " << vdets.size()
00451                   << " cells" << std::endl;
00452         spr::debugEcalDets (0, vdets);
00453       }
00454       return vdets;
00455     }
00456   }
00457 
00458   std::vector<DetId> newECALIdEW(std::vector<DetId>& dets, unsigned int last,
00459                                  int ieta, std::vector<CaloDirection>& dir, 
00460                                  const CaloSubdetectorTopology& barrelTopo, 
00461                                  const CaloSubdetectorTopology& endcapTopo, 
00462                                  const EcalBarrelGeometry& barrelGeom, 
00463                                  const EcalEndcapGeometry& endcapGeom,
00464                                  bool debug) {
00465 
00466     if (debug) {
00467       std::cout << "newECALIdEW::Add " << ieta << " rows of cells for " 
00468                 << last << ":" << dets.size() << ":" << (dets.size()-last) 
00469                 << " cells" << std::endl;
00470       spr::debugEcalDets (last, dets, dir);
00471     }
00472 
00473     std::vector<DetId> vdets; vdets.clear();
00474     std::vector<CaloDirection> dirs; dirs.clear();
00475     vdets.insert(vdets.end(), dets.begin(), dets.end());
00476     dirs.insert(dirs.end(), dir.begin(), dir.end());
00477 
00478     if (ieta > 0) {
00479       for (unsigned int i1=last; i1<dets.size(); i1++) {
00480         int flag = 0;
00481         std::vector<DetId> cells;
00482         spr::simpleMove(dets[i1], dir[i1], barrelTopo, endcapTopo,
00483                         barrelGeom, endcapGeom, cells, flag, debug);
00484         if (flag != 0) {
00485           if (std::count(vdets.begin(),vdets.end(),cells[0]) == 0) {
00486             CaloDirection dirn = dir[i1];
00487             if (flag < 0) {
00488               if (dirn == EAST) dirn = WEST;
00489               else              dirn = EAST;
00490             }
00491             vdets.push_back(cells[0]);
00492             dirs.push_back(dirn);
00493           }
00494         }
00495       }
00496       ieta--;
00497     }
00498     
00499     if (debug) {
00500       std::cout << "newECALIdEW::Addition results a set of " 
00501                 << (vdets.size()-dets.size()) << " new  cells" << std::endl;
00502       spr::debugEcalDets (dets.size(), vdets, dirs);
00503     }
00504 
00505     if (ieta > 0) {
00506       last = dets.size();
00507       return spr::newECALIdEW(vdets,last,ieta,dirs,barrelTopo,endcapTopo,barrelGeom,endcapGeom,debug);
00508     } else {
00509       if (debug) {
00510         std::cout << "newECALIdEW::Final list (EW) consists of " <<vdets.size()
00511                   << " cells" << std::endl;
00512         spr::debugEcalDets (0, vdets);
00513       }
00514       return vdets;
00515     }
00516   }
00517 
00518   std::vector<DetId> newECALIdEW(std::vector<DetId>& dets, unsigned int last,
00519                                  std::vector<int>& ietaE, 
00520                                  std::vector<int>& ietaW,
00521                                  std::vector<CaloDirection>& dir, 
00522                                  const CaloSubdetectorTopology& barrelTopo, 
00523                                  const CaloSubdetectorTopology& endcapTopo, 
00524                                  const EcalBarrelGeometry& barrelGeom, 
00525                                  const EcalEndcapGeometry& endcapGeom,
00526                                  bool debug) {
00527 
00528     if (debug) {
00529       std::cout << "newECALIdEW::Add " << ietaE[0] << "|" << ietaW[0]
00530                 << " rows of cells for " << (dets.size()-last) 
00531                 << " cells (last " << last << ")" << std::endl;
00532       spr::debugEcalDets (last, dets, dir);
00533     }
00534 
00535     std::vector<DetId> vdets;
00536     vdets.insert(vdets.end(), dets.begin(), dets.end());
00537     std::vector<CaloDirection> dirs;
00538     dirs.insert(dirs.end(), dir.begin(), dir.end());
00539     std::vector<int> jetaE, jetaW;
00540     jetaE.insert(jetaE.end(), ietaE.begin(), ietaE.end());
00541     jetaW.insert(jetaW.end(), ietaW.begin(), ietaW.end());
00542     int keta = 0;
00543     for (unsigned int i1=last; i1<dets.size(); i1++) {
00544       int ieta = ietaW[i1];
00545       if (dir[i1] == EAST) ieta = ietaE[i1]; 
00546       if (ieta > 0) {
00547         int flag=0;
00548         std::vector<DetId> cells;
00549         spr::simpleMove(dets[i1], dir[i1], barrelTopo, endcapTopo,
00550                         barrelGeom, endcapGeom, cells, flag, debug);
00551         ieta--;
00552         if (ieta > keta) keta = ieta;
00553         if (dir[i1] == EAST) jetaE[i1] = ieta;
00554         else                 jetaW[i1] = ieta;
00555         if (flag != 0) {
00556           if (std::count(vdets.begin(),vdets.end(),cells[0]) == 0) {
00557             vdets.push_back(cells[0]);
00558             CaloDirection dirn = dir[i1];
00559             int ketaE = ietaE[i1];
00560             int ketaW = ietaW[i1];
00561             if (dirn == EAST) ketaE = ieta;
00562             else              ketaW = ieta;
00563             if (flag < 0) {
00564               int ktmp = ketaW; ketaW    = ketaE; ketaE    = ktmp;
00565               if (dirn == EAST) dirn = WEST;
00566               else              dirn = EAST;
00567             }
00568             dirs.push_back(dirn);
00569             jetaE.push_back(ketaE);
00570             jetaW.push_back(ketaW);
00571           }
00572         }
00573       }
00574     }
00575     
00576     if (debug) {
00577       std::cout << "newECALIdEW::Addition results a set of " 
00578                 << (vdets.size()-dets.size()) << " new  cells (last " 
00579                 << dets.size() << ", ieta " << keta << ")" << std::endl;
00580       spr::debugEcalDets (dets.size(), vdets);
00581     }
00582 
00583     if (keta > 0) {
00584       last = dets.size();
00585       return spr::newECALIdEW(vdets, last, jetaE, jetaW, dirs, barrelTopo,
00586                               endcapTopo, barrelGeom, endcapGeom, debug);
00587     } else {
00588       if (debug) {
00589         std::cout << "newECALIdEW::Final list (EW) consists of " <<vdets.size()
00590                   << " cells" << std::endl;
00591         spr::debugEcalDets (0, vdets);
00592       }
00593       return vdets;
00594     }
00595   }
00596 
00597   void simpleMove(DetId& det, const CaloDirection& dir, 
00598                   const CaloSubdetectorTopology& barrelTopo, 
00599                   const CaloSubdetectorTopology& endcapTopo, 
00600                   const EcalBarrelGeometry& barrelGeom, 
00601                   const EcalEndcapGeometry& endcapGeom, 
00602                   std::vector<DetId>& cells, int& ok, bool debug) {
00603 
00604     DetId cell;
00605     ok = 0;
00606     if (det.subdetId() == EcalBarrel) {
00607       EBDetId detId = det;
00608       std::vector<DetId> neighbours = barrelTopo.getNeighbours(detId,dir);
00609       if (neighbours.size()>0 && !neighbours[0].null()) {
00610         cells.push_back(neighbours[0]);
00611         cell = neighbours[0];
00612         ok   = 1;
00613       } else {
00614         const int ietaAbs ( detId.ietaAbs() ) ; // abs value of ieta
00615         if (EBDetId::MAX_IETA == ietaAbs ) {
00616           // get ee nbrs for for end of barrel crystals
00617           const EcalBarrelGeometry::OrderedListOfEEDetId&
00618             ol( * barrelGeom.getClosestEndcapCells(detId) ) ;
00619           // take closest neighbour on the other side, that is in the endcap
00620           cell = *(ol.begin() );
00621           neighbours = endcapTopo.getNeighbours(cell,dir);
00622           if (neighbours.size()>0 && !neighbours[0].null()) ok = 1;
00623           else                                              ok =-1;
00624           for (EcalBarrelGeometry::OrderedListOfEEDetId::const_iterator iptr=ol.begin(); iptr != ol.end(); ++iptr)
00625             cells.push_back(*iptr);
00626         }
00627       }
00628     } else if (det.subdetId() == EcalEndcap) {
00629       EEDetId detId = det;
00630       std::vector<DetId> neighbours = endcapTopo.getNeighbours(detId,dir);
00631       if (neighbours.size()>0 && !neighbours[0].null()) {
00632         cells.push_back(neighbours[0]);
00633         cell = neighbours[0];
00634         ok   = 1;
00635       } else {
00636         // are we on the outer ring ?
00637         const int iphi ( detId.iPhiOuterRing() ) ;
00638         if (iphi!= 0) {
00639           // get eb nbrs for for end of endcap crystals
00640           const EcalEndcapGeometry::OrderedListOfEBDetId&
00641             ol( * endcapGeom.getClosestBarrelCells(detId) ) ;
00642           // take closest neighbour on the other side, that is in the barrel.
00643           cell = *(ol.begin() );
00644           neighbours = barrelTopo.getNeighbours(cell,dir);
00645           if (neighbours.size()>0 && !neighbours[0].null()) ok = 1;
00646           else                                              ok =-1;
00647           for (EcalEndcapGeometry::OrderedListOfEBDetId::const_iterator iptr=ol.begin(); iptr != ol.end(); ++iptr)
00648             cells.push_back(*iptr);
00649         }
00650       }
00651     }  
00652     if (debug) {
00653       std::cout << "simpleMove:: Move DetId 0x" << std::hex << det() 
00654                 << std::dec << " along " << dir << " to get 0x" << std::hex
00655                 << cell() << std::dec << " with flag " << ok << " # "
00656                 << cells.size();
00657       for (unsigned int i1=0; i1<cells.size(); ++i1) 
00658         std::cout << " " << std::hex << cells[0]() << std::dec;
00659       std::cout << std::endl;
00660     }
00661   }
00662 
00663   void extraIds(const DetId& det, std::vector<DetId>& dets, int ietaE, int ietaW, int iphiN, int iphiS, const EcalBarrelGeometry& barrelGeom, const EcalEndcapGeometry& endcapGeom, std::vector<DetId>& cells, bool debug) {
00664 
00665     if (det.subdetId() == EcalBarrel) {
00666       EBDetId id = det;
00667       if (debug) std::cout << "extraIds::Cell " << id << " rows "  << ietaW
00668                            << "|" << ietaE << " columns " << iphiS << "|"
00669                            << iphiN << std::endl;
00670       int etaC = id.ietaAbs();
00671       int phiC = id.iphi();
00672       int zsid = id.zside();
00673       for (int eta = -ietaW; eta <= ietaE; ++eta) {
00674         for (int phi = -iphiS; phi <= iphiN; ++phi) {
00675           int iphi = phiC+phi;
00676           if (iphi < 0)        iphi += 360;
00677           else if (iphi > 360) iphi -= 360;    
00678           int ieta = zsid*(etaC+eta);
00679           if (EBDetId::validDetId(ieta,iphi)) {
00680             id = EBDetId(ieta,iphi);
00681             if (barrelGeom.present(id)) {
00682               if (std::count(dets.begin(),dets.end(),(DetId)id) == 0) {
00683                 cells.push_back((DetId)id);
00684               }
00685             }
00686           }
00687         }
00688       }
00689     } else if (det.subdetId() == EcalEndcap) {
00690       EEDetId id = det;
00691       if (debug) std::cout << "extraIds::Cell " << id << " rows "  << ietaW
00692                            << "|" << ietaE << " columns " << iphiS << "|"
00693                            << iphiN << std::endl;
00694       int ixC  = id.ix();
00695       int iyC  = id.iy();
00696       int zsid = id.zside();
00697       for (int kx = -ietaW; kx <= ietaE; ++kx) {
00698         for (int ky = -iphiS; ky <= iphiN; ++ky) {
00699           int ix = ixC+kx;
00700           int iy = iyC+ky;
00701           if (EEDetId::validDetId(ix,iy,zsid)) {
00702             id = EEDetId(ix,iy,zsid);
00703             if (endcapGeom.present(id)) {
00704               if (std::count(dets.begin(),dets.end(),(DetId)id) == 0) {
00705                 cells.push_back((DetId)id);
00706               }
00707             }
00708           }
00709         }
00710       }
00711     } 
00712 
00713     if (debug) {
00714       std::cout << "extraIds:: finds " << cells.size() << " new cells" 
00715                 << std::endl;
00716       spr::debugEcalDets (0, cells);
00717     }
00718   }
00719 }