CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TFitConstraintM.cc
Go to the documentation of this file.
1 // Classname: TFitConstraintM
2 // Author: Jan E. Sundermann, Verena Klose (TU Dresden)
3 
4 //________________________________________________________________
5 //
6 // TFitConstraintM::
7 // --------------------
8 //
9 // Fit constraint: mass conservation ( m_i - m_j - MassConstraint == 0 )
10 //
11 
12 #include <iostream>
13 #include <iomanip>
16 #include "TLorentzVector.h"
17 #include "TClass.h"
18 
19 //----------------
20 // Constructor --
21 //----------------
22 TFitConstraintM::TFitConstraintM() : TAbsFitConstraint(), _ParList1(0), _ParList2(0), _TheMassConstraint(0) {}
23 
24 TFitConstraintM::TFitConstraintM(std::vector<TAbsFitParticle*>* ParList1,
25  std::vector<TAbsFitParticle*>* ParList2,
26  Double_t Mass)
27  : TAbsFitConstraint(), _ParList1(0), _ParList2(0) {
28  // ParList1: Vector containing first list of constrained particles
29  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
30  // ParList2: Vector containing second list of constrained particles
31  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
32 
33  if (ParList1) {
34  _ParList1 = (*ParList1);
35  }
36  if (ParList2) {
37  _ParList2 = (*ParList2);
38  }
39  if (Mass >= 0) {
41  } else if (Mass < 0) {
42  edm::LogWarning("NegativeMassConstr")
43  << "Mass constraint in TFitConstraintM cannot be set to a negative value, will be set to 0.";
44  _TheMassConstraint = 0.;
45  }
46 }
47 
49  const TString& title,
50  std::vector<TAbsFitParticle*>* ParList1,
51  std::vector<TAbsFitParticle*>* ParList2,
52  Double_t Mass)
53  : TAbsFitConstraint(name, title), _ParList1(0), _ParList2(0) {
54  // ParList1: Vector containing first list of constrained particles
55  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
56  // ParList2: Vector containing second list of constrained particles
57  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
58 
59  if (ParList1) {
60  _ParList1 = (*ParList1);
61  }
62  if (ParList2) {
63  _ParList2 = (*ParList2);
64  }
65  if (Mass >= 0) {
67  } else if (Mass < 0) {
68  edm::LogWarning("NegativeMassConstr")
69  << "Mass constraint in TFitConstraintM cannot be set to a negative value, will be set to 0.";
70  _TheMassConstraint = 0.;
71  }
72 }
74  // Add one constrained particle to first list of particles
75  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
76 
77  _ParList1.push_back(particle);
78 }
79 
81  // Add one constrained particle to second list of particles
82  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
83 
84  _ParList2.push_back(particle);
85 }
86 
89  TAbsFitParticle* p3,
90  TAbsFitParticle* p4,
91  TAbsFitParticle* p5,
92  TAbsFitParticle* p6,
93  TAbsFitParticle* p7,
94  TAbsFitParticle* p8,
95  TAbsFitParticle* p9,
96  TAbsFitParticle* p10) {
97  // Add many constrained particle to first list of particles
98  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
99 
100  if (p1)
101  addParticle1(p1);
102  if (p2)
103  addParticle1(p2);
104  if (p3)
105  addParticle1(p3);
106  if (p4)
107  addParticle1(p4);
108  if (p5)
109  addParticle1(p5);
110  if (p6)
111  addParticle1(p6);
112  if (p7)
113  addParticle1(p7);
114  if (p8)
115  addParticle1(p8);
116  if (p9)
117  addParticle1(p9);
118  if (p10)
119  addParticle1(p10);
120 }
121 
124  TAbsFitParticle* p3,
125  TAbsFitParticle* p4,
126  TAbsFitParticle* p5,
127  TAbsFitParticle* p6,
128  TAbsFitParticle* p7,
129  TAbsFitParticle* p8,
130  TAbsFitParticle* p9,
131  TAbsFitParticle* p10) {
132  // Add many constrained particle to second list of particles
133  // ( sum[ m_i ] - sum[ m_j ] - MassConstraint == 0 )
134 
135  if (p1)
136  addParticle2(p1);
137  if (p2)
138  addParticle2(p2);
139  if (p3)
140  addParticle2(p3);
141  if (p4)
142  addParticle2(p4);
143  if (p5)
144  addParticle2(p5);
145  if (p6)
146  addParticle2(p6);
147  if (p7)
148  addParticle2(p7);
149  if (p8)
150  addParticle2(p8);
151  if (p9)
152  addParticle2(p9);
153  if (p10)
154  addParticle2(p10);
155 }
156 
157 //--------------
158 // Destructor --
159 //--------------
161 
162 //--------------
163 // Operations --
164 //--------------
166  // returns derivative df/dP with P=(p,E) and f the constraint (f=0).
167  // The matrix contains one row (df/dp, df/dE).
168 
169  TMatrixD* DerivativeMatrix = new TMatrixD(1, 4);
170  (*DerivativeMatrix) *= 0.;
171 
172  // Pf[4] is the 4-Mom (p,E) of the sum of particles on
173  // the list particle is part of
174 
175  Double_t Factor = 0.;
176  TLorentzVector Pf(0., 0., 0., 0.);
177 
178  if (OnList(&_ParList1, particle)) {
179  UInt_t Npart = _ParList1.size();
180  for (unsigned int i = 0; i < Npart; i++) {
181  const TLorentzVector* FourVec = (_ParList1[i])->getCurr4Vec();
182  Pf += (*FourVec);
183  }
184  if (Pf.M() == 0.) {
185  edm::LogInfo("KinFitter") << "Division by zero in " << IsA()->GetName() << " (named " << GetName() << ", titled "
186  << GetTitle() << ") will lead to Inf in derivative matrix for particle "
187  << particle->GetName() << ".";
188  }
189  Factor = 1. / Pf.M();
190  } else if (OnList(&_ParList2, particle)) {
191  UInt_t Npart = _ParList2.size();
192  for (unsigned int i = 0; i < Npart; i++) {
193  const TLorentzVector* FourVec = (_ParList2[i])->getCurr4Vec();
194  Pf += (*FourVec);
195  }
196  if (Pf.M() == 0.) {
197  edm::LogInfo("KinFitter") << "Division by zero in " << IsA()->GetName() << " (named " << GetName() << ", titled "
198  << GetTitle() << ") will lead to Inf in derivative matrix for particle "
199  << particle->GetName() << ".";
200  }
201  Factor = -1. / Pf.M();
202  } else {
203  Factor = 0.;
204  }
205 
206  (*DerivativeMatrix)(0, 0) = -Pf[0];
207  (*DerivativeMatrix)(0, 1) = -Pf[1];
208  (*DerivativeMatrix)(0, 2) = -Pf[2];
209  (*DerivativeMatrix)(0, 3) = +Pf[3];
210  (*DerivativeMatrix) *= Factor;
211 
212  return DerivativeMatrix;
213 }
214 
216  // Get initial value of constraint (before the fit)
217 
218  Double_t InitValue(0);
219  InitValue = CalcMass(&_ParList1, true) - CalcMass(&_ParList2, true) - _TheMassConstraint;
220  return InitValue;
221 }
222 
224  // Get value of constraint after the fit
225 
226  Double_t CurrentValue(0);
227  CurrentValue = CalcMass(&_ParList1, false) - CalcMass(&_ParList2, false) - _TheMassConstraint;
228  return CurrentValue;
229 }
230 
231 Bool_t TFitConstraintM::OnList(std::vector<TAbsFitParticle*>* List, TAbsFitParticle* particle) {
232  // Checks whether list contains given particle
233 
234  Bool_t ok(false);
235  UInt_t Npart = List->size();
236  for (unsigned int i = 0; i < Npart; i++) {
237  ok = (particle == (*List)[i]);
238  if (ok)
239  break;
240  }
241  return ok;
242 }
243 
244 Double_t TFitConstraintM::CalcMass(std::vector<TAbsFitParticle*>* List, Bool_t IniVal) {
245  // Calculates initial/current invariant mass of provided list of particles
246 
247  TLorentzVector P(0., 0., 0., 0.);
248  UInt_t Npart = List->size();
249  for (unsigned int i = 0; i < Npart; i++) {
250  const TLorentzVector* FourVec = nullptr;
251  if (IniVal)
252  FourVec = ((*List)[i])->getIni4Vec();
253  else
254  FourVec = ((*List)[i])->getCurr4Vec();
255  P += (*FourVec);
256  }
257  return P.M();
258 }
259 
261  // Collect information to be used for printout
262 
263  std::stringstream info;
264  info << std::scientific << std::setprecision(6);
265 
266  info << "__________________________" << std::endl << std::endl;
267  info << "OBJ: " << IsA()->GetName() << "\t" << GetName() << "\t" << GetTitle() << std::endl;
268 
269  info << "initial value: " << getInitValue() << std::endl;
270  info << "current value: " << getCurrentValue() << std::endl;
271  info << "mass: " << _TheMassConstraint << std::endl;
272 
273  return info.str();
274 }
275 
277  // Print constraint contents
278 
279  edm::LogVerbatim("KinFitter") << this->getInfoString();
280 }
Log< level::Info, true > LogVerbatim
static const TGPicture * info(bool iBackgroundIsBlack)
void print() override
Double_t CalcMass(std::vector< TAbsFitParticle * > *List, Bool_t IniVal)
const TString p2
Definition: fwPaths.cc:13
Double_t getCurrentValue() override
Double_t _TheMassConstraint
Bool_t OnList(std::vector< TAbsFitParticle * > *List, TAbsFitParticle *particle)
TMatrixD * getDerivative(TAbsFitParticle *particle) override
~TFitConstraintM() override
const TString p1
Definition: fwPaths.cc:12
Log< level::Info, false > LogInfo
TString getInfoString() override
void addParticles2(TAbsFitParticle *p1, TAbsFitParticle *p2=nullptr, TAbsFitParticle *p3=nullptr, TAbsFitParticle *p4=nullptr, TAbsFitParticle *p5=nullptr, TAbsFitParticle *p6=nullptr, TAbsFitParticle *p7=nullptr, TAbsFitParticle *p8=nullptr, TAbsFitParticle *p9=nullptr, TAbsFitParticle *p10=nullptr)
void addParticles1(TAbsFitParticle *p1, TAbsFitParticle *p2=nullptr, TAbsFitParticle *p3=nullptr, TAbsFitParticle *p4=nullptr, TAbsFitParticle *p5=nullptr, TAbsFitParticle *p6=nullptr, TAbsFitParticle *p7=nullptr, TAbsFitParticle *p8=nullptr, TAbsFitParticle *p9=nullptr, TAbsFitParticle *p10=nullptr)
std::pair< OmniClusterRef, TrackingParticleRef > P
std::vector< TAbsFitParticle * > _ParList1
void addParticle1(TAbsFitParticle *particle)
Double_t getInitValue() override
Log< level::Warning, false > LogWarning
std::vector< TAbsFitParticle * > _ParList2
void addParticle2(TAbsFitParticle *particle)