#include "pandaStyle.C"
// Parameters containers
TH2F* hRadLen;
TH2F* hMatLen;

Double_t momD[1], thetaD[10], phiD[10];
momD[0] = 0.25;

phiD[9]=314;
phiD[8]=319;
phiD[7]=324;
phiD[6]=329;
phiD[5]=334;
phiD[4]=339;
phiD[3]=344;
phiD[2]=349;
phiD[1]=354;
phiD[0]=359;

thetaD[0]=150;
thetaD[1]=151.5;
thetaD[2]=153;
thetaD[3]=154.5;
thetaD[4]=156;
thetaD[5]=157.5;
thetaD[6]=159;
thetaD[7]=160.5;
thetaD[8]=162;
thetaD[9]=163.5;

TString momS[1], thetaS[10], phiS[10];
momS[0] = "0.25";

phiS[0]="359";
phiS[1]="354";
phiS[2]="349";
phiS[3]="344";
phiS[4]="339";
phiS[5]="334";
phiS[6]="329";
phiS[7]="324";
phiS[8]="319";
phiS[9]="314";

thetaS[0]="150";
thetaS[1]="151.5";
thetaS[2]="153";
thetaS[3]="154.5";
thetaS[4]="156";
thetaS[5]="157.5";
thetaS[6]="159";
thetaS[7]="160.5";
thetaS[8]="162";
thetaS[9]="163.5";

TString run[1];
run[0] = "run0";

Int_t count=0;


void make_hist()
{
  cout << "Making histogramms ...";
  Int_t bin, min, max;

  hRadLen = new TH2F("X_{0}","radlen",10,149.25,164.25,10,311.5,361.5);
  hRadLen->GetXaxis()->SetTitle("#theta (arbitrary)");
  hRadLen->GetYaxis()->SetTitle("#phi (arbitrary)");
  hRadLen->GetZaxis()->SetTitle("X/X_{0}");
  hRadLen->SetMarkerSize(2);
  hRadLen->SetMarkerColor(kWhite);

  hMatLen = new TH2F("Material lenght","matlen",10,149.25,164.25,10,311.5,361.5);
  hMatLen->GetXaxis()->SetTitle("#theta (arbitrary)");
  hMatLen->GetYaxis()->SetTitle("#phi (arbitrary)");
  hMatLen->GetZaxis()->SetTitle("Material length [m]");
  hMatLen->SetMarkerSize(2);
  hMatLen->SetMarkerColor(kBlack);

  cout << " completed" << endl;
}

void data_analys(Int_t mo, Int_t ph, Int_t th)
{
  TString main = "/d/panda02/dkhaneft/";

  TString filefull = main  + "/run0" + "_mom" + momS[mo] + "_phi" + phiS[ph] + "_theta" + thetaS[th] + ".root";

  count++;
  cout << "***** " << count << " *****" << endl;

  TFile* f = new TFile(filefull);
  TTree *t = (TTree *) f->Get("cbmsim");

  TClonesArray* rad_array=new TClonesArray("FairRadLenPoint");
  t->SetBranchAddress("RadLen",&rad_array);

  double radlen=0., effradl=0., effradlsum=0.;
  TVector3 in, out, dist, distsum;

  for (Int_t event=0; event<1; event++)
  {
    t->GetEntry(event);

    for (Int_t k=0; k<rad_array->GetEntriesFast(); k++){
      FairRadLenPoint* radpoint = (FairRadLenPoint*)rad_array->At(k);

      radlen = radpoint->GetRadLength();
      in = radpoint->GetPosition();
      out = radpoint->GetPositionOut();
      dist = in - out;

      distsum += dist;
      //RadLen calculation
      effradl = dist.Mag()/radlen;
      effradlsum += effradl;
    }
    //Filling
    hRadLen->Fill(149.25 + 1.5*th,311.5 + 5*ph,effradlsum);
    hMatLen->Fill(149.25 + 1.5*th,311.5 + 5*ph,distsum.Mag()/1000.);
  }
}

void save_hist()
{
  cout << "Data saving ...";

  TCanvas *c1 = new TCanvas("","",1);
  c1->cd();

  BetterStatBox(gPad);
  set_margin(0.05, 0.12, 0.2, 0.16);
  gPad->SetLogz(0);

  TString outFile = "/u/dkhaneft/programs/results/radlen.root";
  TFile *out = TFile::Open(outFile,"RECREATE");
  out->cd();

  gStyle->SetPaintTextFormat("4.2f");
  hMatLen->Draw("coltextz");
  hMatLen->Write();
  c1->SaveAs("/u/dkhaneft/programs/results/matlen.eps");

  gStyle->SetPaintTextFormat("4.2f");
  hRadLen->Draw("coltextz");
  hRadLen->Write();
  c1->SaveAs("/u/dkhaneft/programs/results/radlen.eps");

  out->Close();

  cout << "SAVING completed!" << endl;
}

void radlen_analys()
{
  gROOT->LoadMacro("$VMCWORKDIR/gconfig/rootlogon.C");
  gROOT->LoadMacro("$VMCWORKDIR/gconfig/basiclibs.C");
  rootlogon();
  basiclibs();

  LoadPandaStyle();
  
  gStyle->SetPalette(1);
  gStyle->SetOptStat(0);
  gStyle->SetOptFit(0);
  gStyle->SetOptTitle(0);
  gStyle->SetFrameLineWidth(0);
  gStyle->SetFrameBorderMode(0);
  gStyle->SetFrameBorderSize(0);

  make_hist();

  for(int mo = 0; mo < 1; mo++){
    for(int ph = 0; ph < 10; ph++){
      for(int th = 0; th < 10; th++){
      	data_analys(mo, ph, th);
      }
    }
  }
  save_hist();
}
