CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/FastSimulation/CaloGeometryTools/src/BaseCrystal.cc

Go to the documentation of this file.
00001 //FAMOS headers
00002 #include "FastSimulation/CaloGeometryTools/interface/BaseCrystal.h"
00003 
00004 // Data Formats
00005 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00006 
00007 typedef ROOT::Math::Plane3D::Point Point;
00008 
00009 BaseCrystal::BaseCrystal(const DetId&  cell):cellid_(cell)
00010 {
00011   subdetn_ = cellid_.subdetId();
00012 }
00013 
00014 void BaseCrystal::setCorners(const CaloCellGeometry::CornersVec& vec,const GlobalPoint& pos)
00015 {
00016   unsigned ncorners= vec.size();
00017   if(ncorners!=8) return;
00018 
00019   // This is really a pity to have to make the conversion GlobalPoint to XYZPoint, but the latter have many
00020   // useful properties (transformations, conversions....) that aren't implemented
00021   // for the GlobalPoints and GlobalVectors. 
00022 
00023   if(subdetn_==EcalBarrel)
00024     {
00025       if(pos.z()>0.)
00026         {
00027           corners_.clear();
00028           corners_.resize(8);
00029           for(unsigned ic=0;ic<8;++ic)
00030             {
00031               corners_[ic]=XYZPoint(vec[ic].x(),vec[ic].y(),vec[ic].z());
00032             }
00033         }
00034       else
00035         {
00036           corners_.clear();
00037           corners_.resize(8);
00038           corners_[0]=XYZPoint(vec[2].x(),vec[2].y(),vec[2].z());
00039           corners_[1]=XYZPoint(vec[3].x(),vec[3].y(),vec[3].z());
00040           corners_[2]=XYZPoint(vec[0].x(),vec[0].y(),vec[0].z());
00041           corners_[3]=XYZPoint(vec[1].x(),vec[1].y(),vec[1].z());
00042           corners_[4]=XYZPoint(vec[6].x(),vec[6].y(),vec[6].z());
00043           corners_[5]=XYZPoint(vec[7].x(),vec[7].y(),vec[7].z());
00044           corners_[6]=XYZPoint(vec[4].x(),vec[4].y(),vec[4].z());
00045           corners_[7]=XYZPoint(vec[5].x(),vec[5].y(),vec[5].z());
00046         }
00047     }
00048   else if(subdetn_==EcalEndcap)
00049     {
00050       double x=pos.x();
00051       double y=pos.y();
00052       double z=pos.z();
00053       unsigned offset=0;
00054       int zsign=1;
00055       if(z>0) 
00056         {
00057           if(x>0&&y>0) 
00058             offset=1;
00059           else  if(x<0&&y>0) 
00060             offset=2;
00061           else if(x>0&&y<0) 
00062             offset=0;
00063           else if (x<0&&y<0) 
00064             offset=3;
00065           zsign=1;
00066         }
00067       else
00068         {
00069           if(x>0&&y>0) 
00070             offset=3;
00071           else if(x<0&&y>0) 
00072             offset=2;
00073           else if(x>0&&y<0) 
00074             offset=0;
00075           else if(x<0&&y<0) 
00076             offset=1;
00077           zsign=-1;
00078         }
00079       corners_.clear();
00080       corners_.resize(8);
00081       for(unsigned ic=0;ic<4;++ic)
00082         {
00083           unsigned i1=(unsigned)((zsign*ic+offset)%4);
00084           unsigned i2=i1+4;
00085           corners_[ic]=XYZPoint(vec[i1].x(),vec[i1].y(),vec[i1].z());
00086           corners_[4+ic]=XYZPoint(vec[i2].x(),vec[i2].y(),vec[i2].z());
00087         }
00088     }
00089   computeBasicProperties();
00090 }
00091 
00092 void BaseCrystal::computeBasicProperties()
00093 {
00094   if(corners_.size()==0) return;
00095   center_=XYZPoint(0.,0.,0.);  
00096   for(unsigned ic=0;ic<8;++ic)
00097     {
00098       center_+=corners_[ic];
00099     }
00100   
00101   center_*=0.125;
00102 
00103   //  std::cout << " Ncorners ? " << corners_.size() << std::endl;
00104   frontcenter_ = 0.25*(corners_[0]+corners_[1]+corners_[2]+corners_[3]);
00105   backcenter_ = 0.25*(corners_[4]+corners_[5]+corners_[6]+corners_[7]);
00106   crystalaxis_ = backcenter_-frontcenter_;
00107   firstedgedirection_=-(corners_[1]-corners_[0]).Unit();
00108   fifthedgedirection_=-(corners_[5]-corners_[4]).Unit();
00109   lateraldirection_.resize(4);
00110   //  std::cout << " Direction laterales " << std::endl;
00111   for(unsigned il=0;il<4;++il)
00112     {
00113       lateraldirection_[il]=-(corners_[(il+1)%4]-corners_[il]).Unit();
00114     }
00115   
00116   Plane3D frontPlane((Point)corners_[0],(Point)corners_[1],(Point)corners_[2]);
00117   Plane3D backPlane ((Point)corners_[4],(Point)corners_[5],(Point)corners_[6]);
00118   lateralPlane_.resize(6);
00119   for(unsigned i=0;i<4;++i)
00120     {
00121       lateralPlane_[i]=
00122         Plane3D((Point)corners_[i],(Point)corners_[(i+1)%4],(Point)corners_[i+4]);
00123     }
00124   // Front plane i=4 (UP)
00125   lateralPlane_[4] = frontPlane;
00126   // Back plane i =5 (DOWN)
00127   lateralPlane_[5] = backPlane;
00128 
00129   exitingNormal_.resize(6);
00130   for(unsigned i=0;i<6;++i)
00131     {
00132       exitingNormal_[i] = 
00133         (lateralPlane_[i].Distance(Point(center_.X(),center_.Y(),center_.Z())) < 0.) ? 
00134         lateralPlane_[i].Normal().Unit() : -lateralPlane_[i].Normal().Unit();
00135     }
00136 }
00137 
00138 void BaseCrystal::getLateralEdges(unsigned i,XYZPoint& a,XYZPoint& b)const
00139 {
00140   if(i<4U) // i >= 0, since i is unsigned
00141     {
00142       a=corners_[i];
00143       b=corners_[i+4]; 
00144     }
00145 }
00146 
00147 void BaseCrystal::getFrontSide(XYZPoint &a,XYZPoint &b,XYZPoint &c,XYZPoint &d) const
00148 {
00149   a=corners_[0];
00150   b=corners_[1];
00151   c=corners_[2];
00152   d=corners_[3];
00153 }
00154 
00155 void BaseCrystal::getFrontSide(std::vector<XYZPoint>& corners) const
00156 {
00157   if(corners.size()==4)
00158     {
00159       corners[0]=corners_[0];
00160       corners[1]=corners_[1];
00161       corners[2]=corners_[2];
00162       corners[3]=corners_[3];
00163     }
00164 }
00165 
00166 void BaseCrystal::getBackSide(XYZPoint &a,XYZPoint &b,XYZPoint &c,XYZPoint &d) const
00167 {
00168   a=corners_[4];
00169   b=corners_[5];
00170   c=corners_[6];
00171   d=corners_[7];
00172 }
00173 
00174 void BaseCrystal::getBackSide(std::vector<XYZPoint>& corners) const
00175 {
00176   if(corners.size()==4)
00177     {
00178       corners[0]=corners_[4];
00179       corners[1]=corners_[5];
00180       corners[2]=corners_[6];
00181       corners[3]=corners_[7];
00182     }
00183 }
00184 
00185 void BaseCrystal::getLateralSide(unsigned i,XYZPoint &a,XYZPoint &b,XYZPoint &c,XYZPoint &d) const
00186 {
00187   if(i<4U) // i >= 0, since i is unsigned
00188     {
00189       getLateralEdges(i,a,b);
00190       getLateralEdges((i+1)%4,c,d);
00191     }
00192 }
00193 
00194 void BaseCrystal::getLateralSide(unsigned i,std::vector<XYZPoint>& corners) const
00195 {
00196   if(corners.size()==4&&i<4U) // i >= 0, since i is unsigned
00197     {
00198       corners[0]=corners_[i];
00199       corners[1]=corners_[i+4];
00200       corners[2]=corners_[4+(i+1)%4];
00201       corners[3]=corners_[(i+1)%4];
00202     }
00203 }
00204 
00205 void BaseCrystal::getDrawingCoordinates(std::vector<float> &x,std::vector<float> &y,std::vector<float> &z) const
00206 {
00207   x.clear();
00208   y.clear();
00209   z.clear();
00210 
00211   x.push_back(corners_[0].X());
00212   x.push_back(corners_[3].X());
00213   x.push_back(corners_[2].X());
00214   x.push_back(corners_[1].X());
00215   x.push_back(corners_[5].X());
00216   x.push_back(corners_[6].X());
00217   x.push_back(corners_[7].X());
00218   x.push_back(corners_[4].X());
00219   x.push_back(corners_[0].X());
00220   x.push_back(corners_[1].X());
00221   x.push_back(corners_[2].X());
00222   x.push_back(corners_[6].X());
00223   x.push_back(corners_[5].X());
00224   x.push_back(corners_[4].X());
00225   x.push_back(corners_[7].X());
00226   x.push_back(corners_[3].X());
00227 
00228   y.push_back(corners_[0].Y());
00229   y.push_back(corners_[3].Y());
00230   y.push_back(corners_[2].Y());
00231   y.push_back(corners_[1].Y());
00232   y.push_back(corners_[5].Y());
00233   y.push_back(corners_[6].Y());
00234   y.push_back(corners_[7].Y());
00235   y.push_back(corners_[4].Y());
00236   y.push_back(corners_[0].Y());
00237   y.push_back(corners_[1].Y());
00238   y.push_back(corners_[2].Y());
00239   y.push_back(corners_[6].Y());
00240   y.push_back(corners_[5].Y());
00241   y.push_back(corners_[4].Y());
00242   y.push_back(corners_[7].Y());
00243   y.push_back(corners_[3].Y());
00244 
00245   z.push_back(corners_[0].Z());
00246   z.push_back(corners_[3].Z());
00247   z.push_back(corners_[2].Z());
00248   z.push_back(corners_[1].Z());
00249   z.push_back(corners_[5].Z());
00250   z.push_back(corners_[6].Z());
00251   z.push_back(corners_[7].Z());
00252   z.push_back(corners_[4].Z());
00253   z.push_back(corners_[0].Z());
00254   z.push_back(corners_[1].Z());
00255   z.push_back(corners_[2].Z());
00256   z.push_back(corners_[6].Z());
00257   z.push_back(corners_[5].Z());
00258   z.push_back(corners_[4].Z());
00259   z.push_back(corners_[7].Z());
00260   z.push_back(corners_[3].Z());
00261 }
00262 
00263 
00264 
00265 
00266 void BaseCrystal::getSide(const CaloDirection& side, XYZPoint &a,XYZPoint &b,XYZPoint &c,XYZPoint &d) const
00267 {
00268   switch (side)
00269     {
00270     case UP:
00271       getFrontSide(a,b,c,d);
00272       break;
00273     case DOWN:
00274       getBackSide(a,b,c,d);
00275       break;
00276     default:
00277       getLateralSide(CaloDirectionOperations::Side(side),a,b,c,d);
00278     }
00279 }
00280 
00281 void BaseCrystal::print() const{
00282   std::cout << "CellID " << cellid_.rawId() << std::endl;
00283   std::cout << " Corners " << std::endl;
00284   for(unsigned ic=0;ic<corners_.size();++ic)
00285     std::cout << corners_[ic] << std::endl;
00286   std::cout << " Center " << center_ << std::endl;
00287   std::cout << " Front Center " << frontcenter_ << std::endl;
00288   std::cout << " Back Center " << backcenter_ << std::endl;
00289   std::cout << " Normales sortantes " << std::endl;
00290   for(unsigned id=0;id<exitingNormal_.size();++id)
00291     std::cout << exitingNormal_[id] << std::endl;
00292 }
00293 
00294 void BaseCrystal::getSide(const CaloDirection& side, std::vector<XYZPoint>& corners) const
00295 {
00296   switch (side)
00297     {
00298     case UP:
00299       getFrontSide(corners);
00300       break;
00301     case DOWN:
00302       getBackSide(corners);
00303       break;
00304     default:
00305       getLateralSide(CaloDirectionOperations::Side(side),corners);
00306     }
00307 }
00308 
00309 
00310