#include "hparticlecandfillerpar.h"
#include "hpario.h"
#include "hparamlist.h"
#include "hhistconverter.h"
#include "TMath.h"
#include <iostream>
using namespace std;
ClassImp(HParticleCandFillerPar)
HParticleCandFillerPar::HParticleCandFillerPar(const Char_t* name,const Char_t* title,
					       const Char_t* context)
    : HParCond(name,title,context)
{
    
    clear();
}
HParticleCandFillerPar::~HParticleCandFillerPar()
{
  
    removeHists();
}
void HParticleCandFillerPar::removeHists(){
    for(Int_t s = 0; s < 6; s ++){
	if(hphiLow  [s]) delete hphiLow  [s];
        if(hphiUp   [s]) delete hphiUp   [s];
	if(hthetaLow[s]) delete hthetaLow[s];
	if(hthetaUp [s]) delete hthetaUp [s];
	hphiLow  [s] = 0;
	hphiUp   [s] = 0;
	hthetaLow[s] = 0;
	hthetaUp [s] = 0;
    }
}
void HParticleCandFillerPar::createHists(){
    for(Int_t s = 0; s < 6; s ++){
        hphiLow  [s] = (TH1D*)HHistConverter::createHist(Form("hphiLowS%i"  ,s),phiLow  [s]);
	hphiUp   [s] = (TH1D*)HHistConverter::createHist(Form("hphiUpS%i"   ,s),phiUp   [s]);
        hthetaLow[s] = (TH1D*)HHistConverter::createHist(Form("hthetaLowS%i",s),thetaLow[s]);
	hthetaUp [s] = (TH1D*)HHistConverter::createHist(Form("hthetaUpS%i" ,s),thetaUp [s]);
    }
}
void HParticleCandFillerPar::clear()
{
    for(Int_t s = 0; s < 6; s ++){
	hphiLow  [s] = 0;
	hphiUp   [s] = 0;
	hthetaLow[s] = 0;
	hthetaUp [s] = 0;
    }
    status=kFALSE;
    resetInputVersions();
    changed=kFALSE;
}
void HParticleCandFillerPar::printParam(void)
{
    cout<<"############################################################"<<endl;
    cout<<"HParticleCandFillerPar:"<<endl;
    for(Int_t s = 0; s < 6; s ++){
         HHistConverter::printArrayInfo(phiLow[s],Form("phiLowS%i",s),10,0);
    }
    for(Int_t s = 0; s < 6; s ++){
         HHistConverter::printArrayInfo(phiUp[s],Form("phiUpS%i",s),10,0);
    }
    for(Int_t s = 0; s < 6; s ++){
         HHistConverter::printArrayInfo(thetaLow[s],Form("thetaLowS%i",s),10,0);
    }
    for(Int_t s = 0; s < 6; s ++){
         HHistConverter::printArrayInfo(thetaUp[s],Form("thetaUpS%i",s),10,0);
    }
    cout<<"zRichCenter = "<<zRichCenter<<endl;
    cout<<"############################################################"<<endl;
}
Bool_t HParticleCandFillerPar::init(HParIo* inp,Int_t* set)
{
    
  Bool_t rc = HParCond::init(inp,set);
  if (rc && changed){
      
      removeHists();
      createHists();
  }
  return rc;
}
void HParticleCandFillerPar::putParams(HParamList* l)
{
    
    
    if (!l) return;
    for(Int_t s = 0; s < 6; s ++){
	l->add(Form("phiLowS%i"  ,s), phiLow[s]);
    }
    for(Int_t s = 0; s < 6; s ++){
	l->add(Form("phiUpS%i"   ,s), phiUp[s]);
    }
    for(Int_t s = 0; s < 6; s ++){
	l->add(Form("thetaLowS%i",s), thetaLow[s]);
    }
    for(Int_t s = 0; s < 6; s ++){
	l->add(Form("thetaUpS%i" ,s), thetaUp[s]);
    }
    l->add("zRichCenter",zRichCenter);
}
Bool_t HParticleCandFillerPar::getParams(HParamList* l)
{
    if (!l) return kFALSE;
    for(Int_t s = 0; s < 6; s ++){
	if(! (l->fill(Form("phiLowS%i"  ,s), &phiLow[s])   )) return kFALSE;
    }
    for(Int_t s = 0; s < 6; s ++){
	if(! (l->fill(Form("phiUpS%i"   ,s), &phiUp[s])    )) return kFALSE;
    }
    for(Int_t s = 0; s < 6; s ++){
	if(! (l->fill(Form("thetaLowS%i",s), &thetaLow[s]) )) return kFALSE;
    }
    for(Int_t s = 0; s < 6; s ++){
	if(! (l->fill(Form("thetaUpS%i" ,s), &thetaUp[s])  )) return kFALSE;
    }
    if(! l->fill("zRichCenter", &zRichCenter)) return kFALSE;
    return kTRUE;
}
void HParticleCandFillerPar::setPhiLow  (Int_t s, const TArrayD& linData) {
    if(s >= 0 && s < 6) {
	phiLow  [s].Set(linData.GetSize(),linData.GetArray());
	if(hphiLow[s]) delete hphiLow[s];
        hphiLow[s] = (TH1D*) HHistConverter::createHist(Form("hphiLowS%i",s),phiLow[s]);
    }
}
void HParticleCandFillerPar::setPhiUp   (Int_t s, const TArrayD& linData) {
    if(s >= 0 && s < 6) {
	phiUp   [s].Set(linData.GetSize(),linData.GetArray());
	if(hphiUp[s]) delete hphiUp[s];
        hphiUp[s] = (TH1D*) HHistConverter::createHist(Form("hphiUpS%i",s),phiUp[s]);
    }
}
void HParticleCandFillerPar::setThetaLow(Int_t s, const TArrayD& linData) {
    if(s >= 0 && s < 6) {
	thetaLow[s].Set(linData.GetSize(),linData.GetArray());
	if(hthetaLow[s]) delete hthetaLow[s];
        hthetaLow[s] = (TH1D*) HHistConverter::createHist(Form("hthetaLowS%i",s),thetaLow[s]);
    }
}
void HParticleCandFillerPar::setThetaUp (Int_t s, const TArrayD& linData) {
    if(s >= 0 && s < 6) {
	thetaUp [s].Set(linData.GetSize(),linData.GetArray());
	if(hthetaUp[s]) delete hthetaUp[s];
        hthetaUp[s] = (TH1D*) HHistConverter::createHist(Form("hthetaUpS%i",s),thetaUp[s]);
    }
}
Float_t HParticleCandFillerPar::getRichCorr(Float_t zVertex, Float_t thetaRich, Float_t phiRich){
    
    const static Float_t dzTarg = 5.0; 
    const static Float_t zRef = zRichCenter + dzTarg;
    const static Float_t zNorm = 22.5F; 
    const static Float_t thetaRef = 50.0F; 
    const static Int_t zParMax=6;
    const static Float_t dzThetaPar[zParMax] =
    {-0.820714F,-0.021845F,0.00050965F,2.57468e-5F,1.19138e-7F,-5.83638e-9F};
    const static Float_t dz2ThetaPar[zParMax] =
    {-0.0662483F,-0.00375807F,-9.81675e-6F,3.5676e-6F,1.2312e-8F,-1.99097e-9F};
    
    const static Float_t dzThetaPhiPar[2] = {0.0408321F/256.0F, 0.00150907F/256.0F};
    const static Float_t dz2ThetaPhiPar[2] = {0.00107495F/256.0F, 9.0156e-5F/256.0F};
    Float_t dz = (zVertex - zRef)/zNorm;;
    Float_t dz2 = dz*dz;
    Float_t theta0 = thetaRich - thetaRef;
    Float_t dTheta = dzThetaPar[0]*dz + dz2ThetaPar[0]*dz2;
    Float_t thetaPower = 1.0F;
    for (Int_t n=1; n<zParMax; ++n) {
        thetaPower *= theta0;
	dTheta += dzThetaPar[n]*thetaPower*dz + dz2ThetaPar[n]*thetaPower*dz2;
    }
    
    Float_t phi0 = fmod(phiRich,60.0F) - 30.0F;
    dTheta += ((dzThetaPhiPar[0] + dzThetaPhiPar[1]*theta0)*dz +
	       (dz2ThetaPhiPar[0] + dz2ThetaPhiPar[1]*theta0)*dz2)*phi0*phi0;
    return dTheta;
}
Bool_t  HParticleCandFillerPar::acceptPhiTheta(Int_t s,Float_t mom,Float_t dphi,Float_t dtheta){
    
    
    
    
    if(dtheta < HParticleTool::getInterpolatedValue(hthetaUp [s],mom,kFALSE) &&
       dtheta > HParticleTool::getInterpolatedValue(hthetaLow[s],mom,kFALSE) &&
       dphi   < HParticleTool::getInterpolatedValue(hphiUp   [s],mom,kFALSE) &&
       dphi   > HParticleTool::getInterpolatedValue(hphiLow  [s],mom,kFALSE)
      ) return kTRUE;
    return kFALSE;
}
 hparticlecandfillerpar.cc:1  hparticlecandfillerpar.cc:2  hparticlecandfillerpar.cc:3  hparticlecandfillerpar.cc:4  hparticlecandfillerpar.cc:5  hparticlecandfillerpar.cc:6  hparticlecandfillerpar.cc:7  hparticlecandfillerpar.cc:8  hparticlecandfillerpar.cc:9  hparticlecandfillerpar.cc:10  hparticlecandfillerpar.cc:11  hparticlecandfillerpar.cc:12  hparticlecandfillerpar.cc:13  hparticlecandfillerpar.cc:14  hparticlecandfillerpar.cc:15  hparticlecandfillerpar.cc:16  hparticlecandfillerpar.cc:17  hparticlecandfillerpar.cc:18  hparticlecandfillerpar.cc:19  hparticlecandfillerpar.cc:20  hparticlecandfillerpar.cc:21  hparticlecandfillerpar.cc:22  hparticlecandfillerpar.cc:23  hparticlecandfillerpar.cc:24  hparticlecandfillerpar.cc:25  hparticlecandfillerpar.cc:26  hparticlecandfillerpar.cc:27  hparticlecandfillerpar.cc:28  hparticlecandfillerpar.cc:29  hparticlecandfillerpar.cc:30  hparticlecandfillerpar.cc:31  hparticlecandfillerpar.cc:32  hparticlecandfillerpar.cc:33  hparticlecandfillerpar.cc:34  hparticlecandfillerpar.cc:35  hparticlecandfillerpar.cc:36  hparticlecandfillerpar.cc:37  hparticlecandfillerpar.cc:38  hparticlecandfillerpar.cc:39  hparticlecandfillerpar.cc:40  hparticlecandfillerpar.cc:41  hparticlecandfillerpar.cc:42  hparticlecandfillerpar.cc:43  hparticlecandfillerpar.cc:44  hparticlecandfillerpar.cc:45  hparticlecandfillerpar.cc:46  hparticlecandfillerpar.cc:47  hparticlecandfillerpar.cc:48  hparticlecandfillerpar.cc:49  hparticlecandfillerpar.cc:50  hparticlecandfillerpar.cc:51  hparticlecandfillerpar.cc:52  hparticlecandfillerpar.cc:53  hparticlecandfillerpar.cc:54  hparticlecandfillerpar.cc:55  hparticlecandfillerpar.cc:56  hparticlecandfillerpar.cc:57  hparticlecandfillerpar.cc:58  hparticlecandfillerpar.cc:59  hparticlecandfillerpar.cc:60  hparticlecandfillerpar.cc:61  hparticlecandfillerpar.cc:62  hparticlecandfillerpar.cc:63  hparticlecandfillerpar.cc:64  hparticlecandfillerpar.cc:65  hparticlecandfillerpar.cc:66  hparticlecandfillerpar.cc:67  hparticlecandfillerpar.cc:68  hparticlecandfillerpar.cc:69  hparticlecandfillerpar.cc:70  hparticlecandfillerpar.cc:71  hparticlecandfillerpar.cc:72  hparticlecandfillerpar.cc:73  hparticlecandfillerpar.cc:74  hparticlecandfillerpar.cc:75  hparticlecandfillerpar.cc:76  hparticlecandfillerpar.cc:77  hparticlecandfillerpar.cc:78  hparticlecandfillerpar.cc:79  hparticlecandfillerpar.cc:80  hparticlecandfillerpar.cc:81  hparticlecandfillerpar.cc:82  hparticlecandfillerpar.cc:83  hparticlecandfillerpar.cc:84  hparticlecandfillerpar.cc:85  hparticlecandfillerpar.cc:86  hparticlecandfillerpar.cc:87  hparticlecandfillerpar.cc:88  hparticlecandfillerpar.cc:89  hparticlecandfillerpar.cc:90  hparticlecandfillerpar.cc:91  hparticlecandfillerpar.cc:92  hparticlecandfillerpar.cc:93  hparticlecandfillerpar.cc:94  hparticlecandfillerpar.cc:95  hparticlecandfillerpar.cc:96  hparticlecandfillerpar.cc:97  hparticlecandfillerpar.cc:98  hparticlecandfillerpar.cc:99  hparticlecandfillerpar.cc:100  hparticlecandfillerpar.cc:101  hparticlecandfillerpar.cc:102  hparticlecandfillerpar.cc:103  hparticlecandfillerpar.cc:104  hparticlecandfillerpar.cc:105  hparticlecandfillerpar.cc:106  hparticlecandfillerpar.cc:107  hparticlecandfillerpar.cc:108  hparticlecandfillerpar.cc:109  hparticlecandfillerpar.cc:110  hparticlecandfillerpar.cc:111  hparticlecandfillerpar.cc:112  hparticlecandfillerpar.cc:113  hparticlecandfillerpar.cc:114  hparticlecandfillerpar.cc:115  hparticlecandfillerpar.cc:116  hparticlecandfillerpar.cc:117  hparticlecandfillerpar.cc:118  hparticlecandfillerpar.cc:119  hparticlecandfillerpar.cc:120  hparticlecandfillerpar.cc:121  hparticlecandfillerpar.cc:122  hparticlecandfillerpar.cc:123  hparticlecandfillerpar.cc:124  hparticlecandfillerpar.cc:125  hparticlecandfillerpar.cc:126  hparticlecandfillerpar.cc:127  hparticlecandfillerpar.cc:128  hparticlecandfillerpar.cc:129  hparticlecandfillerpar.cc:130  hparticlecandfillerpar.cc:131  hparticlecandfillerpar.cc:132  hparticlecandfillerpar.cc:133  hparticlecandfillerpar.cc:134  hparticlecandfillerpar.cc:135  hparticlecandfillerpar.cc:136  hparticlecandfillerpar.cc:137  hparticlecandfillerpar.cc:138  hparticlecandfillerpar.cc:139  hparticlecandfillerpar.cc:140  hparticlecandfillerpar.cc:141  hparticlecandfillerpar.cc:142  hparticlecandfillerpar.cc:143  hparticlecandfillerpar.cc:144  hparticlecandfillerpar.cc:145  hparticlecandfillerpar.cc:146  hparticlecandfillerpar.cc:147  hparticlecandfillerpar.cc:148  hparticlecandfillerpar.cc:149  hparticlecandfillerpar.cc:150  hparticlecandfillerpar.cc:151  hparticlecandfillerpar.cc:152  hparticlecandfillerpar.cc:153  hparticlecandfillerpar.cc:154  hparticlecandfillerpar.cc:155  hparticlecandfillerpar.cc:156  hparticlecandfillerpar.cc:157  hparticlecandfillerpar.cc:158  hparticlecandfillerpar.cc:159  hparticlecandfillerpar.cc:160  hparticlecandfillerpar.cc:161  hparticlecandfillerpar.cc:162  hparticlecandfillerpar.cc:163  hparticlecandfillerpar.cc:164  hparticlecandfillerpar.cc:165  hparticlecandfillerpar.cc:166  hparticlecandfillerpar.cc:167  hparticlecandfillerpar.cc:168  hparticlecandfillerpar.cc:169  hparticlecandfillerpar.cc:170  hparticlecandfillerpar.cc:171  hparticlecandfillerpar.cc:172  hparticlecandfillerpar.cc:173  hparticlecandfillerpar.cc:174  hparticlecandfillerpar.cc:175  hparticlecandfillerpar.cc:176  hparticlecandfillerpar.cc:177  hparticlecandfillerpar.cc:178  hparticlecandfillerpar.cc:179  hparticlecandfillerpar.cc:180  hparticlecandfillerpar.cc:181  hparticlecandfillerpar.cc:182  hparticlecandfillerpar.cc:183  hparticlecandfillerpar.cc:184  hparticlecandfillerpar.cc:185  hparticlecandfillerpar.cc:186  hparticlecandfillerpar.cc:187  hparticlecandfillerpar.cc:188  hparticlecandfillerpar.cc:189  hparticlecandfillerpar.cc:190  hparticlecandfillerpar.cc:191  hparticlecandfillerpar.cc:192  hparticlecandfillerpar.cc:193  hparticlecandfillerpar.cc:194  hparticlecandfillerpar.cc:195  hparticlecandfillerpar.cc:196  hparticlecandfillerpar.cc:197  hparticlecandfillerpar.cc:198  hparticlecandfillerpar.cc:199  hparticlecandfillerpar.cc:200  hparticlecandfillerpar.cc:201  hparticlecandfillerpar.cc:202  hparticlecandfillerpar.cc:203  hparticlecandfillerpar.cc:204  hparticlecandfillerpar.cc:205  hparticlecandfillerpar.cc:206  hparticlecandfillerpar.cc:207  hparticlecandfillerpar.cc:208  hparticlecandfillerpar.cc:209  hparticlecandfillerpar.cc:210  hparticlecandfillerpar.cc:211  hparticlecandfillerpar.cc:212  hparticlecandfillerpar.cc:213  hparticlecandfillerpar.cc:214  hparticlecandfillerpar.cc:215  hparticlecandfillerpar.cc:216  hparticlecandfillerpar.cc:217  hparticlecandfillerpar.cc:218  hparticlecandfillerpar.cc:219  hparticlecandfillerpar.cc:220  hparticlecandfillerpar.cc:221  hparticlecandfillerpar.cc:222  hparticlecandfillerpar.cc:223  hparticlecandfillerpar.cc:224  hparticlecandfillerpar.cc:225  hparticlecandfillerpar.cc:226  hparticlecandfillerpar.cc:227  hparticlecandfillerpar.cc:228  hparticlecandfillerpar.cc:229  hparticlecandfillerpar.cc:230  hparticlecandfillerpar.cc:231  hparticlecandfillerpar.cc:232  hparticlecandfillerpar.cc:233  hparticlecandfillerpar.cc:234  hparticlecandfillerpar.cc:235  hparticlecandfillerpar.cc:236  hparticlecandfillerpar.cc:237  hparticlecandfillerpar.cc:238  hparticlecandfillerpar.cc:239  hparticlecandfillerpar.cc:240  hparticlecandfillerpar.cc:241  hparticlecandfillerpar.cc:242  hparticlecandfillerpar.cc:243  hparticlecandfillerpar.cc:244  hparticlecandfillerpar.cc:245  hparticlecandfillerpar.cc:246  hparticlecandfillerpar.cc:247  hparticlecandfillerpar.cc:248  hparticlecandfillerpar.cc:249  hparticlecandfillerpar.cc:250  hparticlecandfillerpar.cc:251  hparticlecandfillerpar.cc:252  hparticlecandfillerpar.cc:253  hparticlecandfillerpar.cc:254  hparticlecandfillerpar.cc:255  hparticlecandfillerpar.cc:256  hparticlecandfillerpar.cc:257  hparticlecandfillerpar.cc:258  hparticlecandfillerpar.cc:259  hparticlecandfillerpar.cc:260  hparticlecandfillerpar.cc:261  hparticlecandfillerpar.cc:262  hparticlecandfillerpar.cc:263