#include "hparticlegeantpair.h"
#include "hparticletool.h"
#include "hphysicsconstants.h"
#include <iostream>
#include <iomanip>
using namespace std;
ClassImp(HParticleGeantPair)
Bool_t HParticleGeantPair::fbCheckAcceptance=kTRUE;
HParticleGeantPair::HParticleGeantPair()
{
    clear();
}
HParticleGeantPair::~HParticleGeantPair()
{
}
Bool_t HParticleGeantPair::isTruePair()
{
    
    
    if(fstatusFlags&0x01) return kTRUE;
    return kFALSE;
}
Bool_t HParticleGeantPair::isInAcceptance(Int_t ver)
{
    if(ver<0)        { if( (fstatusFlags&6) == 6 ) return kTRUE; }  
    else if(ver==0)  { if( (fstatusFlags&2) == 2 ) return kTRUE; }  
    else if(ver==1)  { if( (fstatusFlags&4) == 4 ) return kTRUE; }  
    return kFALSE;
}
Int_t HParticleGeantPair::isFakePair()
{
    
    
    
    if     (fPID[0] < 0 && fPID[1] < 0 ) return 2;
    else if(fPID[0] < 0 || fPID[1] < 0 ) return 1;
    else                                 return 0;
}
Bool_t  HParticleGeantPair::calcVertex()
{
    if(!fcand[0] || !fcand[1]) {
	Error("calcVertex()","Candidates not yet filled! Skipped!");
	return kFALSE;
    }
    HGeomVector base1,dir1,base2,dir2,dirMother;
    Float_t x,y,z;
    fcand[0]->getVertex(x,y,z);
    base1.setXYZ(x,y,z);
    fcand[1]->getVertex(x,y,z);
    base2.setXYZ(x,y,z);
    dir1.setXYZ(fc[0].X(),fc[0].Y(),fc[0].Z());
    dir2.setXYZ(fc[1].X(),fc[1].Y(),fc[1].Z());
    fDecayVertex  = HParticleTool::calcVertexAnalytical(base1,dir1,base2,dir2); 
    dirMother.setXYZ((*this).X(),(*this).Y(),(*this).Z());
    fVerMinDistCand[0]   = HParticleTool::calculateMinimumDistanceStraightToPoint(base1,dir1,fEventVertex); 
    fVerMinDistCand[1]   = HParticleTool::calculateMinimumDistanceStraightToPoint(base2,dir2,fEventVertex); 
    fVerMinDistMother    = HParticleTool::calculateMinimumDistanceStraightToPoint(fDecayVertex,dirMother,fEventVertex);
    fVerDistMother       = (fDecayVertex-fEventVertex).length(); 
    fMinDistCandidates   = HParticleTool::calculateMinimumDistance(base1,dir1,base2,dir2);
    return kTRUE;
}
Bool_t  HParticleGeantPair::calcVectors(Int_t pid1,Int_t pid2,Int_t motherpid,HGeomVector& vertex)
{
    
    
    if(!fcand[0] || !fcand[1]) { Error("calcVectors()","Candidates not yet filled! Skipped!"); return kFALSE; }
    fPID[0]    = pid1;
    fPID[1]    = pid2;
    fMotherPID = motherpid;
    fEventVertex = vertex;
    HParticleTool::getTLorentzVector(fcand[0],fc[0],fPID[0]);
    HParticleTool::getTLorentzVector(fcand[1],fc[1],fPID[1]);
    (*((TLorentzVector*)this)) =  fc[0] + fc[1];
    foAngle = fc[0].Angle(fc[1].Vect()) * TMath::RadToDeg();
    calcVertex();
    return kTRUE;
}
Bool_t  HParticleGeantPair::setPair(HGeantKine* cnd1,Int_t pid1,
			    HGeantKine* cnd2,Int_t pid2,
			    HGeantKine* mother,Int_t motherpid,HGeomVector& vertex)
{
    
    
    
    
    
    
    
    fcand[0]     = cnd1;
    fcand[1]     = cnd2;
    fmother      = mother;
    if(fmother){
	if(motherpid <= 0  ) fMotherPID = fmother->getID();
	else                 fMotherPID = motherpid;
    } else {
	if(motherpid > 0)    fMotherPID = motherpid;
	else {
	    Warning("setPair()","HGeantKine pointer for mother == 0 and motherpid not valid");
	}
    }
    if(fcand[0] && fcand[1]){
       if( fcand[0]->getParentTrack()!=0 && fcand[0]->getParentTrack() == fcand[1]->getParentTrack() ) fstatusFlags = fstatusFlags|0x01;
       if(fbCheckAcceptance && fcand[0]->isInAcceptance()) fstatusFlags = fstatusFlags|0x02;
       if(fbCheckAcceptance && fcand[1]->isInAcceptance()) fstatusFlags = fstatusFlags|0x04;
    }
    return calcVectors(pid1,pid2,motherpid,vertex);
}
Bool_t  HParticleGeantPair::setPair(HGeantKine* cnd1,HGeantKine* cnd2,
			    HGeantKine* mother, Int_t motherpid,HGeomVector& vertex)
{
    
    
    
    
    
    
    
    fcand[0]     = cnd1;
    fcand[1]     = cnd2;
    fmother      = mother;
    if(fmother){
	if(motherpid <= 0  ) fMotherPID = fmother->getID();
	else                 fMotherPID = motherpid;
    } else {
	if(motherpid > 0)    fMotherPID = motherpid;
        else {
	    Warning("setPair()","HGeantKine pointer for mother == 0 and motherpid not valid");
	}
    }
    Int_t pid1 =  cnd1->getID();
    Int_t pid2 =  cnd2->getID();
    if(fcand[0] && fcand[1]){
       if( fcand[0]->getParentTrack()!=0 && fcand[0]->getParentTrack() == fcand[1]->getParentTrack() ) fstatusFlags = fstatusFlags|0x01;
       if(fbCheckAcceptance && fcand[0]->isInAcceptance()) fstatusFlags = fstatusFlags|0x02;
       if(fbCheckAcceptance && fcand[1]->isInAcceptance()) fstatusFlags = fstatusFlags|0x04;
    }
    return calcVectors(pid1,pid2,motherpid,vertex);
}
void  HParticleGeantPair::print(UInt_t selection)
{
    
    
    
    
    
    cout<<"HParticleGeantPair::print() --------------------------------------"<<endl;
    if( (selection>>0) & 0x01){
	if(fcand[0]) fcand[0]->print();
	if(fcand[1]) fcand[1]->print();
        if(fmother)  fmother->print();
    }
    if( (selection>>1) & 0x01){
	cout<<"    pid1 = "   <<fPID[0]
	    <<", pid2 = "     <<fPID[1]
	    <<", motherpid = "<<fMotherPID
	    <<", oAngle = "<<foAngle
	    <<", chrg1 = "  << HPhysicsConstants::charge(fPID[0])
	    <<", chrg2 = "  << HPhysicsConstants::charge(fPID[1])
	    <<endl;
    }
    if( (selection>>2) & 0x01){
	cout<<"primary vertex    : "<<setw(12)<<fEventVertex.X()   <<", "<<setw(12)<<fEventVertex.Y()<<", "<<setw(12)<<fEventVertex.Z()<<endl;
	cout<<"decay   vertex    : "<<setw(12)<<fDecayVertex.X()   <<", "<<setw(12)<<fDecayVertex.Y()<<", "<<setw(12)<<fDecayVertex.Z()<<endl;
	cout<<"vermindist cand1  : "<<setw(12)<<fVerMinDistCand[0] <<", vermindist cand2 : "<<setw(12)<<fVerMinDistCand[1]<<", mindist candidates : "<<setw(12)<<fMinDistCandidates<<endl;
	cout<<"vermindist mother : "<<setw(12)<<fVerMinDistMother  <<", verdist mother   : "<<setw(12)<<fVerDistMother<<endl;
    }
}
void  HParticleGeantPair::clear()
{
    
    fcand[0]         = 0;
    fcand[1]         = 0;
    fmother          = 0;
    fPID[0]          = -10;
    fPID[1]          = -10;
    fMotherPID       = -10;
    foAngle          = -1;
    fstatusFlags     = 0;
    fVerMinDistCand[0]   = 0.;
    fVerMinDistCand[1]   = 0.;
    fVerMinDistMother    = 0.;
    fVerDistMother       = 0.;
    fMinDistCandidates   = 0.;
}
 hparticlegeantpair.cc:100  hparticlegeantpair.cc:101  hparticlegeantpair.cc:102  hparticlegeantpair.cc:103  hparticlegeantpair.cc:104  hparticlegeantpair.cc:105  hparticlegeantpair.cc:106  hparticlegeantpair.cc:107  hparticlegeantpair.cc:108  hparticlegeantpair.cc:109  hparticlegeantpair.cc:110  hparticlegeantpair.cc:111  hparticlegeantpair.cc:112  hparticlegeantpair.cc:113  hparticlegeantpair.cc:114  hparticlegeantpair.cc:115  hparticlegeantpair.cc:116  hparticlegeantpair.cc:117  hparticlegeantpair.cc:118  hparticlegeantpair.cc:119  hparticlegeantpair.cc:120  hparticlegeantpair.cc:121  hparticlegeantpair.cc:122  hparticlegeantpair.cc:123  hparticlegeantpair.cc:124  hparticlegeantpair.cc:125  hparticlegeantpair.cc:126  hparticlegeantpair.cc:127  hparticlegeantpair.cc:128  hparticlegeantpair.cc:129  hparticlegeantpair.cc:130  hparticlegeantpair.cc:131  hparticlegeantpair.cc:132  hparticlegeantpair.cc:133  hparticlegeantpair.cc:134  hparticlegeantpair.cc:135  hparticlegeantpair.cc:136  hparticlegeantpair.cc:137  hparticlegeantpair.cc:138  hparticlegeantpair.cc:139  hparticlegeantpair.cc:140  hparticlegeantpair.cc:141  hparticlegeantpair.cc:142  hparticlegeantpair.cc:143  hparticlegeantpair.cc:144  hparticlegeantpair.cc:145  hparticlegeantpair.cc:146  hparticlegeantpair.cc:147  hparticlegeantpair.cc:148  hparticlegeantpair.cc:149  hparticlegeantpair.cc:150  hparticlegeantpair.cc:151  hparticlegeantpair.cc:152  hparticlegeantpair.cc:153  hparticlegeantpair.cc:154  hparticlegeantpair.cc:155  hparticlegeantpair.cc:156  hparticlegeantpair.cc:157  hparticlegeantpair.cc:158  hparticlegeantpair.cc:159  hparticlegeantpair.cc:160  hparticlegeantpair.cc:161  hparticlegeantpair.cc:162  hparticlegeantpair.cc:163  hparticlegeantpair.cc:164  hparticlegeantpair.cc:165  hparticlegeantpair.cc:166  hparticlegeantpair.cc:167  hparticlegeantpair.cc:168  hparticlegeantpair.cc:169  hparticlegeantpair.cc:170  hparticlegeantpair.cc:171  hparticlegeantpair.cc:172  hparticlegeantpair.cc:173  hparticlegeantpair.cc:174  hparticlegeantpair.cc:175  hparticlegeantpair.cc:176  hparticlegeantpair.cc:177  hparticlegeantpair.cc:178  hparticlegeantpair.cc:179  hparticlegeantpair.cc:180  hparticlegeantpair.cc:181  hparticlegeantpair.cc:182  hparticlegeantpair.cc:183  hparticlegeantpair.cc:184  hparticlegeantpair.cc:185  hparticlegeantpair.cc:186  hparticlegeantpair.cc:187  hparticlegeantpair.cc:188  hparticlegeantpair.cc:189  hparticlegeantpair.cc:190  hparticlegeantpair.cc:191  hparticlegeantpair.cc:192  hparticlegeantpair.cc:193  hparticlegeantpair.cc:194  hparticlegeantpair.cc:195  hparticlegeantpair.cc:196  hparticlegeantpair.cc:197  hparticlegeantpair.cc:198  hparticlegeantpair.cc:199  hparticlegeantpair.cc:200  hparticlegeantpair.cc:201  hparticlegeantpair.cc:202  hparticlegeantpair.cc:203  hparticlegeantpair.cc:204  hparticlegeantpair.cc:205  hparticlegeantpair.cc:206  hparticlegeantpair.cc:207  hparticlegeantpair.cc:208  hparticlegeantpair.cc:209  hparticlegeantpair.cc:210  hparticlegeantpair.cc:211  hparticlegeantpair.cc:212  hparticlegeantpair.cc:213  hparticlegeantpair.cc:214  hparticlegeantpair.cc:215  hparticlegeantpair.cc:216  hparticlegeantpair.cc:217  hparticlegeantpair.cc:218  hparticlegeantpair.cc:219  hparticlegeantpair.cc:220  hparticlegeantpair.cc:221  hparticlegeantpair.cc:222  hparticlegeantpair.cc:223  hparticlegeantpair.cc:224  hparticlegeantpair.cc:225  hparticlegeantpair.cc:226  hparticlegeantpair.cc:227  hparticlegeantpair.cc:228  hparticlegeantpair.cc:229  hparticlegeantpair.cc:230  hparticlegeantpair.cc:231  hparticlegeantpair.cc:232  hparticlegeantpair.cc:233  hparticlegeantpair.cc:234  hparticlegeantpair.cc:235  hparticlegeantpair.cc:236  hparticlegeantpair.cc:237  hparticlegeantpair.cc:238  hparticlegeantpair.cc:239  hparticlegeantpair.cc:240  hparticlegeantpair.cc:241  hparticlegeantpair.cc:242  hparticlegeantpair.cc:243  hparticlegeantpair.cc:244