CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/PhysicsTools/KinFitter/src/TFitConstraintM.cc

Go to the documentation of this file.
00001 // Classname: TFitConstraintM
00002 // Author: Jan E. Sundermann, Verena Klose (TU Dresden)      
00003 
00004 
00005 //________________________________________________________________
00006 // 
00007 // TFitConstraintM::
00008 // --------------------
00009 //
00010 // Fit constraint: mass conservation ( m_i - m_j - MassConstraint == 0 )
00011 //
00012 
00013 #include <iostream>
00014 #include <iomanip>
00015 #include "PhysicsTools/KinFitter/interface/TFitConstraintM.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 #include "TLorentzVector.h"
00018 #include "TClass.h"
00019 
00020 
00021 //----------------
00022 // Constructor --
00023 //----------------
00024 TFitConstraintM::TFitConstraintM()
00025   : TAbsFitConstraint() 
00026   ,_ParList1(0)
00027   ,_ParList2(0)
00028   ,_TheMassConstraint(0)
00029 {
00030 
00031 }
00032 
00033 TFitConstraintM::TFitConstraintM(std::vector<TAbsFitParticle*>* ParList1,
00034                                  std::vector<TAbsFitParticle*>* ParList2, Double_t Mass)
00035   : TAbsFitConstraint() 
00036   ,_ParList1(0)
00037   ,_ParList2(0)
00038 {
00039   // ParList1: Vector containing first list of constrained particles 
00040   //           ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00041   // ParList2: Vector containing second list of constrained particles 
00042   //           ( sum[ m_i ] - sum[ m_j ]  - MassConstraint == 0 )
00043 
00044   if (ParList1) {
00045     _ParList1 = (*ParList1);
00046   }
00047   if (ParList2) {
00048     _ParList2 = (*ParList2);
00049   }
00050   if (Mass >= 0) {
00051     _TheMassConstraint = Mass;
00052   }
00053   else if(Mass < 0) {
00054     edm::LogWarning ("NegativeMassConstr")
00055       << "Mass constraint in TFitConstraintM cannot be set to a negative value, will be set to 0.";
00056     _TheMassConstraint = 0.;
00057   }
00058 }
00059 
00060 TFitConstraintM::TFitConstraintM(const TString &name, const TString &title,
00061                                  std::vector<TAbsFitParticle*>* ParList1,
00062                                  std::vector<TAbsFitParticle*>* ParList2, Double_t Mass)
00063   : TAbsFitConstraint(name, title) 
00064   ,_ParList1(0)
00065   ,_ParList2(0) 
00066 {
00067   // ParList1: Vector containing first list of constrained particles 
00068   //           ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00069   // ParList2: Vector containing second list of constrained particles 
00070   //           ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00071 
00072   if (ParList1) {
00073     _ParList1 = (*ParList1);
00074   }
00075   if (ParList2) {
00076     _ParList2 = (*ParList2);
00077   }  
00078   if (Mass >= 0) {
00079     _TheMassConstraint = Mass;
00080   }
00081   else if(Mass < 0) {
00082     edm::LogWarning ("NegativeMassConstr")
00083       << "Mass constraint in TFitConstraintM cannot be set to a negative value, will be set to 0.";
00084     _TheMassConstraint = 0.;
00085   }
00086 }
00087 void TFitConstraintM::addParticle1( TAbsFitParticle* particle ) {
00088   // Add one constrained particle to first list of particles
00089   // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00090 
00091   _ParList1.push_back( particle );
00092 
00093 }
00094 
00095 void TFitConstraintM::addParticle2( TAbsFitParticle* particle ) {
00096   // Add one constrained particle to second list of particles
00097   // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00098 
00099   _ParList2.push_back( particle );
00100 
00101 }
00102 
00103 void TFitConstraintM::addParticles1( TAbsFitParticle* p1, TAbsFitParticle* p2, TAbsFitParticle* p3, TAbsFitParticle* p4,
00104                                      TAbsFitParticle* p5, TAbsFitParticle* p6, TAbsFitParticle* p7, TAbsFitParticle* p8,
00105                                      TAbsFitParticle* p9, TAbsFitParticle* p10) {
00106   // Add many constrained particle to first list of particles
00107   // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00108 
00109   if (p1) addParticle1( p1 );
00110   if (p2) addParticle1( p2 );
00111   if (p3) addParticle1( p3 );
00112   if (p4) addParticle1( p4 );
00113   if (p5) addParticle1( p5 );
00114   if (p6) addParticle1( p6 );
00115   if (p7) addParticle1( p7 );
00116   if (p8) addParticle1( p8 );
00117   if (p9) addParticle1( p9 );
00118   if (p10) addParticle1( p10 );
00119 
00120 }
00121 
00122 void TFitConstraintM::addParticles2( TAbsFitParticle* p1, TAbsFitParticle* p2, TAbsFitParticle* p3, TAbsFitParticle* p4,
00123                                      TAbsFitParticle* p5, TAbsFitParticle* p6, TAbsFitParticle* p7, TAbsFitParticle* p8,
00124                                      TAbsFitParticle* p9, TAbsFitParticle* p10) {
00125   // Add many constrained particle to second list of particles
00126   // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
00127 
00128   if (p1) addParticle2( p1 );
00129   if (p2) addParticle2( p2 );
00130   if (p3) addParticle2( p3 );
00131   if (p4) addParticle2( p4 );
00132   if (p5) addParticle2( p5 );
00133   if (p6) addParticle2( p6 );
00134   if (p7) addParticle2( p7 );
00135   if (p8) addParticle2( p8 );
00136   if (p9) addParticle2( p9 );
00137   if (p10) addParticle2( p10 );
00138 
00139 }
00140 
00141 
00142 //--------------
00143 // Destructor --
00144 //--------------
00145 TFitConstraintM::~TFitConstraintM() {
00146 
00147 }
00148 
00149 //--------------
00150 // Operations --
00151 //--------------
00152 TMatrixD* TFitConstraintM::getDerivative( TAbsFitParticle* particle ) {
00153   // returns derivative df/dP with P=(p,E) and f the constraint (f=0).
00154   // The matrix contains one row (df/dp, df/dE).
00155 
00156   TMatrixD* DerivativeMatrix = new TMatrixD(1,4);
00157   (*DerivativeMatrix) *= 0.;
00158 
00159   // Pf[4] is the 4-Mom (p,E) of the sum of particles on 
00160   // the list particle is part of 
00161   
00162   Double_t Factor = 0.;
00163   TLorentzVector Pf(0., 0., 0., 0.);
00164 
00165   if( OnList( &_ParList1, particle) ) {
00166     UInt_t Npart = _ParList1.size();
00167     for (unsigned int i=0; i<Npart; i++) {
00168       const TLorentzVector* FourVec = (_ParList1[i])->getCurr4Vec();
00169       Pf += (*FourVec);
00170     }
00171     if( Pf.M() == 0. ) {
00172       edm::LogInfo ("KinFitter")
00173         << "Division by zero in "
00174         << IsA()->GetName() << " (named " << GetName() << ", titled " << GetTitle()
00175         << ") will lead to Inf in derivative matrix for particle "
00176         << particle->GetName() << ".";
00177     }
00178     Factor = 1./ Pf.M();
00179   } else if (OnList( &_ParList2, particle) ) {
00180     UInt_t Npart = _ParList2.size();
00181     for (unsigned int i=0; i<Npart; i++) {
00182       const TLorentzVector* FourVec = (_ParList2[i])->getCurr4Vec();
00183       Pf += (*FourVec);
00184     }
00185     if( Pf.M() == 0. ) {
00186       edm::LogInfo ("KinFitter")
00187         << "Division by zero in "
00188         << IsA()->GetName() << " (named " << GetName() << ", titled " << GetTitle()
00189         << ") will lead to Inf in derivative matrix for particle "
00190         << particle->GetName() << ".";
00191     }
00192     Factor = -1./Pf.M();
00193   } else {
00194     Factor = 0.; 
00195   }
00196   
00197   (*DerivativeMatrix)(0,0) = -Pf[0] ;
00198   (*DerivativeMatrix)(0,1) = -Pf[1];
00199   (*DerivativeMatrix)(0,2) = -Pf[2];
00200   (*DerivativeMatrix)(0,3) = +Pf[3];
00201   (*DerivativeMatrix) *= Factor;
00202 
00203   return DerivativeMatrix;
00204 
00205 }
00206 
00207 
00208 Double_t TFitConstraintM::getInitValue() {
00209   // Get initial value of constraint (before the fit)
00210 
00211   Double_t InitValue(0) ;   
00212   InitValue = CalcMass(&_ParList1,true) - CalcMass(&_ParList2,true) - _TheMassConstraint ;
00213   return InitValue;
00214 }
00215 
00216 Double_t TFitConstraintM::getCurrentValue() {
00217   // Get value of constraint after the fit
00218 
00219   Double_t CurrentValue(0);
00220   CurrentValue= CalcMass(&_ParList1,false) - CalcMass(&_ParList2,false) - _TheMassConstraint;
00221   return CurrentValue;
00222 }
00223  
00224 
00225 Bool_t TFitConstraintM::OnList(std::vector<TAbsFitParticle*>* List,
00226                                TAbsFitParticle* particle) {
00227   // Checks whether list contains given particle
00228 
00229   Bool_t ok(false);
00230   UInt_t Npart = List->size();
00231   for (unsigned int i=0;i<Npart;i++) {
00232     ok = (particle == (*List)[i]);
00233     if (ok) break;
00234   }
00235   return ok;
00236 }
00237 
00238 Double_t TFitConstraintM::CalcMass(std::vector<TAbsFitParticle*>* List, Bool_t IniVal) {
00239   // Calculates initial/current invariant mass of provided list of particles
00240 
00241   TLorentzVector P(0., 0., 0., 0.);
00242   UInt_t Npart = List->size();
00243   for (unsigned int i=0;i<Npart;i++) {
00244     const TLorentzVector* FourVec = 0;
00245     if (IniVal)
00246       FourVec = ((*List)[i])->getIni4Vec();
00247     else      
00248       FourVec = ((*List)[i])->getCurr4Vec();
00249     P += (*FourVec);
00250   }
00251   return P.M();
00252 }
00253 
00254 TString TFitConstraintM::getInfoString() {
00255   // Collect information to be used for printout
00256 
00257   std::stringstream info;
00258   info << std::scientific << std::setprecision(6);
00259 
00260   info << "__________________________" << std::endl
00261        << std::endl;
00262   info <<"OBJ: " << IsA()->GetName() << "\t" << GetName() << "\t" << GetTitle() << std::endl;
00263 
00264   info << "initial value: " << getInitValue() << std::endl;
00265   info << "current value: " << getCurrentValue() << std::endl;
00266   info << "mass: " << _TheMassConstraint << std::endl;
00267 
00268   return info.str();
00269 
00270 }
00271 
00272 void TFitConstraintM::print() {
00273   // Print constraint contents
00274 
00275   edm::LogVerbatim("KinFitter") << this->getInfoString();
00276 
00277 }