2 #include "StKFVerticesCollection.h"
5 #include "TRSymMatrix.h"
7 #include "TPolyMarker.h"
10 Double_t StKFVerticesCollection::fgVxPenaltyFactor = 1000;
12 StKFVerticesCollection::StKFVerticesCollection(Int_t NoPeaks, Double_t *zOfPeaks, Double_t sigmaXY, Double_t sigmaZ) :
13 fVertices(NoPeaks,0) {
14 fVertices.SetOwner(kTRUE);
15 for (Int_t peak = 0; peak < NoPeaks; peak++) {
16 AddVertex(0.,0., zOfPeaks[peak], sigmaXY, sigmaZ);
20 void StKFVerticesCollection::AddVertex(Double_t x, Double_t y, Double_t z, Double_t sigmaXY, Double_t sigmaZ) {
22 vtx->Vertex().SetBeamConstraint(x, y, z, sigmaXY, sigmaXY, sigmaZ);
23 fVertices.AddLast(vtx);
27 Int_t N2 = col.NoVertices();
28 for (Int_t i = N2-1; i >= 0; i--) {
29 fVertices.AddLast(col.Remove(i));
34 void StKFVerticesCollection::SetMc(Int_t NoMuMcVertex, Int_t NoMuMcTrack,
const Float_t *time,
35 const Float_t *x,
const Float_t *y,
const Float_t *z,
36 const Int_t *NoDaughters,
const Int_t *IdParTrk,
const Int_t *gePid) {
37 Int_t Nvtx = NoVertices();
38 for (Int_t l = 0; l < Nvtx; l++) {
41 Int_t kv = V->IdTruth();
42 if (kv > 0 && kv <= NoMuMcVertex) {
43 if (time && x && y && z && NoDaughters && IdParTrk) {
44 Int_t kvp = IdParTrk[kv-1];
46 if (kvp > 0 && kvp <= NoMuMcTrack) ge = gePid[kvp-1];
47 V->SetMc(time[kv-1],x[kv-1],y[kv-1],z[kv-1],NoDaughters[kv-1],ge);
54 Int_t Nvtx = vc.NoVertices();
55 for (Int_t l = 0; l < Nvtx; l++) {
58 os << Form(
"Vtx: %3i",l) << *V << endl;
63 Double_t StKFVerticesCollection::DoTrack2VertexAssociation(
const TObjArray &particles) {
64 Double_t chi2Total = 0;
65 Int_t NVtx = NoVertices();
66 Int_t Ntracks = particles.GetEntriesFast();
68 TArrayD Chi2s(Ntracks);
69 for (Int_t l = 0; l < NVtx; l++) {
75 for (Int_t k = 0; k < Ntracks; k++) {
77 particle = (
KFParticle *) particles.UncheckedAt(k);
78 if (! particle)
continue;
79 if (particle->GetID() > 100000)
continue;
80 Double_t chi2il = particle->GetDeviationFromVertex(vtx->Vertex());
85 TMath::Sort(Ntracks,Chi2s.GetArray(),Idx.GetArray(),0);
87 for (Int_t j = 0; j < Ntracks; j++) {
89 particle = (
KFParticle *) particles.UncheckedAt(k);
90 if (! particle)
continue;
91 if (Chi2s[k] > StAnneling::Chi2Cut())
break;
93 chi2Vx += track->Chi2()/2 + TMath::Log(track->Weight() + StAnneling::Weight());
96 if (vtx->NoTracks() < 2) {
100 if (StKFVertex::Debug()) vtx->PrintW(
"DoTrack2VertexAssociation ");
104 fVertices.Compress();
105 if (NoVertices()) UpdateWeights();
109 Double_t StKFVerticesCollection::UpdateStVertexTrackAssociation() {
110 Double_t chi2Total = 0;
111 Int_t NVtx = NoVertices();
112 for (Int_t l = 0; l < NVtx; l++) {
115 chi2Total += vtx->UpdateVertex2TrackChi2();
120 void StKFVerticesCollection::CleanDuplicatedVertices() {
121 Int_t NVtx = NoVertices();
123 for (Int_t l = 1; l < NVtx; l++) {
125 if (! vtxl)
continue;
126 Int_t NL = vtxl->NoTracks();
127 TRVector vL(3,vtxl->Vertex().Parameters());
128 TRSymMatrix CL(3,vtxl->Vertex().CovarianceMatrix());
129 for (Int_t m = 0; m < l; m++) {
131 if (! vtxm)
continue;
133 Int_t NM = vtxm->NoTracks();
135 for (Int_t i = 0; i < NL; i++)
136 for (Int_t j = 0; j < NM; j++)
137 if (vtxl->Track(i)->OrigParticle() == vtxm->Track(j)->OrigParticle()) Nmatched++;
138 if (Nmatched == TMath::Min(NL,NM)) {
139 TRVector vM(3,vtxm->Vertex().Parameters());
140 TRSymMatrix CM(3,vtxm->Vertex().CovarianceMatrix());
144 Double_t chi2 = G.Product(vM,TRArray::kATxSxA);
145 Double_t prob = TMath::Prob(chi2,3);
147 if ((NL > NM) || ((NL == NM) && (vtxl->Vertex().GetChi2() < vtxm->Vertex().GetChi2()))) {
148 if (StKFVertex::Debug()) {
149 vtxm->Print(Form(
"Cleaned Vertex prob %7.2f M %3i keep L %3i L(%3i/%7.2f) M (%3i/%7.2f)\t",
150 prob,m,l,NL,vtxl->Vertex().GetChi2(),NM,vtxm->Vertex().GetChi2()));
152 delete vtxm;
Vertex(m) = 0;
155 if (StKFVertex::Debug()) {
156 vtxl->Print(Form(
"Cleaned Vertex prob %7.2f L %3i keep M %3i M(%3i/%7.2f) L (%3i/%7.2f)",
157 prob,l,m,NM,vtxm->Vertex().GetChi2(),NL,vtxl->Vertex().GetChi2()));
159 delete vtxl;
Vertex(l) = 0;
166 fVertices.Compress();
169 void StKFVerticesCollection::MergeDuplicatedVertices() {
171 for (Int_t l = 1; l < NoVertices(); l++) {
173 if (! vtxl)
continue;
174 if (! vtxl->NoTracks()) {
delete vtxl; vtxl =
Vertex(l) = 0;
continue;}
175 TRVector vL(3,vtxl->Vertex().Parameters());
176 TRSymMatrix CL(3,vtxl->Vertex().CovarianceMatrix());
177 for (Int_t m = 0; m < l; m++) {
179 if (! vtxm)
continue;
180 if (! vtxm->NoTracks()) {
delete vtxm; vtxm =
Vertex(m) = 0;
continue;}
181 TRVector vM(3,vtxm->Vertex().Parameters());
183 if (vM.Mag() > 5.)
continue;
184 TRSymMatrix CM(3,vtxm->Vertex().CovarianceMatrix());
190 Double_t chi2 = G.Product(vM,TRArray::kATxSxA);
191 Double_t prob = TMath::Prob(chi2,3);
195 delete vtxl; vtxl =
Vertex(l) = 0;
201 fVertices.Compress();
204 void StKFVerticesCollection::UpdateWeights() {
205 Int_t NVtx = NoVertices();
206 for (Int_t l = 0; l < NVtx; l++) {
208 if (! vtxl)
continue;
210 if (StKFVertex::Debug()) vtxl->PrintW(
"Weights to Update ");
211 for (Int_t i = 0; i < vtxl->NoTracks(); i++) {
212 Double_t Dominator = TMath::Exp(-StAnneling::Chi2Cut()/(2*StAnneling::Temperature())) + vtxl->Track(i)->Weight();
213 for (Int_t m = 0; m < NVtx; m++) {
214 if (l == m)
continue;
216 if (! vtxm)
continue;
217 for (Int_t j = 0; j < vtxm->NoTracks(); j++) {
218 if (vtxl->Track(i)->OrigParticle() == vtxm->Track(j)->OrigParticle()) {
219 Dominator += vtxm->Track(j)->Weight();
224 vtxl->Track(i)->W() = vtxl->Track(i)->Weight()/Dominator;
225 vtxl->Track(i)->Particle() = *(vtxl->Track(i)->OrigParticle());
226 Double_t *CovXyz = vtxl->Track(i)->Particle().CovarianceMatrix();
227 for (Int_t j = 0; j < 36; j++) CovXyz[j] = CovXyz[j]/vtxl->Track(i)->W();
229 if (StKFVertex::Debug()) vtxl->PrintW(
"Updated Weights ");
233 void StKFVerticesCollection::UniqueTracks2VertexAssociation(){
235 Int_t NVtx = NoVertices();
236 for (Int_t l = 0; l < NVtx; l++) {
238 if (! vtxl)
continue;
240 for (Int_t i = vtxl->NoTracks()-1; i >= 0; i--) {
241 if (! vtxl->Track(i))
continue;
242 Double_t WMax = vtxl->Track(i)->Weight();
246 KFParticle *particleMax = vtxl->Track(i)->OrigParticle();
247 for (Int_t m = 0; m < NVtx; m++) {
248 if (l == m)
continue;
250 if (! vtxm)
continue;
251 for (Int_t j = 0; j < vtxm->NoTracks(); j++) {
252 if (particleMax == vtxm->Track(j)->OrigParticle()) {
254 if (vtxm->Track(j)->Weight() > WMax) {
255 WMax = vtxm->Track(j)->Weight();
258 particleMax = vtxm->Track(j)->OrigParticle();
265 for (Int_t m = 0; m < NVtx; m++) {
267 if (! vtxm)
continue;
268 delete vtxm->Remove(particleMax);
271 if (! vtxl->NoTracks())
break;
275 for (Int_t m = 0; m < NVtx; m++) {
277 if (! vtxm)
continue;
279 if (particleMax->GetID()%100000) {
280 delete vtxm->Remove(particleMax);
282 if (vtxm->NoTracks() == 0) {
delete vtxm;
Vertex(m) = 0;}
284 }
else vtxm->Track(iMax)->W() = vtxm->Track(iMax)->Weight();
289 for (Int_t l = 0; l < NVtx; l++) {
291 if (! vtxl)
continue;
292 if (vtxl->NoTracks() == 0) {
delete vtxl;
Vertex(l) = 0;}
294 fVertices.Compress();
297 for (Int_t l = 0; l < NVtx; l++) {
299 if (! vtxl)
continue;
300 Int_t N = vtxl->NoTracks();
301 for (Int_t i = 0; i < N; i++) {
302 KFParticle *particle = vtxl->Track(i)->OrigParticle();;
303 Int_t
ID = particle->GetID()%100000 + 100000*vtxl->ID();;
309 Double_t StKFVerticesCollection::Fit(Int_t marker, TCanvas *c1, TH1 *Vtx) {
311 Double_t chi2Total = 1e10;
312 fVertices.Compress();
313 Int_t NVtx = NoVertices();
314 if (! NVtx)
return chi2Total;
315 for (Int_t l = 0; l < NVtx; l++) {
318 vtx->Vertex().SetBeamConstraintOff();
320 if (vtx->Vertex().GetNDF() < 1 || vtx->NoTracks() < 2) {
325 chi2Total += fgVxPenaltyFactor;
328 fVertices.Compress();
329 CleanDuplicatedVertices();
330 fVertices.Compress();
331 chi2Total = UpdateStVertexTrackAssociation();
334 if (StKFVertex::Debug()) {
335 cout <<
"chi2Total = " << chi2Total
336 <<
" at Temperature " << StAnneling::Temperature()
337 <<
" and Log(Temperature) " << TMath::Log(StAnneling::Temperature())
338 <<
" no. vertices " << NoVertices()
342 Double_t ymax = Vtx->GetMaximum();
343 for (Int_t i = 0; i < NVtx; i++) {
346 Double_t X = vtx->Vertex().GetParameter(2);
347 Double_t Y = vtx->NoTracks();
348 if (Y > ymax) ymax = Y;
349 TPolyMarker * pm =
new TPolyMarker(1, &X, &Y);
350 Vtx->GetListOfFunctions()->Add(pm);
351 pm->SetMarkerColor(TMath::Log(StAnneling::Temperature())+2);
355 pm->SetMarkerColor(4);
356 if (StKFVertex::Debug()) vtx->PrintW();
358 pm->SetMarkerStyle(m);
359 pm->SetMarkerSize(2);
363 Vtx->SetMaximum(ymax);