20 double sqr(
double f) {
return f*
f; }
24 vector<DAClusterizerInZT::track_t>
28 tks.reserve(tracks.size());
29 for(vector<reco::TransientTrack>::const_iterator it=tracks.begin(); it!=tracks.end(); it++){
32 auto tsPCA = (*it).stateAtBeamLine().trackStateAtPCA();
33 t.
z = tsPCA.position().z();
37 auto const & t_mom = tsPCA.momentum();
41 sqr((*it).track().dzError())
47 t.
dt2 =
sqr((*it).dtErrorExt()) +
sqr(vertexSizeTime);
80 vector<track_t> & tks,
82 const double rho0 )
const {
86 unsigned int nt=tks.size();
90 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
91 k->sw = 0.;
k->swz = 0.;
k->swt = 0.;
k->se = 0.;
92 k->swE = 0.;
k->tC=0.;
97 for(
unsigned int i=0;
i<
nt;
i++){
100 double Zi = rho0*
std::exp(-beta*(dzCutOff_*dzCutOff_));
102 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
112 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
113 double zratio =
k->pk*
k->ei/Zi;
115 k->se += tks[
i].pi*zratio/
k->pk;
116 double w = tks[
i].pi * zratio /( tks[
i].dz2 + tks[
i].dt2 );
119 k->swz += w * tks[
i].z;
120 k->swt += w * tks[
i].t;
121 k->swE += w * e_ik(tks[
i],*
k);
129 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
131 const double znew=
k->swz/
k->sw;
132 const double tnew=
k->swt/
k->sw;
133 delta +=
sqr(
k->z-znew) +
sqr(
k->t-tnew);
136 k->tC = 2*
k->swE/
k->sw;
138 edm::LogInfo(
"sumw") <<
"invalid sum of weights in fit: " <<
k->sw << endl;
139 if(verbose_){
cout <<
" a cluster melted away ? pk=" <<
k->pk <<
" sumw=" <<
k->sw << endl;}
140 k->tC = (rho0 == 0. ? -1 : 0);
142 if(rho0 == 0.)
k->pk =
k->pk *
k->se / sumpi;
152 if(y.size()<2)
return false;
154 for(vector<vertex_t>::iterator
k=y.begin(); (
k+1)!=y.end();
k++){
157 double rho =
k->pk + (
k+1)->pk;
159 k->z = (
k->pk *
k->z + (
k+1)->z * (
k+1)->pk)/rho;
160 k->t = (
k->pk *
k->t + (
k+1)->
t * (
k+1)->pk)/rho;
162 k->z = 0.5*(
k->z + (
k+1)->z);
163 k->t = 0.5*(
k->t + (
k+1)->
t);
179 if(y.size()<2)
return false;
181 for(vector<vertex_t>::iterator
k=y.begin(); (
k+1)!=y.end();
k++){
184 double rho=
k->pk + (
k+1)->pk;
185 double swE=
k->swE+(
k+1)->swE -
k->pk * (
k+1)->pk / rho * (
sqr((
k+1)->z -
k->z) +
187 double Tc=2*swE/(
k->sw+(
k+1)->sw);
191 k->z = (
k->pk *
k->z + (
k+1)->z * (
k+1)->pk)/rho;
192 k->t = (
k->pk *
k->t + (
k+1)->
t * (
k+1)->pk)/rho;
194 k->z = 0.5*(
k->z + (
k+1)->z);
195 k->t = 0.5*(
k->t + (
k+1)->
t);
212 if(y.size()<2)
return false;
214 unsigned int nt=tks.size();
216 vector<vertex_t>::iterator
k0=y.end();
217 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
220 double pmax=
k->pk/(
k->pk+rho0*
exp(-beta*dzCutOff_*dzCutOff_));
221 for(
unsigned int i=0;
i<
nt;
i++){
223 double p =
k->pk *
std::exp(-beta*e_ik(tks[
i],*
k)) / tks[
i].zi ;
225 if( (p > 0.9*pmax) && (tks[i].
pi>0) ){ nUnique++; }
229 if((nUnique<2)&&(sump<sumpmin)){
236 if(verbose_){
cout <<
"eliminating prototype at " << k0->z <<
"," << k0->t
237 <<
" with sump=" << sumpmin << endl;}
246 vector<track_t> & tks,
247 vector<vertex_t> & y )
const {
251 unsigned int nt=tks.size();
253 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
259 for(
unsigned int i=0;
i<
nt;
i++){
260 double w = tks[
i].pi/(tks[
i].dz2 + tks[
i].dt2);
270 for(
unsigned int i=0;
i<
nt;
i++){
271 double dx = tks[
i].z-(
k->z);
272 double dt = tks[
i].t-(
k->t);
273 double w = tks[
i].pi/(tks[
i].dz2 + tks[
i].dt2);
274 a += w*(
sqr(dx)/tks[
i].dz2 +
sqr(dt)/tks[
i].dt2);
282 return betamax/
pow(coolingFactor_,
int(
std::log(T0*betamax)*logCoolingFactor_)-1 );
285 return betamax/coolingFactor_;
290 vector<track_t> & tks,
291 vector<vertex_t> & y,
300 std::vector<std::pair<double, unsigned int> > critical;
301 for(
unsigned int ik=0; ik<y.size(); ik++){
302 if (beta*y[ik].tC > 1.){
303 critical.push_back( make_pair(y[ik].tC, ik));
306 std::stable_sort(critical.begin(), critical.end(), std::greater<std::pair<double, unsigned int> >() );
308 for(
unsigned int ic=0; ic<critical.size(); ic++){
309 unsigned int ik=critical[ic].second;
311 double p1=0, z1=0, t1=0, w1=0;
312 double p2=0, z2=0,
t2=0,
w2=0;
314 for(
unsigned int i=0;
i<tks.size();
i++){
317 double p=y[ik].pk *
exp(-beta*e_ik(tks[
i],y[ik])) / tks[
i].zi*tks[
i].pi;
318 double w=p/(tks[
i].dz2 + tks[
i].dt2);
319 if(tks[i].z < y[ik].z){
320 p1+=
p; z1+=w*tks[
i].z; t1+=w*tks[
i].t; w1+=
w;
322 p2+=
p; z2+=w*tks[
i].z;
t2+=w*tks[
i].t;
w2+=
w;
326 if(w1>0){ z1=z1/w1; t1=t1/w1;}
else{ z1=y[ik].z-
epsilon; t1=y[ik].t-
epsilon; }
330 if( ( ik > 0 ) && ( y[ik-1].z>=z1 ) ){ z1=0.5*(y[ik].z+y[ik-1].z); t1=0.5*(y[ik].t+y[ik-1].t); }
331 if( ( ik+1 < y.size()) && ( y[ik+1].z<=z2 ) ){ z2=0.5*(y[ik].z+y[ik+1].z);
t2=0.5*(y[ik].t+y[ik+1].t); }
337 vnew.
pk = p1*y[ik].pk/(p1+
p2);
338 y[ik].pk= p2*y[ik].pk/(p1+
p2);
343 y.insert(y.begin()+ik, vnew);
346 for(
unsigned int jc=ic; jc<critical.size(); jc++){
347 if (critical[jc].
second>ik) {critical[jc].second++;}
367 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
368 if ( ( (
k==y.begin())|| (
k-1)->z <
k->z - zsep) && (((
k+1)==y.end() )|| (
k+1)->z >
k->z + zsep)) {
375 vnew.
pk= 0.5* (*k).pk;
376 (*k).pk= 0.5* (*k).pk;
380 }
else if( y1.empty() || (y1.back().z <
k->z -zsep) || (y1.back().t <
k->t - tsep) ){
396 verbose_(conf.getUntrackedParameter<bool>(
"verbose",
false)),
398 vertexSize_(conf.getParameter<double>(
"vertexSize")),
400 coolingFactor_(
std::
sqrt(conf.getParameter<double>(
"coolingFactor"))),
403 dzCutOff_(conf.getParameter<double>(
"dzCutOff")),
404 d0CutOff_(conf.getParameter<double>(
"d0CutOff")),
410 edm::LogWarning(
"DAClusterizerInZT") <<
"DAClusterizerInZT: invalid Tmin" << Tmin <<
" reset do default " << 1./
betamax_ << endl;
428 for(vector<track_t>::const_iterator
t=tks0.begin();
t!=tks0.end();
t++){tks.push_back(*
t); }
429 std::stable_sort(tks.begin(), tks.end(), recTrackLessZ1);
431 cout <<
"-----DAClusterizerInZT::dump ----" << endl;
432 cout <<
" beta=" << beta <<
" betamax= " <<
betamax_ << endl;
435 for(vector<vertex_t>::const_iterator
k=y.begin();
k!=y.end();
k++){
438 cout << endl <<
" t= ";
439 for(vector<vertex_t>::const_iterator
k=y.begin();
k!=y.end();
k++){
442 cout << endl <<
"T=" << setw(15) << 1./beta <<
" Tc= ";
443 for(vector<vertex_t>::const_iterator
k=y.begin();
k!=y.end();
k++){
447 cout << endl <<
" pk=";
449 for(vector<vertex_t>::const_iterator
k=y.begin();
k!=y.end();
k++){
450 cout << setw(8) << setprecision(3) <<
fixed <<
k->pk;
458 cout <<
"---- z +/- dz t +/- dt ip +/-dip pt phi eta weights ----" << endl;
460 for(
unsigned int i=0;
i<tks.size();
i++){
464 cout << setw (3)<<
i <<
")" << setw (8) <<
fixed << setprecision(4)<< tz <<
" +/-" << setw (6)<<
sqrt(tks[
i].dz2)
465 << setw(8) <<
fixed << setprecision(4) << tt <<
" +/-" << setw(6) <<
std::sqrt(tks[
i].dt2) ;
470 cout << setw(1) << tks[
i].tt->track().hitPattern().pixelBarrelLayersWithMeasurement();
471 cout << setw(1) << tks[
i].tt->track().hitPattern().pixelEndcapLayersWithMeasurement();
472 cout << setw(1) << hex << tks[
i].tt->track().hitPattern().trackerLayersWithMeasurement()-tks[
i].tt->track().hitPattern().pixelLayersWithMeasurement() <<
dec;
476 cout << setw (8) << IP.
value() <<
"+/-" << setw (6) << IP.
error();
477 cout <<
" " << setw(6) << setprecision(2) << tks[
i].tt->track().pt()*tks[
i].tt->track().charge();
478 cout <<
" " << setw(5) << setprecision(2) << tks[
i].tt->track().phi()
479 <<
" " << setw(5) << setprecision(2) << tks[
i].tt->track().eta() ;
482 for(vector<vertex_t>::const_iterator
k=y.begin();
k!=y.end();
k++){
483 if((tks[
i].
pi>0)&&(tks[
i].zi>0)){
487 cout << setw (8) << setprecision(3) <<
p;
491 E+=p*
e_ik(tks[i],*
k);
499 cout << endl <<
"T=" << 1/beta <<
" E=" << E <<
" n="<< y.size() <<
" F= " <<
F << endl <<
"----------" << endl;
507 vector< TransientVertex >
512 vector<track_t> tks=
fill(tracks);
513 unsigned int nt=tks.size();
517 if (tks.empty())
return clusters;
541 split(beta, tks,y, 1.);
560 while(
split(beta, tks,y,1.) && (ntry++<10) ){
569 if(
verbose_ ){
cout <<
"dump after 1st merging " << endl;
dump(beta,y,tks,2);}
577 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
k->pk =1.; }
580 if(
verbose_ ){
cout <<
"rho0=" << rho0 <<
" niter=" << niter << endl;
dump(beta,y,tks,2);}
584 while(
merge(y,tks.size())){}
585 if(
verbose_ ){
cout <<
"dump after 2nd merging " << endl;
dump(beta,y,tks,2);}
590 while(
purge(y,tks,rho0, beta)){
600 cout <<
"Final result, rho0=" << rho0 << endl;
605 for(
unsigned int i=0;
i<
nt;
i++){
607 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
613 for(vector<vertex_t>::iterator
k=y.begin();
k!=y.end();
k++){
616 vector< reco::TransientTrack > vertexTracks;
621 for(
unsigned int i=0;
i<
nt;
i++){
622 const double invdt = 1.0/
std::sqrt(tks[
i].dt2);
624 double p =
k->pk *
exp(-beta*
e_ik(tks[
i],*
k)) / tks[
i].zi;
625 if( (tks[i].
pi>0) && ( p > 0.5 ) ){
627 vertexTracks.push_back(*(tks[i].
tt)); tks[
i].zi=0;
628 mean += tks[
i].t*invdt*
p;
629 expv_x2 += tks[
i].t*tks[
i].t*invdt*
p;
635 expv_x2 = expv_x2/normw;
636 const double time_var = expv_x2 - mean*
mean;
637 const double crappy_error_guess =
std::sqrt(time_var);
641 0,0,0,crappy_error_guess);
643 clusters.push_back(v);
655 vector< vector<reco::TransientTrack> >
660 cout <<
"###################################################" << endl;
661 cout <<
"# DAClusterizerInZT::clusterize nt="<<tracks.size() << endl;
662 cout <<
"###################################################" << endl;
665 vector< vector<reco::TransientTrack> >
clusters;
666 vector< TransientVertex >
pv=
vertices(tracks);
668 if(
verbose_){
cout <<
"# DAClusterizerInZT::clusterize pv.size="<<pv.size() << endl; }
669 if (pv.size()==0){
return clusters;}
673 vector< reco::TransientTrack> aCluster=pv.begin()->originalTracks();
677 std::cout <<
' ' << pv.begin()->position() <<
' ' << pv.begin()->time() << std::endl;
680 for(vector<TransientVertex>::iterator
k=pv.begin()+1;
k!=pv.end();
k++){
683 std::cout <<
' ' <<
k->position() <<
' ' <<
k->time() << std::endl;
688 clusters.push_back(aCluster);
691 for(
unsigned int i=0;
i<
k->originalTracks().size();
i++){
692 aCluster.push_back(
k->originalTracks().at(
i));
696 clusters.push_back(aCluster);
698 if(
verbose_) {
std::cout <<
"# DAClusterizerInZT::clusterize clusters.size="<<clusters.size() << std::endl; }
std::vector< std::vector< reco::TransientTrack > > clusterize(const std::vector< reco::TransientTrack > &tracks) const
T getParameter(std::string const &) const
common ppss p3p6s2 common epss epspn46 common const1 w2
const reco::TransientTrack * tt
double update(double beta, std::vector< track_t > &tks, std::vector< vertex_t > &y, const double rho0=0.0) const
std::vector< TransientVertex > vertices(const std::vector< reco::TransientTrack > &tracks, const int verbosity=0) const
double e_ik(const track_t &t, const vertex_t &k) const
void dump(const double beta, const std::vector< vertex_t > &y, const std::vector< track_t > &tks, const int verbosity=0) const
U second(std::pair< T, U > const &p)
auto const T2 &decltype(t1.eta()) t2
void splitAll(std::vector< vertex_t > &y) const
Abs< T >::type abs(const T &t)
bool split(double beta, std::vector< track_t > &tks, std::vector< vertex_t > &y, double threshold) const
double BeamWidthX() const
beam width X
std::vector< track_t > fill(const std::vector< reco::TransientTrack > &tracks) const
bool merge(std::vector< vertex_t > &, int) const
double BeamWidthY() const
beam width Y
Square< F >::type sqr(const F &f)
static int position[264][3]
DAClusterizerInZT(const edm::ParameterSet &conf)
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
double beta0(const double betamax, std::vector< track_t > &tks, std::vector< vertex_t > &y) const
Power< A, B >::type pow(const A &a, const B &b)
bool purge(std::vector< vertex_t > &, std::vector< track_t > &, double &, const double) const