#include "StHbtMaker/Infrastructure/StHbtTrack.hh"
#include "StHbtMaker/Infrastructure/StHbtTypes.hh"
#include "StHbtMaker/Cut/trackCutMonitor_TpcNSigma.h"

#ifdef __ROOT__
#include <TString.h>
ClassImp(trackCutMonitor_TpcNSigma)
#endif

//_________________
trackCutMonitor_TpcNSigma::trackCutMonitor_TpcNSigma() {
  
  mCharge = 0;
  mPartType = 0;
  hNSigmaElectron = new TH1F(Form("trackCutMonitor_nSigmaElectron"),
			     Form("nSigmaElectron; n#sigma(e); dN/dn#sigma(e)"),
			     40,-10.,10.);
  hNSigmaPion = new TH1F(Form("trackCutMonitor_nSigmaPion"),
			 Form("nSigmaPion; n#sigma(#pi); dN/dn#sigma(e)"),
			 40,-10.,10.);
  hNSigmaKaon = new TH1F(Form("trackCutMonitor_nSigmaKaon"),
			 Form("nSigmaKaon; n#sigma(K); dN/dn#sigma(e)"),
			 40,-10.,10.);
  hNSigmaProton = new TH1F(Form("trackCutMonitor_nSigmaProton"),
			   Form("nSigmaProton; n#sigma(p); dN/dn#sigma(e)"),
			   40, -10., 10.);
}
  
//_________________
trackCutMonitor_TpcNSigma::trackCutMonitor_TpcNSigma(Int_t Charge, 
						     Int_t PartType) {
  if(Charge<-1. || Charge>1) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle charge: " << Charge
	      << " Set it to 0 (all charges)" << std::endl;
    Charge = 0;
  }
  mCharge = Charge;
  if(PartType<0 || PartType>4) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle type: " << PartType
	      << " Set it to 0 (all hypotheses)" << std::endl;
    PartType = 0;
  }
  mPartType = PartType; //0-all, 1-elec, 2-pion, 3-kaon, 4-proton
  
  switch(mPartType) {
  case 0:
    hNSigmaElectron = new TH1F(Form("trackCutMonitor_nSigmaElectron"),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    hNSigmaPion = new TH1F(Form("trackCutMonitor_nSigmaPion"),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaKaon = new TH1F(Form("trackCutMonitor_nSigmaKaon"),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaProton = new TH1F(Form("trackCutMonitor_nSigmaProton"),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);
    break;
  case 1:
    hNSigmaElectron = new TH1F(Form("trackCutMonitor_nSigmaElectron"),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    break;
  case 2:
    hNSigmaPion = new TH1F(Form("trackCutMonitor_nSigmaPion"),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    break;
  case 3:
    hNSigmaKaon = new TH1F(Form("trackCutMonitor_nSigmaKaon"),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    break;
  case 4:
    hNSigmaProton = new TH1F(Form("trackCutMonitor_nSigmaProton"),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);
    break;
  default:
    hNSigmaElectron = new TH1F(Form("trackCutMonitor_nSigmaElectron"),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    hNSigmaPion = new TH1F(Form("trackCutMonitor_nSigmaPion"),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaKaon = new TH1F(Form("trackCutMonitor_nSigmaKaon"),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaProton = new TH1F(Form("trackCutMonitor_nSigmaProton"),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);    
  };
}

//_________________
trackCutMonitor_TpcNSigma::trackCutMonitor_TpcNSigma(Int_t Charge, Int_t PartType, 
						     const Char_t* TitCutMoni) {
  if(Charge<-1. || Charge>1) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle charge: " << Charge
	      << " Set it to 0 (all charges)" << std::endl;
    Charge = 0;
  }
  mCharge = Charge;
  if(PartType<0 || PartType>4) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle type: " << PartType
	      << " Set it to 0 (all hypotheses)" << std::endl;
    PartType = 0;
  }
  mPartType = PartType; //0-all, 1-elec, 2-pion, 3-kaon, 4-proton

  TString title(TitCutMoni);

  switch(mPartType) {
  case 0:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);
    break;
  case 1:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    break;
  case 2:
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    break;
  case 3:
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    break;
  case 4:
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);
    break;
  default:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       40,-10.,10.);
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   40,-10.,10.);
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     40,-10.,10.);
  };
}

//_________________
trackCutMonitor_TpcNSigma::trackCutMonitor_TpcNSigma(Int_t Charge, Int_t PartType,
						     const Char_t* TitCutMoni,
						     Int_t nBinsNSigma, 
						     Double_t NSigmaLow, 
						     Double_t NSigmaHi) {
  if(Charge<-1. || Charge>1) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle charge: " << Charge
	      << " Set it to 0 (all charges)" << std::endl;
    Charge = 0;
  }
  mCharge = Charge;
  if(PartType<0 || PartType>4) {
    std::cout << "trackCutMonitor_TpcNSigma: Wrong particle type: " << PartType
	      << " Set it to 0 (all hypotheses)" << std::endl;
    PartType = 0;
  }
  mPartType = PartType; //0-all, 1-elec, 2-pion, 3-kaon, 4-proton

  TString title(TitCutMoni);
  Int_t mNBinsX = nBinsNSigma;
  Double_t mBinXLo = NSigmaLow;
  Double_t mBinXHi = NSigmaHi;

  switch(mPartType) {
  case 0:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       mNBinsX, mBinXLo, mBinXHi);
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     mNBinsX, mBinXLo, mBinXHi);
    break;
  case 1:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       mNBinsX, mBinXLo, mBinXHi);
    break;
  case 2:
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    break;
  case 3:
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    break;
  case 4:
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     mNBinsX, mBinXLo, mBinXHi);
    break;
  default:
    hNSigmaElectron = new TH1F(title.Data(),
			       Form("nSigmaElectron charge=%2d; n#sigma(e); dN/dn#sigma(e)", mCharge),
			       mNBinsX, mBinXLo, mBinXHi);
    hNSigmaPion = new TH1F(title.Data(),
			   Form("nSigmaPion charge=%2d; n#sigma(#pi); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    hNSigmaKaon = new TH1F(title.Data(),
			   Form("nSigmaKaon charge=%2d; n#sigma(K); dN/dn#sigma(e)", mCharge),
			   mNBinsX, mBinXLo, mBinXHi);
    hNSigmaProton = new TH1F(title.Data(),
			     Form("nSigmaProton charge=%2d; n#sigma(p); dN/dn#sigma(e)", mCharge),
			     mNBinsX, mBinXLo, mBinXHi);
  };
}

//_________________
trackCutMonitor_TpcNSigma::~trackCutMonitor_TpcNSigma() {

  if(hNSigmaElectron) delete hNSigmaElectron;
  if(hNSigmaPion) delete hNSigmaPion;
  if(hNSigmaKaon) delete hNSigmaKaon;
  if(hNSigmaProton) delete hNSigmaProton;
}

//_________________
void trackCutMonitor_TpcNSigma::Fill(const StHbtTrack *track) {
  
  if(track->Charge() == mCharge || mCharge==0) {
    switch(mPartType) {
    case 0:
      hNSigmaElectron->Fill(track->NSigmaElectron());
      hNSigmaPion->Fill(track->NSigmaPion());
      hNSigmaKaon->Fill(track->NSigmaKaon());
      hNSigmaProton->Fill(track->NSigmaProton());
      break;
    case 1:
      hNSigmaElectron->Fill(track->NSigmaElectron());
      break;
    case 2:
      hNSigmaPion->Fill(track->NSigmaPion());
      break;
    case 3:
      hNSigmaKaon->Fill(track->NSigmaKaon());
      break;
    case 4:
      hNSigmaProton->Fill(track->NSigmaProton());
      break;
    default:
      hNSigmaElectron->Fill(track->NSigmaElectron());
      hNSigmaPion->Fill(track->NSigmaPion());
      hNSigmaKaon->Fill(track->NSigmaKaon());
      hNSigmaProton->Fill(track->NSigmaProton());      
    }; //switch(mPartType)
  } //if(track->Charge() == mCharge || mCharge==0)
}
