//------------------------------------------------------------//
// Save the results from previous method
//------------------------------------------------------------//
void makeSemiFinalHists(TString base, char* interpretation)
{  

  cout<<endl;
  cout<<endl;
  cout<<endl;
  cout<<"Interpretation: "<<interpretation<<endl;
  cout<<endl;
  cout<<endl;
  cout<<endl;
  // Specify the save directory and make sure it exists
  TString append = "_Output_hypotest";
  TString outdir = "limitResults/" + base + "/";
  system(Form("mkdir -p %s",outdir.Data()));

  // Open up this summary file so we can read in all of the
  // necessary information
  gROOT->ProcessLine(".L summary_harvest_tree_description.h");

  TString inputFile = TString(Form("%s__1_harvest_list",(base+append).Data()));

  // Get the harvested tree
  TTree* tree = harvesttree( inputFile!=0 ? inputFile : 0 );
  if( tree == 0 ){
    cout<<"Cannot open the list file.  Leaving"<<endl;
    return;
  }

  // Create file for histograms to live in
  TFile* output = new TFile(outdir + base + "_histograms.root","recreate");
  output->cd();

      // Observed CLs
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("CLs:%s",interpretation), "hCLs" , "Observed CLs Median", "p1>0 && p1<=1" );

    if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 

    // Observed CLs - significance
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("StatTools::GetSigma( CLs ):%s",interpretation), "sigp1clsf" , "One-sided significalce of observed CLs", "p1>0 && p1<=1" );
    //TH2D* hist = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLs ):mN1:mC1", "sigp1clsf" , "One-sided significalce of observed CLs", "" );

    if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 

    // Expected CLs
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("CLsexp:%s",interpretation), "hCLsexp" , "Expected CLs Median", "p1>0 && p1<=1" );
    //TH2D* hist = DrawUtil::triwsmooth( tree, "CLsexp:mN1:mC1", "hCLsexp" , "Expected CLs Median", "" );

    if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 

    // Expected CLs - significance
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("StatTools::GetSigma( CLsexp ):%s",interpretation), "sigp1expclsf" , "One-sided significalce of expected CLs", "p1>0 && p1<=1" );
    //TH2D* hist = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLsexp ):mN1:mC1", "sigp1expclsf" , "One-sided significalce of expected CLs", "" );

    if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 

    // Expected CLs +1 sigma
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("clsu1s:%s",interpretation), "hCLsu1s" , "Expected CLs +1#sigma", "clsu1s>0" );
    //TH2D* hist = DrawUtil::triwsmooth( tree, "clsu1s:mN1:mC1", "hCLsu1s" , "Expected CLs +1#sigma", "" );

    if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 

    // Expected CLs +1 sigma significance
    TH2D* sigclsu1s = DrawUtil::triwsmooth(tree, Form("StatTools::GetSigma( clsu1s ):%s",interpretation), "sigclsu1s" , "One-sided significalce of expected CLs (+1 sigma)", "clsu1s>0" );
    //TH2D* sigclsu1s = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( clsu1s ):mN1:mC1", "sigclsu1s" , "One-sided significalce of expected CLs (+1 sigma)", "" );

    if (sigclsu1s!=0) 
    {
        sigclsu1s->Write();
        delete sigclsu1s; sigclsu1s=0;
    } 
    else oops(); 

    // Expected CLs -1 sigma
    TH2D* hist = DrawUtil::triwsmooth( tree, Form("clsd1s:%s",interpretation), "hCLsd1s" , "Expected CLs -1#sigma", "clsd1s>0" );
    //TH2D* hist = DrawUtil::triwsmooth( tree, "clsd1s:mN1:mC1", "hCLsd1s" , "Expected CLs -1#sigma", "" );
 
   if (hist!=0) 
    {
       hist->Write();
       delete hist; hist=0;
    } 
    else oops(); 
 
    // Expected CLs -1 sigma significance
    TH2D* sigclsd1s = DrawUtil::triwsmooth( tree, Form("StatTools::GetSigma( clsd1s ):%s",interpretation), "sigclsd1s" , "One-sided significalce of expected CLs (-1 sigma)", "clsd1s>0" );
    //TH2D* sigclsd1s = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( clsd1s ):mN1:mC1", "sigclsd1s" , "One-sided significalce of expected CLs (-1 sigma)", "" );

    if (sigclsd1s!=0) 
    {
        sigclsd1s->Write();
        delete sigclsd1s; sigclsd1s=0;
    } 
    else oops(); 

    // Save and quit
    output->Close();
  
    
    // clean up the working directory
    system(Form("mv summary* %s",outdir.Data()));
    system(Form("mv %s* %s",base.Data(),outdir.Data()));

}
void Show_SR(TString oredList,  TCanvas *can, float xlow, float xhigh, float ylow, float yhigh, bool useShape, TLegend *leg)
{
    Int_t c_myRed      = TColor::GetColor("#aa000");
    
    can->cd();

    TLatex lat;
    //lat.SetTextAlign( 11 );
    lat.SetTextSize( 0.0265 );
    lat.SetTextColor( 12 );
    lat.SetTextFont( 42 );

    cout << "Draw signal region labels." << endl;
    gROOT->ProcessLine(".L summary_harvest_tree_description.h+");
    gSystem->Load("libSusyFitter.so");

    TString txtfile=oredList;
    txtfile.ReplaceAll(".root","");

    TTree* tree = harvesttree( txtfile!=0 ? txtfile : 0 );
    if (tree==0) { 
        cout << "Cannot open list file. Exit." << endl;
        return;
    }

    Float_t fID;
    Float_t m0; 
    Float_t m12; 

    TBranch *b_m0;
    TBranch *b_m12;
    TBranch *b_fID;

    tree->SetBranchAddress("m0", &m0, &b_m0);
    tree->SetBranchAddress("m12", &m12, &b_m12);
    tree->SetBranchAddress("fID",  &fID,  &b_fID);

    bool drawMarker;

    for( Int_t i = 0; i < tree->GetEntries(); i++ ){
        drawMarker = false;
        
        tree->GetEntry( i );
        cout << m0 << " " << m12 << " " << fID << endl;
        
        TMarker marker;
        //marker.SetMarkerColor(4);
        marker.SetMarkerSize(2.5);
        marker.SetMarkerStyle(29);

        int _m0 = (int) m0;
        int _m12 = (int) m12;

        if(oredList.Contains("GG")){
            if( (_m0 == 700 && _m12 == 550) || (_m0 == 1162 && _m12 == 337) || (_m0 == 1250 && _m12 == 50) )
                drawMarker = true;
        } else if(oredList.Contains("SS")){
            if( (_m0 == 850 && _m12 == 100) || (_m0 == 450 && _m12 == 400))
                drawMarker = true;
        } else if(oredList.Contains("SG")){
            if( (_m0 == 1425 && _m12 == 525) || (_m0 == 1612 && _m12 == 37))
                drawMarker = true;
        }

        if (drawMarker)
            marker.DrawMarker(m0, m12);

        TString mySR = GetSRName(fID, useShape);
	lat.DrawLatex(m0,m12,mySR.Data());
	     
    }
    
    leg->Draw("same");
    // add up/down lines
    TLine *line1;
    TLine *line2;
    if (oredList.Contains("GG")) {
        line1 = new TLine( 972, 1412, 1062, 1412);
        line2 = new TLine( 972, 1355, 1062, 1355);
        cout << "GG line1" << endl;
        
    } else if (oredList.Contains("SS")) {
        line1 = new TLine( 793, 1128, 860, 1128); 
        line2 = new TLine( 793, 1081, 860, 1081); 
        cout << "SS line1" << endl;
    } else if (oredList.Contains("SG")) {
        line1 = new TLine( 1150, 1645, 1260, 1645); 
        line2 = new TLine( 1150, 1565, 1260, 1565); 
        cout << "SG line1" << endl;
    }
/*
    line1->SetLineWidth(2);
    line1->SetLineColor(c_myRed);
    line1->SetLineStyle(3);
    line1->Draw("SAME") ;

    line2->SetLineWidth(2);
    line2->SetLineColor(c_myRed);
    line2->SetLineStyle(3);
    line2->Draw("SAME") ;                
*/
}
const char*
m0_vs_m12_nofloat(const char* textfile = 0, TH2D* inputHist = 0, const char* rootfile = "m0m12_nofloat.root", TString id1="m0",TString id2="m12", int   nbinsX=21,int nbinsY=17, float minX=20,float maxX=860, float minY=92.5, float maxY=347.5)
{
   // set combination style and remove existing canvas'
   CombinationGlob::Initialize();

   initialize();

   // get the harvested tree
   TTree* tree = harvesttree( textfile!=0 ? textfile : 0 );
   if (tree==0) {
     cout << "Cannot open list file. Exit." << endl;
     return "";
   }

   // store histograms to output file
   const char* outfile(0);
   if (textfile!=0) {
     TObjArray* arr = TString(textfile).Tokenize("/");
     TObjString* objstring = (TObjString*)arr->At( arr->GetEntries()-1 );
     outfile = Form("%s%s",objstring->GetString().Data(),".root");
     delete arr;
   } else {
     outfile = rootfile;
   }

   cout << "Histograms being written to : " << outfile << endl;
   TFile* output = TFile::Open(outfile,"RECREATE");
   output->cd();

   TH2D* hist = nullptr;
   if (inputHist!=NULL){
     TH2D *clonehclPmin2=(TH2D*)inputHist->Clone();
     hist = DrawUtil::triwsmooth( tree, "p1:m12:m0", "hclPmin2" , "Observed CLsplusb", "p1>=0 && p1<=1", clonehclPmin2 );}
   else{
     hist = DrawUtil::triwsmooth( tree, "p1:m12:m0", "hclPmin2" , "Observed CLsplusb", "p1>=0 && p1<=1", inputHist);}


   if (hist!=0) {
     hist->Smooth();
     hist->Write();
     delete hist; hist=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }



   TH2D* sigp1 = nullptr;
   if (inputHist!=NULL){
     TH2D *clonesigp1=(TH2D*)inputHist->Clone();
     sigp1 = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(p1):m12:m0", "sigp1" , "One-sided significance of CLsplusb", "(p1>0 && p1<=1)", clonesigp1 );}
   else{
     sigp1 = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(p1):m12:m0", "sigp1" , "One-sided significance of CLsplusb", "(p1>0 && p1<=1)", inputHist );}

   if (sigp1!=0) {
     sigp1->Smooth();
     sigp1->Write();
     delete sigp1; sigp1=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   TH2D* p0exp = nullptr;
   if (inputHist!=NULL){
      TH2D *clonep0exp=(TH2D*)inputHist->Clone();
      p0exp = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(p0exp):m12:m0", "p0exp" , "One-sided significance p0 (exp)", "(p0exp>0 && p0exp<=1)", clonep0exp );}
   else{
      p0exp = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(p0exp):m12:m0", "p0exp" , "One-sided significance p0 (exp)", "(p0exp>0 && p0exp<=1)", inputHist );}

   if (p0exp!=0) {
      p0exp->Smooth();
      p0exp->Write();
      delete p0exp; p0exp=0;
   } else {
      cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }
   TH2D* p0expraw = nullptr;
   if (inputHist!=NULL){
      TH2D *clonep0expraw=(TH2D*)inputHist->Clone();
      p0expraw = DrawUtil::triwsmooth( tree, "p0exp:m12:m0", "p0expraw" , "One-sided significance p0 (exp)", "(p0exp>0 && p0exp<=1)", clonep0expraw );}
   else{
      p0expraw = DrawUtil::triwsmooth( tree, "p0exp:m12:m0", "p0expraw" , "One-sided significance p0 (exp)", "(p0exp>0 && p0exp<=1)", inputHist );}

   if (p0expraw!=0) {
      p0expraw->Smooth();
      p0expraw->Write();
      delete p0expraw; p0expraw=0;
   } else {
      cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   // cls:clsexp:clsu1s:clsd1
   TH2D* p1clsf = nullptr;

   if (inputHist!=NULL){
     TH2D *clonep1clsf=(TH2D*)inputHist->Clone();
     p1clsf = DrawUtil::triwsmooth( tree, "CLs:m12:m0", "sigp1clsf" , "Observed CLs", "p1>0 && p1<=1", clonep1clsf );}
   else{
     p1clsf = DrawUtil::triwsmooth( tree, "CLs:m12:m0", "sigp1clsf" , "Observed CLs", "p1>0 && p1<=1", inputHist );
   }


   if (p1clsf!=0) {
     p1clsf->Smooth();
     p1clsf->Write();
     delete p1clsf; p1clsf=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   TH2D* sigp1clsf = nullptr;
   if (inputHist!=NULL){
     TH2D *clonesigp1clsf=(TH2D*)inputHist->Clone();
     sigp1clsf = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLs ):m12:m0", "sigp1clsf" , "One-sided significance of observed CLs", "p1>0 && p1<=1",clonesigp1clsf );}
   else{
     sigp1clsf = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLs ):m12:m0", "sigp1clsf" , "One-sided significance of observed CLs", "p1>0 && p1<=1", inputHist );}


   if (sigp1clsf!=0) {
     sigp1clsf->Smooth();
     sigp1clsf->Write();
     delete sigp1clsf; sigp1clsf=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }
   TH2D* sigp1expclsf = nullptr;
   if (inputHist!=NULL){
     TH2D *clonesigp1expclsf=(TH2D*)inputHist->Clone();
     sigp1expclsf = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLsexp ):m12:m0", "sigp1expclsf" , "One-sided significance of expected CLs", "p1>0 && p1<=1", clonesigp1expclsf );}
   else{
     sigp1expclsf = DrawUtil::triwsmooth( tree, "StatTools::GetSigma( CLsexp ):m12:m0", "sigp1expclsf" , "One-sided significance of expected CLs", "p1>0 && p1<=1", inputHist );}


   if (sigp1expclsf!=0) {
     sigp1expclsf->Smooth();
     sigp1expclsf->Write();
     delete sigp1expclsf; sigp1expclsf=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }
   TH2D* sigclsu1s = nullptr;
   if (inputHist!=NULL){
     TH2D *clonesigclsu1s=(TH2D*)inputHist->Clone();
      sigclsu1s = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(clsu1s):m12:m0", "sigclsu1s" , "One-sided significance of expected CLs (+1 sigma)", "clsu1s>0", clonesigclsu1s );}
   else{
     sigclsu1s = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(clsu1s):m12:m0", "sigclsu1s" , "One-sided significance of expected CLs (+1 sigma)", "clsu1s>0", inputHist );}

   if (sigclsu1s!=0) {
     sigclsu1s->Smooth();
     sigclsu1s->Write();
     delete sigclsu1s; sigclsu1s=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }
   TH2D* sigclsd1s = nullptr;
  if (inputHist!=NULL){
     TH2D *clonesigclsd1s=(TH2D*)inputHist->Clone();
     sigclsd1s = DrawUtil::triwsmooth( tree , "StatTools::GetSigma(clsd1s):m12:m0", "sigclsd1s" , "One-sided significance of expected CLs (-1 sigma)", "clsd1s>0",clonesigclsd1s );}
   else{
     sigclsd1s = DrawUtil::triwsmooth( tree, "StatTools::GetSigma(clsd1s):m12:m0", "sigclsd1s" , "One-sided significance of expected CLs (-1 sigma)", "clsd1s>0", inputHist );}
   if (sigclsd1s!=0) {
     sigclsd1s->Smooth();
     sigclsd1s->Write();
     delete sigclsd1s; sigclsd1s=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }


   ///////////////////////////////////////////////////// upper limit * cross section plots
   TH2D* UpperLimit = nullptr;
  if (inputHist!=NULL){
     TH2D *cloneupperlimit=(TH2D*)inputHist->Clone();
     UpperLimit = DrawUtil::triwsmooth( tree, "upperLimit:m12:m0", "upperLimit" , "upperlimit","1", cloneupperlimit);}
   else{
     UpperLimit = DrawUtil::triwsmooth( tree, "upperLimit:m12:m0", "upperLimit" , "upperlimit","1", inputHist);}


   if (UpperLimit!=0) {
     UpperLimit->Smooth();
     UpperLimit->Write();
     delete UpperLimit; UpperLimit=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   TH2D* ExpectedUpperLimit = nullptr;
  if (inputHist!=NULL){
     TH2D *cloneexpectedUpperlimit=(TH2D*)inputHist->Clone();
     ExpectedUpperLimit = DrawUtil::triwsmooth( tree, "expectedUpperLimit:m12:m0", "expectedUpperLimit" , "expectedUpperlimit","1", cloneexpectedUpperlimit);}
   else{
     ExpectedUpperLimit = DrawUtil::triwsmooth( tree, "expectedUpperLimit:m12:m0", "expectedUpperLimit" , "expectedUpperlimit","1", inputHist);}


   if (ExpectedUpperLimit!=0) {
     ExpectedUpperLimit->Smooth();
     ExpectedUpperLimit->Write();
     delete ExpectedUpperLimit; ExpectedUpperLimit=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   TH2D* xsec = nullptr;
  if (inputHist!=NULL){
     TH2D *clonexsec=(TH2D*)inputHist->Clone();
     xsec = DrawUtil::triwsmooth( tree, "xsec:m12:m0", "xsec" , "xsec","1", clonexsec);}
   else{
     xsec = DrawUtil::triwsmooth( tree, "xsec:m12:m0", "xsec" , "xsec","1", inputHist);}


   if (xsec!=0) {
     xsec->Smooth();
     xsec->Write();
     delete xsec; xsec=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }

   TH2D* excludedXsec = nullptr;
  if (inputHist!=NULL){
     TH2D *cloneexcludedXsec=(TH2D*)inputHist->Clone();
     excludedXsec = DrawUtil::triwsmooth( tree, "excludedXsec:m12:m0", "excludedXsec" , "excludedXsec","1", cloneexcludedXsec);}
   else{
     excludedXsec = DrawUtil::triwsmooth( tree, "excludedXsec:m12:m0", "excludedXsec" , "excludedXsec","1", inputHist);}


   if (excludedXsec!=0) {
     excludedXsec->Smooth();
     excludedXsec->Write();
     delete excludedXsec; excludedXsec=0;
   } else {
     cout << "Cannot make smoothed significance histogram. Exit." << endl;
   }


   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   output->Close();
   //if (output!=0) { delete output; output=0; }
   cout << "Done." << endl;

   return outfile;
}