TrgC3d.hh 12.2 KB
Newer Older
1 2 3 4 5 6 7
#ifndef TrgC3d_HH
#define TrgC3d_HH

#include <iostream>
#include <cstdint>
#include <cassert>
#include <vector>
8
#include <algorithm>
9 10 11 12 13 14
#include "Particle.hh"
#include "TrgC2d.hh"

class TrgC3d {
public:
  TrgC3d() : fParticle(0) {
15 16
      fCenterNorm.first = 0;
      fCenterNorm.second = 0;
17 18 19 20 21 22 23 24 25 26 27 28
  }

  ~TrgC3d() {
  }

  void setEndcap(unsigned e) {
    fEndcap=e;
  }

  bool addTrgC2d(TrgC2d *s) {
    assert(s->endcap()==fEndcap);
    vTrgC2d.push_back(s);
29 30 31 32 33 34 35 36
    if( vTrgC2d.size() == 1 ){
        fCenterNorm.first  = ( s->centerNorm().first ) ;
        fCenterNorm.second = ( s->centerNorm().second ) ;
    }
    else{
        fCenterNorm.first  = ( fCenterNorm.first + s->centerNorm().first ) / 2;
        fCenterNorm.second = ( fCenterNorm.second + s->centerNorm().second ) / 2;
    }
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    return true;
  }
  
  void setParticle(Particle *p) {
    fParticle=p;
  }

  bool isAssociated() const {
    return fParticle!=0;
  }

  Particle* particle() {
    return fParticle;
  }

  void process(bool print=false) {
    fChiSquared=0.0;
    fNumberOfDof=0;

    if(vTrgC2d.size()<3) return;

    // Find position at the minimum fabs(z)
    /*
    unsigned lMin(1000);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      if(print) std::cout << "TrgC2d layer = " << vTrgC2d[i]->layer() << std::endl;
    
      if(lMin>vTrgC2d[i]->layer()) lMin=vTrgC2d[i]->layer();
    }

    if(print) std::cout << "lMin = " << lMin << std::endl;
    double zMin(Geometry::layerZ(fEndcap,lMin));
    */

    // Find position at the shower CoG
    
    double zw(0.0),w(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      if(print) std::cout << "TrgC2d layer = " << vTrgC2d[i]->layer() << std::endl;
      double eT(vTrgC2d[i]->transverseEnergy());
      double weight(eT*eT);
      w+=weight;
      zw+=weight*Geometry::layerZ(fEndcap,vTrgC2d[i]->layer());
    }

    double zMin(zw/w);

    /////////////////////////////////////////////

    if(print) std::cout << "zMin = " << zMin << std::endl;

    double sigma2(0.25*0.25);

    double norm(0.0);
    double mat[3]={0.0,0.0,0.0};
    double vx[2]={0.0,0.0};
    double vy[2]={0.0,0.0};

    for(unsigned i(0);i<vTrgC2d.size();i++) {
      if(print) {
	//if(vTrgC2d[i]->layer()==lMin); vTrgC2d[i]->print();
      }

      Point p(vTrgC2d[i]->point());
      double eT(vTrgC2d[i]->transverseEnergy());
      double weight(eT*eT);
      double dz(Geometry::layerZ(fEndcap,vTrgC2d[i]->layer())-zMin);

      norm+=weight;
      mat[0]+=weight/sigma2;
      mat[1]+=weight*dz/sigma2;
      mat[2]+=weight*dz*dz/sigma2;

      if(print) std::cout << "weight " << weight << " p.x() " << p.x() <<std::endl;

      vx[0]+=weight*p.x()/sigma2;
      vx[1]+=weight*p.x()*dz/sigma2;
      vy[0]+=weight*p.y()/sigma2;
      vy[1]+=weight*p.y()*dz/sigma2;
    }

    if(print) {
      std::cout << "First order x = " << vx[0]/mat[0] << std::endl;
      std::cout << "First order y = " << vy[0]/mat[0] << std::endl;

      std::cout << "Matrix " << mat[0] << " " << mat[1] << std::endl;
      std::cout << "Matrix " << mat[1] << " " << mat[2] << std::endl;
    }

    norm/=vTrgC2d.size();

    double det(mat[0]*mat[2]-mat[1]*mat[1]);

    double inv[3];
    inv[0]=mat[2]/det;
    inv[1]=-mat[1]/det;
    inv[2]=mat[0]/det;

    if(print) {
      std::cout << "MatInv " << inv[0] << " " << inv[1] << std::endl;
      std::cout << "MatInv " << inv[1] << " " << inv[2] << std::endl;
    }

    double x(   vx[0]*inv[0]+vx[1]*inv[1]);
    double tanx(vx[0]*inv[1]+vx[1]*inv[2]);
    double y(   vy[0]*inv[0]+vy[1]*inv[1]);
    double tany(vy[0]*inv[1]+vy[1]*inv[2]);

    fPosition=Point(x,y,zMin);
    fDirection=Point2D(tanx,tany);

    if(print) {
      std::cout << "x = " << x << " zMin*tanx = " << zMin*tanx << std::endl;
    }

    for(unsigned i(0);i<vTrgC2d.size();i++) {
      Point p(vTrgC2d[i]->point());
      double eT(vTrgC2d[i]->transverseEnergy());
      double weight(eT*eT);
      double dz(Geometry::layerZ(fEndcap,vTrgC2d[i]->layer())-zMin);

      fChiSquared+=(x+tanx*dz-p.x())*(x+tanx*dz-p.x())*weight/sigma2;
      fChiSquared+=(y+tany*dz-p.y())*(y+tany*dz-p.y())*weight/sigma2;
      fNumberOfDof+=2;
    }

    fChiSquared/=norm;
    fNumberOfDof-=4;    
  }

  /*
  double charge() const {
    return AdcReading::fAdcLsb*(result(m)+0.5);
  }

  double energy(unsigned m=2) const {
    return charge(m)/Constants::fFcPerGev;
  }

  double mips(unsigned m=2) const {
    return charge(m)*Constants::fMipPerFc[Geometry::thickness(*this)];

  }
  */

182 183 184 185
  pair<float,float> centerNorm(){
      return fCenterNorm;
  }

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  pair<float,float> centerEtaPhi(){
      pair<float,float> fCenterEtaPhi(0.0, 0.0);
      float etaC3d(0.0);
      float phiC3d(0.0);
      double e(0.0);      
      for(unsigned i(0);i<vTrgC2d.size();i++) {
          etaC3d += vTrgC2d[i]->transverseMips() * (vTrgC2d[i]->centerEtaPhi()).first;
          phiC3d += vTrgC2d[i]->transverseMips() * (vTrgC2d[i]->centerEtaPhi()).second;
          e+=vTrgC2d[i]->transverseMips();
      }
      fCenterEtaPhi.first = etaC3d / e; 
      fCenterEtaPhi.second = phiC3d / e; 

      return fCenterEtaPhi;
  }

  pair<float,float> CoGNorm(){
      float xC3d(0.0);
      float yC3d(0.0);
      float zC3d(0.0);

      double e(0.0);      
      for(unsigned i(0);i<vTrgC2d.size();i++) {
          xC3d += vTrgC2d[i]->transverseMips() * (vTrgC2d[i]->center()).first;
          yC3d += vTrgC2d[i]->transverseMips() * (vTrgC2d[i]->center()).second;
          zC3d += vTrgC2d[i]->transverseMips() * Geometry::layerZ(fEndcap,vTrgC2d[i]->layer());
          e+=vTrgC2d[i]->transverseMips();
      }

      fCoGNorm.first = ( xC3d / e ) / ( zC3d / e ) ; 
      fCoGNorm.second = ( yC3d / e ) / ( zC3d / e ); 

      return fCoGNorm;
  }

  float length(){
      float fLenght = fabs( Geometry::layerZ(fEndcap,vTrgC2d.back()->layer()) - Geometry::layerZ(fEndcap,vTrgC2d.front()->layer()) );
      return fLenght;
  }

226
  float C2dDistanceNorm( pair<float,float> C2dCenter ){      
227 228 229 230 231 232
      float dist = sqrt( (fCenterNorm.first-C2dCenter.first)*(fCenterNorm.first-C2dCenter.first) + (fCenterNorm.second-C2dCenter.second)*(fCenterNorm.second-C2dCenter.second) ) ;
      return dist;
  }

  float C3dDistanceNorm( TrgC3d *Clu3d ){      
      float dist = sqrt( ( fCoGNorm.first - Clu3d->CoGNorm().first )*( fCoGNorm.first - Clu3d->CoGNorm().first ) +  ( fCoGNorm.second - Clu3d->CoGNorm().second )*( fCoGNorm.second - Clu3d->CoGNorm().second ) ) ;
233 234 235
      return dist;
  }

236 237 238 239 240 241 242 243 244 245 246 247 248
  float C3dSharedTransverseMips( TrgC3d *Clu3d ){
      double SharedMipT=0.0;
      std::vector<TrgC2d*> extV = Clu3d->trgC2ds();
      for(unsigned i(0); i<extV.size(); i++ ){
          for(unsigned j(0); j<vTrgC2d.size(); j++ ){
              if( vTrgC2d[j]->getUniqueId() == extV[i]->getUniqueId() ){
                  SharedMipT+=vTrgC2d[j]->transverseMips();
              }
          }
      }
      
      return SharedMipT;
  }
249 250 251 252 253 254 255 256 257 258 259 260 261 262
    
    std::vector<double> C3dSharedC2dLayer( TrgC3d *Clu3d ){
        std::vector<double> vSharedC2dLayer;
        std::vector<TrgC2d*> extV = Clu3d->trgC2ds();
        for(unsigned i(0); i<extV.size(); i++ ){
            for(unsigned j(0); j<vTrgC2d.size(); j++ ){
                if( vTrgC2d[j]->getUniqueId() == extV[i]->getUniqueId() ){
                    vSharedC2dLayer.push_back(vTrgC2d[j]->layer());
                }
          }
        }
        
        return vSharedC2dLayer;
    }
263

264 265
    std::vector<double> C3dNotSharedC2dLayer( TrgC3d *Clu3d ){
        std::vector<double> vNotSharedC2dLayer;
266 267 268 269 270 271 272 273
        std::vector<double> vSharedC2dLayer = C3dSharedC2dLayer(Clu3d);
        if(vSharedC2dLayer.size()>0){
            for(unsigned i(0); i<vTrgC2d.size(); i++ ){
                vNotSharedC2dLayer.push_back(vTrgC2d[i]->layer());
            }
            
            for(unsigned j(0); j<vSharedC2dLayer.size(); j++ ){  
                vNotSharedC2dLayer.erase(std::remove(vNotSharedC2dLayer.begin(), vNotSharedC2dLayer.end(), vSharedC2dLayer.at(j)), vNotSharedC2dLayer.end());
274 275 276 277
            }
        }
        return vNotSharedC2dLayer;
    }
278

279 280 281
    int LayerGapInC3d(){
        int gapInC3d = 0;
        std::vector<int> vGap_tmp;
282 283 284
        for(unsigned i(0); i<vTrgC2d.size()-1; i++ ){

            if(vTrgC2d[i+1]->layer() - vTrgC2d[i]->layer() != 1) continue;
285 286 287 288 289 290 291 292
            else{ 
                gapInC3d = (vTrgC2d[i+1]->layer() - vTrgC2d[i]->layer());
                vGap_tmp.push_back(gapInC3d);
            }                
        }
        std::sort( vGap_tmp.begin(), vGap_tmp.end() );
        return vGap_tmp.back();
    }
293 294


295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  Point position() const {
    return fPosition;
  }

  Point2D direction() const {
    return fDirection;
  }

  double energy() const {
    return c2dEnergy();
  }

  double c2dEnergy() const {
    double e(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      e+=vTrgC2d[i]->trgEnergy();
    }
    return e;
  }

315 316 317 318 319 320
  bool containsSigEne(){
      for(unsigned i(0);i<vTrgC2d.size();i++) 
          if( vTrgC2d[i]->containsSigEne() )
              return true;
      return false;
  }
321 322
    

323 324 325 326 327 328 329 330
  double mips() const {
    double e(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      e+=vTrgC2d[i]->mips();
    }
    return e;
  }

331 332 333 334 335 336 337 338
  double transverseMips() const {
      double e(0.0);
      for(unsigned i(0);i<vTrgC2d.size();i++) {
          e+=vTrgC2d[i]->transverseMips();
      }
      return e;
  }

339 340 341 342
  unsigned numberOfTrgC2ds() const {
    return vTrgC2d.size();
  }

343 344 345 346 347 348 349 350
  unsigned numberOfTrgHits() const {
    unsigned n(0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      n+=vTrgC2d[i]->numberOfTrgHits();
    }
    return n;
  }

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  std::vector<TrgC2d*>& trgC2ds() {
    return vTrgC2d;
  }

  double chiSquared() const {
    return fChiSquared;
  }

  unsigned numberOfDof() const {
    return fNumberOfDof;
  }

  // Access to SimHit information

  unsigned numberOfSimHits() const {
    unsigned n(0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      n+=vTrgC2d[i]->numberOfSimHits();
    }
    return n;
  }

  double simEnergy(bool signal=false) const {
    double e(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
	e+=vTrgC2d[i]->simEnergy(signal);
    }
    return e;
  }

  double simTransverseEnergy(bool signal=false) const {
    double e(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
	e+=vTrgC2d[i]->simTransverseEnergy(signal);
    }
    return e;
  }

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

    double simMips(bool signal=false) const {
        double e(0.0);
        for(unsigned i(0);i<vTrgC2d.size();i++) {
            e+=vTrgC2d[i]->simMips(signal);
        }
        return e;
    }
    
  double simTransverseMips(bool signal=false) const {
    double e(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
	e+=vTrgC2d[i]->simTransverseMips(signal);
    }
    return e;
  }


407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  /*
  Point2D scale2D() const {
    assert(vTrgC2d.size()>0);

    Point2D pSum(0,0);
    double eSum(0.0);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      Point2D p2D(vTrgC2d[i]->point2D()/Geometry::layerZ(e,l));
      pSum=pSum+(p2D*vTrgC2d[i]->energy());
      eSum+=vTrgC2d[i]->energy();
    }

    return pSum/eSum;
  }
  */

  // I/O

  bool read(std::istream &fin, TrgC2d* r) {
    /*
    fin >> std::hex;
    if(!RecDetId::read(fin)) return false;
    fin >> std::dec;
    */

    unsigned n;
    fin >> n;
    vTrgC2d.resize(n);
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      fin >> n;
      vTrgC2d[i]=r+n;
    }    
    if(!fin) return false;
    return true;
  }

  bool write(std::ostream &fout, TrgC2d* r) const {
    /*
    fout << std::hex;
    if(!RecDetId::write(fout)) return false;
    fout << std::dec;
    */
    fout << " " << vTrgC2d.size();
    for(unsigned i(0);i<vTrgC2d.size();i++) {
      assert(vTrgC2d[i]>=r);
      fout << " " << vTrgC2d[i]-r;
    }
    if(!fout) return false;
    return true;
  }

   void print() const {
     std::cout << "TrgC3d::" << std::endl;
     std::cout << " Position ";fPosition.print();
     std::cout << " Scale ";fDirection.print();
vpalladi's avatar
vpalladi committed
462
     
463 464 465 466 467 468 469 470 471 472 473
     /*
Rec DetId 0x" << std::hex << fDetId << std::dec << " = "
	      << detector() << "/" 
	      << subdetector() << "/"
	      << endcap() << "/" 
	      << layer() << "/" 
	      << wafer() << "/"
	      << type() << "/" 
	      << cell()
	      << std::endl;
  */
474 475
   }

476 477 478 479 480 481 482 483 484

private:
  unsigned fEndcap;
  Point fPosition;
  Point2D fDirection;
  std::vector<TrgC2d*> vTrgC2d;
  double fChiSquared;
  unsigned fNumberOfDof;
  Particle *fParticle;
485
  pair<float,float> fCenterNorm;
486 487 488
  pair<float,float> fCoGNorm;
  pair<float,float> fCenterEtaPhi;
  float fLenght;  
489

490 491 492
};

#endif