11 #include "vdt/vdtMath.h"
32 coolingFactor_ = conf.
getParameter<
double> (
"coolingFactor");
35 coolingFactor_=-coolingFactor_;
42 LogDebug(
"DAClusterizerinZ_vectorized") <<
"DAClusterizerInZ: invalid Tmin" << Tmin
43 <<
" reset do default " << 1. / betamax_ << endl;
53 return vdt::fast_exp( inp );
62 for(
int i=0;
i!=arg_arr_size; ++
i ) arg_out[
i]=vdt::fast_exp(arg_inp[
i]);
66 for(
int i=0;
i!=arg_arr_size; ++
i ) arg_out[
i]=
std::exp(arg_inp[
i]);
75 for (vector<reco::TransientTrack>::const_iterator it =
tracks.begin(); it
79 double t_z = ((*it).stateAtBeamLine().trackStateAtPCA()).
position().z();
80 double phi=((*it).stateAtBeamLine().trackStateAtPCA()).momentum().phi();
81 double tantheta =
tan(
82 ((*it).stateAtBeamLine().trackStateAtPCA()).momentum().theta());
86 pow((*it).track().dzError(), 2)
87 + (
pow(beamspot.BeamWidthX()*
cos(phi),2)+
pow(beamspot.BeamWidthY()*
sin(phi),2))/
pow(tantheta,2)
88 +
pow(vertexSize_, 2);
91 (*it).stateAtBeamLine().transverseImpactParameter();
98 tks.
AddItem(t_z, t_dz2, &(*it), t_pi);
103 LogDebug(
"DAClusterizerinZ_vectorized") <<
"Track count " << tks.
GetSize() << std::endl;
112 return pow(t_z - k_z, 2) / t_dz2;
117 vertex_t & gvertices,
bool useRho0,
double & rho0)
const {
123 const unsigned int nt = gtracks.
GetSize();
124 const unsigned int nv = gvertices.
GetSize();
133 unsigned int ivertex;
139 auto kernel_calc_exp_arg = [ &
beta, nv ] (
140 const unsigned int itrack,
144 const double track_z = tracks._z[itrack];
145 const double track_dz2 = tracks._dz2[itrack];
148 for (
unsigned int ivertex = 0; ivertex < nv; ++ivertex)
150 double mult_res = ( track_z - vertices._z[ivertex] );
151 vertices._ei_cache[ivertex] = -beta *
152 ( mult_res * mult_res ) / track_dz2;
158 auto kernel_add_Z = [ nv, &Z_init ] (
vertex_t const& vertices) ->
double
160 double ZTemp = Z_init;
162 for (
unsigned int ivertex = 0; ivertex < nv; ++ivertex) {
164 ZTemp += vertices._pk[ivertex] * vertices._ei[ivertex];
169 auto kernel_calc_normalization = [ &
beta, nv ] (
const unsigned int track_num,
175 const double tmp_trk_pi = tks_vec._pi[track_num];
176 const double tmp_trk_Z_sum = tks_vec._Z_sum[track_num];
177 const double tmp_trk_dz2 = tks_vec._dz2[track_num];
178 const double tmp_trk_z = tks_vec._z[track_num];
181 for (
unsigned int k = 0;
k < nv; ++
k) {
182 y_vec._se[
k] += tmp_trk_pi* y_vec._ei[
k] / tmp_trk_Z_sum;
183 w = y_vec._pk[
k] * tmp_trk_pi * y_vec._ei[
k] / tmp_trk_Z_sum / tmp_trk_dz2;
185 y_vec._swz[
k] += w * tmp_trk_z;
186 y_vec._swE[
k] += w * y_vec._ei_cache[
k]/(-
beta);
191 for (ivertex = 0; ivertex < nv; ++ivertex) {
192 gvertices.
_se[ivertex] = 0.0;
193 gvertices.
_sw[ivertex] = 0.0;
194 gvertices.
_swz[ivertex] = 0.0;
195 gvertices.
_swE[ivertex] = 0.0;
202 Z_init = rho0 * local_exp(-beta * dzCutOff_ * dzCutOff_);
206 for (itrack = 0; itrack <
nt; ++itrack) {
207 kernel_calc_exp_arg(itrack, gtracks, gvertices);
211 gtracks.
_Z_sum[itrack] = kernel_add_Z(gvertices);
215 sumpi += gtracks.
_pi[itrack];
217 if (gtracks.
_Z_sum[itrack] > 0) {
218 kernel_calc_normalization(itrack, gtracks, gvertices);
223 auto kernel_calc_z = [ &
delta, &sumpi, nv,
this, useRho0 ] (
vertex_t & vertices )
226 for (
unsigned int ivertex = 0; ivertex < nv; ++ ivertex )
228 if (vertices._sw[ivertex] > 0)
230 double znew = vertices._swz[ ivertex ] / vertices._sw[ ivertex ];
233 delta +=
pow( vertices._z[ ivertex ] - znew, 2 );
234 vertices._z[ ivertex ] = znew;
237 edm::LogInfo(
"sumw") <<
"invalid sum of weights in fit: " << vertices._sw[ivertex]
239 if (this->verbose_) {
240 LogDebug(
"DAClusterizerinZ_vectorized") <<
" a cluster melted away ? pk=" << vertices._pk[ ivertex ] <<
" sumw="
241 << vertices._sw[ivertex] << endl;
248 vertices._pk[ ivertex ] = vertices._pk[ ivertex ] * vertices._se[ ivertex ] / sumpi;
253 kernel_calc_z(gvertices);
265 const unsigned int nv = y.
GetSize();
271 for (
unsigned int k = 0; (
k + 1) < nv;
k++) {
272 if (fabs(y.
_z[
k + 1] - y.
_z[
k]) < 2.e-2) {
275 double Tc=2*swE/(y.
_sw[
k]+y.
_sw[
k+1]);
302 const unsigned int nv = y.
GetSize();
307 for (
unsigned int k = 0; (
k + 1) < nv;
k++) {
309 if (fabs(y.
_z[
k + 1] - y.
_z[
k]) < 1.e-2) {
317 LogDebug(
"DAClusterizerinZ_vectorized") <<
"Merging vertices k = " <<
k << std::endl;
328 const double beta)
const {
330 const unsigned int nv = y.
GetSize();
337 unsigned int k0 = nv;
339 for (
unsigned int k = 0;
k < nv;
k++) {
343 double pmax = y.
_pk[
k] / (y.
_pk[
k] + rho0 * local_exp(-beta * dzCutOff_
346 for (
unsigned int i = 0;
i <
nt;
i++) {
350 double p = y.
_pk[
k] * local_exp(-beta * Eik(tks.
_z[
i], y.
_z[
k],
353 if ((p > 0.9 * pmax) && (tks.
_pi[
i] > 0)) {
359 if ((nUnique < 2) && (sump < sumpmin)) {
367 LogDebug(
"DAClusterizerinZ_vectorized") <<
"eliminating prototype at " << y.
_z[
k0] <<
" with sump="
383 const unsigned int nv = y.
GetSize();
385 for (
unsigned int k = 0;
k < nv;
k++) {
390 for (
unsigned int i = 0;
i <
nt;
i++) {
392 sumwz += w * tks.
_z[
i];
395 y.
_z[
k] = sumwz / sumw;
399 for (
unsigned int i = 0;
i <
nt;
i++) {
400 double dx = tks.
_z[
i] - (y.
_z[
k]);
405 double Tc = 2. * a /
b;
410 if (T0 > 1. / betamax) {
411 return betamax /
pow(coolingFactor_,
int(
log(T0 * betamax) /
log(
412 coolingFactor_)) - 1);
415 return betamax / coolingFactor_;
430 std::vector<std::pair<double, unsigned int> >
critical;
431 for(
unsigned int k=0;
k<nv;
k++){
434 critical.push_back( make_pair(Tc,
k));
437 if (critical.size()==0)
return false;
438 stable_sort(critical.begin(), critical.end(), std::greater<std::pair<double, unsigned int> >() );
444 for(
unsigned int ic=0; ic<critical.size(); ic++){
445 unsigned int k=critical[ic].second;
447 double p1=0, z1=0, w1=0;
448 double p2=0, z2=0,
w2=0;
449 for(
unsigned int i=0;
i<
nt;
i++){
451 double p = y.
_pk[
k] * local_exp(-beta * Eik(tks.
_z[
i], y.
_z[k],
455 if(tks.
_z[
i]<y.
_z[k]){
456 p1+=
p; z1+=w*tks.
_z[
i]; w1+=
w;
462 if(w1>0){ z1=z1/w1;}
else{z1=y.
_z[
k]-
epsilon;}
466 if( ( k > 0 ) && ( y.
_z[k-1]>=z1 ) ){ z1=0.5*(y.
_z[
k]+y.
_z[k-1]); }
467 if( ( k+1 < nv) && ( y.
_z[k+1]<=z2 ) ){ z2=0.5*(y.
_z[
k]+y.
_z[k+1]); }
472 double pk1=p1*y.
_pk[
k]/(p1+
p2);
473 double pk2=p2*y.
_pk[
k]/(p1+
p2);
480 for(
unsigned int jc=ic; jc<critical.size(); jc++){
481 if (critical[jc].
second>k) {critical[jc].second++;}
492 const unsigned int nv = y.
GetSize();
500 LogDebug(
"DAClusterizerinZ_vectorized") <<
"Before Split "<< std::endl;
504 for (
unsigned int k = 0;
k < nv;
k++) {
507 ( (
k == 0) || ( y.
_z[
k - 1] < (y.
_z[
k] - zsep)) ) &&
508 ( ((
k + 1) == nv) || ( y.
_z[
k + 1] > (y.
_z[
k] + zsep)) ) )
514 double new_pk = 0.5 * y.
pk[
k];
521 else if ( (y1.
GetSize() == 0 ) ||
540 LogDebug(
"DAClusterizerinZ_vectorized") <<
"After split " << std::endl;
549 const unsigned int nv = y.
GetSize();
552 LogDebug(
"DAClusterizerinZ_vectorized") <<
"-----DAClusterizerInZ::dump ----" << endl;
553 LogDebug(
"DAClusterizerinZ_vectorized") <<
"beta=" << beta <<
" betamax= " << betamax_ << endl;
554 LogDebug(
"DAClusterizerinZ_vectorized") <<
" z= ";
555 LogDebug(
"DAClusterizerinZ_vectorized") << setprecision(4);
556 for (
unsigned int ivertex = 0; ivertex < nv; ++ ivertex) {
557 LogDebug(
"DAClusterizerinZ_vectorized") << setw(8) << fixed << y.
_z[ivertex];
559 LogDebug(
"DAClusterizerinZ_vectorized") << endl <<
"T=" << setw(15) << 1. / beta
561 LogDebug(
"DAClusterizerinZ_vectorized") << endl
564 for (
unsigned int ivertex = 0; ivertex < nv; ++ ivertex) {
565 LogDebug(
"DAClusterizerinZ_vectorized") << setw(8) << setprecision(3) << fixed << y.
_pk[ivertex];
566 sumpk += y.
_pk[ivertex];
568 LogDebug(
"DAClusterizerinZ_vectorized") << endl;
572 LogDebug(
"DAClusterizerinZ_vectorized") << endl;
573 LogDebug(
"DAClusterizerinZ_vectorized")
574 <<
"---- z +/- dz ip +/-dip pt phi eta weights ----"
576 LogDebug(
"DAClusterizerinZ_vectorized") << setprecision(4);
577 for (
unsigned int i = 0;
i <
nt;
i++) {
581 double tz = tks.
_z[
i];
582 LogDebug(
"DAClusterizerinZ_vectorized") << setw(3) <<
i <<
")" << setw(8) << fixed << setprecision(4)
583 << tz <<
" +/-" << setw(6) <<
sqrt(tks.
_dz2[
i]);
586 LogDebug(
"DAClusterizerinZ_vectorized") <<
" *";
588 LogDebug(
"DAClusterizerinZ_vectorized") <<
" ";
590 if (tks.
tt[
i]->track().hitPattern().hasValidHitInFirstPixelBarrel()) {
591 LogDebug(
"DAClusterizerinZ_vectorized") <<
"+";
593 LogDebug(
"DAClusterizerinZ_vectorized") <<
"-";
595 LogDebug(
"DAClusterizerinZ_vectorized") << setw(1)
596 << tks.
tt[
i]->track().hitPattern().pixelBarrelLayersWithMeasurement();
597 LogDebug(
"DAClusterizerinZ_vectorized") << setw(1)
598 << tks.
tt[
i]->track().hitPattern().pixelEndcapLayersWithMeasurement();
599 LogDebug(
"DAClusterizerinZ_vectorized") << setw(1) << hex
600 << tks.
tt[
i]->track().hitPattern().trackerLayersWithMeasurement()
601 - tks.
tt[
i]->track().hitPattern().pixelLayersWithMeasurement()
603 LogDebug(
"DAClusterizerinZ_vectorized") <<
"=" << setw(1) << hex
604 << tks.
tt[
i]->track().trackerExpectedHitsOuter().numberOfHits()
608 tks.
tt[
i]->stateAtBeamLine().transverseImpactParameter();
609 LogDebug(
"DAClusterizerinZ_vectorized") << setw(8) << IP.
value() <<
"+/-" << setw(6) << IP.
error();
610 LogDebug(
"DAClusterizerinZ_vectorized") <<
" " << setw(6) << setprecision(2)
611 << tks.
tt[
i]->track().pt() * tks.
tt[
i]->track().charge();
612 LogDebug(
"DAClusterizerinZ_vectorized") <<
" " << setw(5) << setprecision(2)
613 << tks.
tt[
i]->track().phi() <<
" " << setw(5)
614 << setprecision(2) << tks.
tt[
i]->track().eta();
617 for (
unsigned int ivertex = 0; ivertex < nv; ++ ivertex) {
622 LogDebug(
"DAClusterizerinZ_vectorized") << setw(8) << setprecision(3) <<
p;
624 LogDebug(
"DAClusterizerinZ_vectorized") <<
" . ";
626 E += p * Eik(tks.
_z[
i], y.
_z[ivertex], tks.
_dz2[
i]);
629 LogDebug(
"DAClusterizerinZ_vectorized") <<
" ";
632 LogDebug(
"DAClusterizerinZ_vectorized") << endl;
634 LogDebug(
"DAClusterizerinZ_vectorized") << endl <<
"T=" << 1 / beta <<
" E=" << E <<
" n=" << y.
GetSize()
635 <<
" F= " <<
F << endl <<
"----------" << endl;
649 vector<TransientVertex> clusters;
662 double beta = beta0(betamax_, tks, y);
665 LogDebug(
"DAClusterizerinZ_vectorized") <<
"Beta0 is " << beta << std::endl;
669 while ((
update(beta, tks, y,
false, rho0) > 1.
e-6) && (niter++
674 while (beta < betamax_) {
678 update(beta, tks,y,
false, rho0);
679 while(
merge(y,beta)){
update(beta, tks,y,
false, rho0);}
681 beta=beta/coolingFactor_;
683 beta=beta/coolingFactor_;
689 while ((
update(beta, tks, y,
false, rho0) > 1.
e-6) && (niter++
698 update(beta, tks,y,
false, rho0);
699 while(
merge(y,beta)){
update(beta, tks,y,
false, rho0);}
701 while(
split(beta, tks,y) && (ntry++<10) ){
703 while((
update(beta, tks,y,
false, rho0)>1.
e-6) && (niter++ < maxIterations_)){}
705 update(beta, tks,y,
false, rho0);
709 while(
merge(y,beta)){
update(beta, tks,y,
false, rho0);}
714 LogDebug(
"DAClusterizerinZ_vectorized") <<
"dump after 1st merging " << endl;
715 dump(beta, y, tks, 2);
722 for (
unsigned int k = 0;
k < y.
GetSize();
k++) {
726 while ((
update(beta, tks, y,
true, rho0) > 1.
e-8) && (niter++
730 LogDebug(
"DAClusterizerinZ_vectorized") <<
"rho0=" << rho0 <<
" niter=" << niter << endl;
731 dump(beta, y, tks, 2);
738 LogDebug(
"DAClusterizerinZ_vectorized") <<
"dump after 2nd merging " << endl;
739 dump(beta, y, tks, 2);
743 while (beta <= betastop_) {
744 while (purge(y, tks, rho0, beta)) {
746 while ((
update(beta, tks, y,
true, rho0) > 1.
e-6) && (niter++
750 beta /= coolingFactor_;
752 while ((
update(beta, tks, y,
true, rho0) > 1.
e-6) && (niter++
758 LogDebug(
"DAClusterizerinZ_vectorized") <<
"Final result, rho0=" << rho0 << endl;
759 dump(beta, y, tks, 2);
766 const unsigned int nv = y.
GetSize();
768 for (
unsigned int i = 0;
i <
nt;
i++) {
769 tks.
_Z_sum[
i] = rho0 * local_exp(-beta * dzCutOff_ * dzCutOff_);
771 for (
unsigned int k = 0;
k < nv;
k++) {
777 for (
unsigned int k = 0;
k < nv;
k++) {
781 vector<reco::TransientTrack> vertexTracks;
782 for (
unsigned int i = 0;
i <
nt;
i++) {
785 double p = y.
_pk[
k] * local_exp(-beta * Eik(tks.
_z[
i], y.
_z[
k],
787 if ((tks.
_pi[
i] > 0) && (p > 0.5)) {
789 vertexTracks.push_back(*(tks.
tt[
i]));
795 clusters.push_back(v);
803 const vector<reco::TransientTrack> &
tracks)
const {
806 std::cout <<
"###################################################" << endl;
807 std::cout <<
"# vectorized DAClusterizerInZ_vect::clusterize nt=" << tracks.size() << endl;
808 std::cout <<
"###################################################" << endl;
811 vector<vector<reco::TransientTrack> > clusters;
812 vector<TransientVertex> pv = vertices(tracks);
815 LogDebug(
"DAClusterizerinZ_vectorized") <<
"# DAClusterizerInZ::clusterize pv.size=" << pv.size()
818 if (pv.size() == 0) {
823 vector<reco::TransientTrack> aCluster = pv.begin()->originalTracks();
825 for (vector<TransientVertex>::iterator
k = pv.begin() + 1;
k != pv.end();
k++) {
826 if ( fabs(
k->position().z() - (
k - 1)->
position().z()) > (2 * vertexSize_)) {
828 clusters.push_back(aCluster);
831 for (
unsigned int i = 0;
i <
k->originalTracks().size();
i++) {
832 aCluster.push_back(
k->originalTracks().at(
i));
836 clusters.push_back(aCluster);
double *__restrict__ _ei_cache
void AddItem(double new_z, double new_dz2, const reco::TransientTrack *new_tt, double new_pi)
double *__restrict__ _swE
T getParameter(std::string const &) const
void local_exp_list(double *arg_inp, double *arg_out, const int arg_arr_size) const
T getUntrackedParameter(std::string const &, T const &) const
common ppss p3p6s2 common epss epspn46 common const1 w2
double beta0(const double betamax, track_t &tks, vertex_t &y) const
void splitAll(vertex_t &y) const
double local_exp(double const &inp) const
void AddItem(double new_z, double new_pk)
Sin< T >::type sin(const T &t)
bool split(const double beta, track_t &t, vertex_t &y) const
track_t fill(const std::vector< reco::TransientTrack > &tracks) const
unsigned int GetSize() const
std::vector< const reco::TransientTrack * > tt
bool merge(vertex_t &) const
double *__restrict__ _dz2
std::vector< std::vector< reco::TransientTrack > > clusterize(const std::vector< reco::TransientTrack > &tracks) const
U second(std::pair< T, U > const &p)
Cos< T >::type cos(const T &t)
void InsertItem(unsigned int i, double new_z, double new_pk)
double *__restrict__ _swz
Tan< T >::type tan(const T &t)
double update(double beta, track_t >racks, vertex_t &gvertices, bool useRho0, double &rho0) const
void RemoveItem(unsigned int i)
DAClusterizerInZ_vect(const edm::ParameterSet &conf)
void dump(const double beta, const vertex_t &y, const track_t &tks, const int verbosity=0) const
std::vector< TransientVertex > vertices(const std::vector< reco::TransientTrack > &tracks, const int verbosity=0) const
static int position[264][3]
unsigned int GetSize() const
double Eik(double const &t_z, double const &k_z, double const &t_dz2) const
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
bool purge(vertex_t &, track_t &, double &, const double) const
Power< A, B >::type pow(const A &a, const B &b)
double *__restrict__ _Z_sum