///////////////////////////////////////////////
//
//plot.C : this is a root macro with some
//         functions which plot
//         different outputs from gpanal.
//
//////////////////////////////////////////////

#include <stdlib>
#include <iostream>
#include "TString.h"

///////////////////////////////////////////////
//manyplot:  make a large canvas to plot 
//significant params of a single gp run
///////////////////////////////////////////////

void manyplot(TString fileName) {

 TCanvas *c = new TCanvas("c","c",900,600); c->Divide(3,3);

 c->cd(1);
 plot(fileName,"b1e","");
 // plot(fileName,"b2e","samee1");
 plot(fileName,"b2e","same");

 c->cd(2);
 plot(fileName,"pae","");

 c->cd(3);
 plot(fileName,"b1thx","");
 plot(fileName,"b2thx","same");

 c->cd(4);
 plot(fileName,"lpe","");
 plot(fileName,"lpp","same");

 c->cd(5);
 plot(fileName,"phe","");

 c->cd(6);
 plot(fileName,"b1thy","");
 plot(fileName,"b2thy","same");

 c->cd(7);
 plot(fileName,"b1x","");
 plot(fileName,"b2x","same");

 c->cd(8);
 plot(fileName,"b1y","");
 plot(fileName,"b2y","same");

 c->cd(9);
 plot(fileName,"lpx","");

 TString psname =  "../tmp-results/"+ fileName.Replace( fileName.Index(".root"), 1 , "-" )+ ".ps";
 c->Print(psname);
 //gSystem->Exec("lpr "+psname);
}

///////////////////////////////////////////////
//manyplot:  make a large canvas to plot 
//significant params of a single gp run
///////////////////////////////////////////////

void manyplotoverlay(TString nameChunk, TString newFileName) {

 TCanvas *c = new TCanvas("c","c",900,600); c->Divide(4,3);

 c->cd(1);
 gPad->SetLogy(kTRUE);
 overlay( nameChunk ,"b1e","../run");

 c->cd(2);
 // gPad->SetLogy(kTRUE);
 overlay( nameChunk ,"b1x","../run");

 c->cd(3);
 //gPad->SetLogy(kTRUE);
 overlay( nameChunk ,"b1y","../run");

 c->cd(4);
 //gPad->SetLogy(kTRUE);
 overlay( nameChunk ,"b1thx","../run");

 c->cd(5);
 //gPad->SetLogy(kTRUE);
 overlay(nameChunk,"b1thy","../run");

 c->cd(6);
 title = new TPaveLabel(0.1,0.94,0.9,0.98, newFileName+" parameterised plots of beam1");
 title->SetFillColor(16);
 title->Draw();



 TCanvas *c1 = new TCanvas("c1","c1",900,600); c1->Divide(4,3);


 c1->cd(1);
 gPad->SetLogy(kTRUE);
 overlay(nameChunk,"b2e","../run");

 c1->cd(2);
 overlay(nameChunk,"b2x","../run");

 c1->cd(3);
 overlay(nameChunk,"b2y","../run");

 c1->cd(4);
 overlay(nameChunk,"b2thx","../run");

 c1->cd(5);
 overlay(nameChunk,"b2thy","../run");

 c1->cd(6);
 title1 = new TPaveLabel(0.1,0.94,0.9,0.98,newFileName+" parameterised plots of beam2");
 title1->SetFillColor(16);
 title1->Draw();


 TString psname1 =  "../tmp-results/"+ newFileName+ "-parambeam1.ps";
 //c->Print(psname1);
 TString psname2 = "../tmp-results/"+ newFileName+ "-parambeam2.ps";
 c1->Print(psname2);

}



/////////////////////////////////////////////////////
//
// this will make many plots of hists with the same
// name contained in root files whose name matches
// a supplied substring.
//
////////////////////////////////////////////////////

void overlay( TString nameChunk ,TString histName ,TString dirName){

  //cout << nameChunk << " " << histName << " " << dirName << endl;
  leg = new TLegend(0.2,0.6,0.5,0.89);
  TSystemDirectory d1;
  d1.SetDirectory(dirName);

  l1 = d1.GetListOfFiles();

  Bool_t first = kTRUE;
  int imax = l1->LastIndex();

  cout << "nfiles= " << imax << endl;
  Int_t iColour = 2;

  for ( int i = 0; i<imax + 1; i++){
    //cout << "i= " << i << endl;
    f = l1.At(i);
    Char_t* fn = f->GetName();
    TString fname = TString(fn);

    if ( fname.Contains( nameChunk ) && fname.Contains(".root")){
      TFile *f2 = new  TFile(dirName+"/"+fname);

      if (first){
   //make a new histogram in the file  
   //it'll have the right size etc. because it's a copy
   TH1D *h1 = (TH1D*)f2->Get(histName);
   h1->SetLineColor(iColour); iColour++;
   //rebin
         h1->Rebin(8);

   // formatting
   h1->SetMaximum(h1->GetMaximum()*1.5);
   h1->SetStats(kFALSE);
   h1->GetXaxis()->SetTitle("E / E_{0}");
   h1->GetXaxis()->SetTitleOffset(0.65);
   h1->GetXaxis()->SetTitleSize(0.065);
   h1->GetYaxis()->SetTitle("Events");
   h1->GetYaxis()->SetTitleOffset(0.65);
   h1->GetYaxis()->SetTitleSize(0.065);
   // drawing
   h1->DrawCopy();
   first = kFALSE;
   leg->AddEntry(h1,"E_{0} = 175","l");

   delete h1;
      }
      else{
   //getting histogram to the plot
   TH1D  *h2 = (TH1D*)f2->Get(histName);

   // format
   h2->SetLineColor(iColour); iColour++;
   h2->Rebin(8);
   leg->AddEntry(h2,"E_{0} = 250","l");
   // drawing
   h2->SetStats(kFALSE);
   h2->DrawCopy("same");

   delete h2;
      }
      leg->SetFillColor(0);
      leg->SetBorderSize(0);
      leg->Draw();
    }
  }
}

void eplotlegend(){
  TCanvas *c = new TCanvas("c1","c1",900,600);
  gPad->SetLogy(kTRUE);
  overlay("noo","lpp","/home/fz/gpanal/run");

}



//////////////////////////////////////////////
// mostly sets plot options, then does a plot
//////////////////////////////////////////////


void plot(TString fileName, TString plotName, TString option) {

  TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(fileName.Data());
  if (!f)
    f = new TFile(fileName.Data());
  TH1D  *h = (TH1D*)f->Get(plotName)->Clone();
  h->Rebin(8);
  //use colors 28 (dark brown) amd 33 (light blueish gray)
  if(plotName == "pae") {
    gPad->SetLogy();
    h->SetLineColor(33);

  }

  if(plotName == "b1e") {
    gPad->SetLogy();
    h->SetLineColor(33);
    //h->SetFillColor(kBlue);

  }
  if(plotName == "b2e") {
    gPad->SetLogy();
    h->SetLineColor(28);
    //h->SetFillColor(kBlue);

  }

  if(plotName == "lpp") {
    gPad->SetLogy();

    h->SetLineColor(28);
    //h->SetFillColor(kBlue);

  }

  if(plotName == "lpe") {
    gPad->SetLogy();
    h->SetLineColor(33);
    //h->SetFillColor(kBlue);

  }
  if(plotName == "phe") {
    gPad->SetLogy();
    h->SetLineColor(33);
    //h->SetFillColor(kBlue);

  }

  if(plotName == "b1thx"){
    // gPad->setLogx();
    h->SetLineColor(33);

  }
  if(plotName == "b2thx"){
    // gPad->setLogx();
    h->SetLineColor(28);
  }


  if(plotName == "b1thy"){
    //gPad->setLogx();
    h->SetLineColor(33);
  }

  if(plotName == "b2thy"){
    // gPad->setLogx();
    h->SetLineColor(28);
  }

  if(plotName == "b1x"){
    // gPad->setLogx();
    h->SetLineColor(33);
  }

  if(plotName == "b2x"){
    // gPad->setLogx();
    h->SetLineColor(28);
  }

  if(plotName == "b1y"){
    // gPad->setLogx();
    h->SetLineColor(33);
  }

  if(plotName == "b2y"){
    // gPad->setLogx();
    h->SetLineColor(28);
  }

  if(plotName == "lpx"){
    // gPad->setLogx();
    h->SetLineColor(33);
  }

  if(plotName == "lpy"){
    // gPad->setLogx();
    h->SetLineColor(28);
  }


  h->DrawCopy(option.Data());

  delete h;
}