mcplots - Rev 302

Subversion Repositories:
Rev:
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <TCanvas.h>
#include <TGraphErrors.h>
#include <TGraphAsymmErrors.h>
#include <TROOT.h>
#include <TSystem.h>
#include "TStyle.h"
#include <TAxis.h>
#include <TVectorF.h>
#include <TVectorT.h>
#include <TMultiGraph.h>
#include <TLegend.h>
#include <TString.h>
#include <TPaveText.h>
#include <TText.h>
#include <TAxis.h>
#include <TH1F.h>
#include <TLatex.h>
#include <TMath.h>
#include <TPad.h>
#include <iostream>
#include <sstream>
#include <TColor.h>
#include <TPRegexp.h>
#include <TRegexp.h>

using namespace std;
using namespace TMath;

string Latex2Root2(string latexinput)
{
    string rootoutput;
    TString tlatexinput = latexinput;
    tlatexinput.ReplaceAll("~"," ");
    tlatexinput.ReplaceAll("$","");    
    tlatexinput.ReplaceAll("\\text{GeV}","GeV");
    tlatexinput.ReplaceAll("\\text{ch}","{ch}");
    tlatexinput.ReplaceAll("\\mathrm{d}","d");
    tlatexinput.ReplaceAll("rangle","GT");
    tlatexinput.ReplaceAll("langle","LT");
    tlatexinput.ReplaceAll("p_T","p_{T}");
//    tlatexinput.ReplaceAll("{\\sigma}","#sigma");
//    tlatexinput.ReplaceAll("\\","#");
//    tlatexinput.ReplaceAll("#perp","{#perp}");
   
   
    rootoutput = tlatexinput;
    return rootoutput;
}

string Latex2Root(string latexinput)
{
    cout << " Input Latex : " << latexinput << endl;

    string rootoutput;
    TString tlatexinput = latexinput;
    tlatexinput.ReplaceAll("~"," ");
    tlatexinput.ReplaceAll("$","");
    tlatexinput.ReplaceAll("\\;","");
    tlatexinput.ReplaceAll("rangle","GT");
    tlatexinput.ReplaceAll("langle","LT");
    tlatexinput.ReplaceAll("\\perp","T");
    tlatexinput.ReplaceAll("\\left","");
    tlatexinput.ReplaceAll("\\right","");
    tlatexinput.ReplaceAll("\\mathrm","");
 
    //   tlatexinput.ReplaceAll("\,","");
    TPRegexp myregexpS("\\\\,");
    while (myregexpS.Substitute(tlatexinput," ")){;}


 

//    Tranlastion of ^x to ^{x}
//    TPRegexp myregexp2("\^\([^{}])");
//    while (myregexp2.Substitute(tlatexinput,"\^{$1}"))
//    {
//    }


    // \text

    TPRegexp myregexp11("\\mathrm{\([^}]+)}");
    cout <<"1 " << myregexp11.Substitute(tlatexinput,"$1") << endl;
    while (myregexp11.Substitute(tlatexinput,"$1")){;}
   
   
    TPRegexp myregexp3("_\\\\text{\([^}]+)}");
    cout <<"1 " << myregexp3.Substitute(tlatexinput,"_{$1}") << endl;
    while (myregexp3.Substitute(tlatexinput,"_{$1}")){;}

    TPRegexp myregexp31("\\^\\\\text{\([^}]+)}");
    cout <<"1 " << myregexp31.Substitute(tlatexinput,"^{$1}") << endl;
    while (myregexp31.Substitute(tlatexinput,"^{$1}")){;}


    TPRegexp myregexp4("\\\\text{\([^}]+)}");
    cout <<"2 "<<  myregexp4.Substitute(tlatexinput,"$1") << endl;
    while (myregexp4.Substitute(tlatexinput,"$1")){;}

    tlatexinput.ReplaceAll("{d}","d");    

    TPRegexp myregexp5("d{\([^}]+)}");
    cout <<"1 " << myregexp5.Substitute(tlatexinput,"d$1") << endl;
    while (myregexp5.Substitute(tlatexinput,"d$1")){;}


    // Tranlastion of _x to _{x}

    TPRegexp myregexp1("_\([^{}])");
    while (myregexp1.Substitute(tlatexinput,"_{$1}")){;}

    // Tranlastion of ^x to ^{x}
    TPRegexp myregexp21("\\^\([^{}])");
//    cout << myregexp21.Substitute(tlatexinput,"^{$1}") << endl;
    while (myregexp21.Substitute(tlatexinput,"^{$1}")){;}

   
    TPRegexp myregexp22("bar \([^ ]+)");
    while (myregexp22.Substitute(tlatexinput,"bar{$1}")){;}  
   
   

//    TString mystring = "1/N \\,  \text{d}{N}/\text{d}{A}";   
//      cout << "mystring " << mystring << endl;       

//    TPRegexp myregexpS("\\\\text");
//    cout << myregexp6.Substitute(tlatexinput,"zzz") << endl;
       
 

   
    rootoutput = tlatexinput;



     cout << " Output Latex : " << rootoutput << endl;
    return rootoutput;
}


//Title=Charged multiplicity at $\sqrt{s} = 1800~\text{GeV}$, $|\eta| < 1$, $p_T > 0.4~\text{GeV}$
//XLabel=$N_\text{ch}$
//YLabel=$\mathrm{d}{\sigma}/\mathrm{d}{n_\text{ch}}$

string BracketsAfterUScore(string line)
{
   
    int startPos=0;
    string myline=line.substr(startPos,myline.length());
    size_t underscorePos = myline.find("_"); // searching for underscore
    while (underscorePos != string::npos) {

        underscorePos = myline.find("_");
        startPos=underscorePos+1;
        string nextSymbol=myline.substr(underscorePos+1,1); // what is follwoing symbol
        string oldSymbol=myline.substr(underscorePos,2); // underscore plus symbol
        string newSymbol="_{";
        newSymbol+=nextSymbol;
        newSymbol+="}";
        TString tline = myline;
       
        if (nextSymbol!="{")
        {
            tline.ReplaceAll(oldSymbol,newSymbol);
           
        }
        myline=tline;
    }
   
    return line;
}

 
string RemoveTagAndBrackets(string line, string tagname)
{

// doesn't work if there are some other brackets after _{bla-bla_bla)
   
    tagname+="{";
    size_t beginpos = line.find(tagname);
    size_t endpos = 100000;
    size_t oldpos = 100000;
    while(beginpos!=string::npos && beginpos!=oldpos )
    {
       
        string secondpart = line.substr(beginpos,line.size()-beginpos);
        endpos = beginpos+secondpart.find("}");
        string mywordnew = line.substr(beginpos+tagname.length(), endpos-beginpos-tagname.length());
        if ( (int)beginpos-2 >=0) {
            const string myunderscore = line.substr(beginpos-1,1);
            if (myunderscore=="_") mywordnew = line.substr(beginpos+tagname.length()-1, endpos-beginpos-tagname.length()+2 );
        }
       
        const string mywordold = line.substr(beginpos, endpos-beginpos+1);
        TString tline = line;
        tline.ReplaceAll(mywordold, mywordnew);
        line = tline;
        oldpos = beginpos;
        beginpos = line.find(tagname);
    }
    return line;
}


string Latex2Root_old(string latexinput)
{
   
    string rootoutput;
   
    latexinput = RemoveTagAndBrackets(latexinput,"\\mathrm");
    latexinput = RemoveTagAndBrackets(latexinput,"\\text");
    latexinput = RemoveTagAndBrackets(latexinput,"");
   
    TString tlatexinput = latexinput;
    tlatexinput.ReplaceAll("~"," ");
    tlatexinput.ReplaceAll("$","");
    tlatexinput.ReplaceAll("rangle","GT");
    tlatexinput.ReplaceAll("langle","LT");
    tlatexinput.ReplaceAll("p_T","p_{T}");
    tlatexinput.ReplaceAll("{\\sigma}","#sigma");
    tlatexinput.ReplaceAll("\\, "," ");
    tlatexinput.ReplaceAll("\\","#");
   
//    tlatexinput.ReplaceAll("#perp","{#perp}");
   
   
    rootoutput = tlatexinput;
    return rootoutput;
}




struct RGBColor {
    double red;
    double green;
    double blue;
};


struct Bin {
    double xlow;  
    double xfocus;
    double xhigh;
    double yval;
    double yerrminus;
    double yerrplus;
};

typedef map<string, string> PropMap;

RGBColor getRGBfromString(string rgbstring)
{
   RGBColor myRGB;
   stringstream ss(rgbstring);
 
   ss >> myRGB.red >> myRGB.green >> myRGB.blue  ;
  if (ss.fail( )) {
    throw ("not a color");
  }

    return myRGB;

}




struct Hist {

    Hist()
        {
            myXLabel="";
            myYLabel="";
            boolReference=0;
        }
   
    string myPathToDataFile;
    string myfilename;
    int myMarkerStyle;
    float myMarkerSize;
    RGBColor myRGB;
    int myLineStyle;
    float myLineWidth;
    string myHistoLegend;
   
    PropMap prop;
    vector<Bin> bins;

    int boolReference;

    string myXLabel;
    string myYLabel;

};

vector<Bin> checkAndCleanHisto(vector<Bin> myoldbins)
{
    vector<Bin> mynewbins;
    //erase
    int skipEnd=0;
   
    for (uint i = 0; i != (myoldbins.size()-1); ++i)
    {
       
            //  vector<Bin>::iterator inext=i++;
            //  cout << *i << endl;
        if (!skipEnd)
        {
            Bin myNewBin;
            myNewBin=myoldbins[i];
            mynewbins.push_back(myNewBin);
        }
        if (myoldbins[i].xfocus>=myoldbins[i+1].xfocus)
        {
            cout << " Attention: X values are not ordered by value ! Rest part of data will be ignored " << endl;
            skipEnd=1;
        }

       
        if (myoldbins[i].yval==myoldbins[i+1].yval && i > 5)
        {
            cout << " Attention: Y value is same as for previous bin ! Rest part of data will be ignored " << endl;
            skipEnd=1;
        }

       
   
            cout << " xfocus " <<myoldbins[i].xfocus << endl;
       
    }

    cout << "size before " << myoldbins.size() << " after " <<  mynewbins.size() << endl;
   
    return mynewbins;
}


typedef vector<Hist> Histograms;

struct Plots
{

    Plots()
    {
        myHistoTitle="No Title";
        myXLabel="";
        myXLabel="";
        myXAxisMax=-999;
        myXAxisMin=-999;
        myYAxisMax=-999;
        myYAxisMin=-999;
        myUpperRightLabel="";
        myUpperLeftLabel="";
        myTextField1="";
        myTextField2="";
        myHistoLogY=0;
        myHistoLogX=0;
        myDrawGrid=0;
        myDrawErrorBarX=0;
        myDrawRatioPlot=0;
       
    }


   
    Histograms histos;
    string myHistoTitle;
    string  myXLabel;
    string  myYLabel;
 
    float myXAxisMax;
    float myXAxisMin;
    float myYAxisMax;
    float myYAxisMin;
       
        float myYGlobalMax;
        float myYGlobalMin;
       
   
    string myUpperRightLabel;
    string myUpperLeftLabel;
    string myTextField1;
    string myTextField2;
    int myHistoLogY;
    int myHistoLogX;
    int myDrawGrid;
    int myDrawErrorBarX;
    int myDrawRatioPlot;

   
    string myOutputFileName;

   
};


Hist readDataFile(string fname)
{
    Hist myhist;
   
    vector<Bin> mybins;
    cout <<"datafile"<< fname << endl;
    ifstream f(fname.c_str());
    string line;
    // read file line by line
    while (getline(f, line)) {
        // histogram block
        if (line.find("# BEGIN HISTOGRAM") == 0) {
 
            // read histogram data
            while (getline(f, line)) {
                const size_t endpos = line.find("# END HISTOGRAM");
                if (endpos == 0) break;
       
                const size_t eqpos = line.find("=");
                if (eqpos != string::npos) {
                    // if where is '=' sign in line, this is a property string: name=value
                    const string name = line.substr(0, eqpos);
                    const string value = line.substr(eqpos + 1);
                    if ( name == "XLabel" ) myhist.myXLabel=Latex2Root(value);
                    if ( name == "YLabel" ) myhist.myYLabel=Latex2Root(value);    
                    // hist.prop[name] = value;
                }
                else {
                    // this is bin content
         
                    // TODO: check number of numbers in line
                    //       should be exactly 4
                    const size_t hashpos = line.find("#");
                    if (hashpos == string::npos)
                    {

                        istringstream ss(line);
                        Bin bin;
                        ss >> bin.xlow >> bin.xfocus >> bin.xhigh >> bin.yval >> bin.yerrminus >> bin.yerrplus ;
                        //cout <<  bin.xlow << endl;
                        if (bin.yval!=0. && bin.yerrminus!=0 && bin.yerrplus!=0)   mybins.push_back(bin); // If y value and its errors = 0 do not read the point.
                        if (ss.bad()) {
                            cerr << "ERROR: fail to read bin content"
                                 << "       " << line << "\n"
                                 << endl;

                        }
                    }
           
                }
            }
     
     
        }
    }

// myYGlobalMax;
// myYGlobalMin;
    myhist.bins=mybins;
    return myhist;

}



Plots read(const char* fname)
{

    gROOT->SetStyle("Plain");
    Plots myplots= Plots();
    Histograms histograms;
 
    ifstream f(fname);
    string line;
    string histTitle;
    string histXtitle;
    string histYtitle;

 
    // read file line by line
    while (getline(f, line)) {
//header block
        if (line.find("# BEGIN PLOT") == 0) {

            while (getline(f, line)) {
                const size_t endpos = line.find("# END PLOT");

       
                if (endpos == 0) break;
                const size_t hashpos = line.find("#");
                if (hashpos == 0) continue;
                const size_t eqpos = line.find("=");
                if (eqpos != string::npos) {
                    // if where is '=' sign in line, this is a property string: name=value
                    const string name = line.substr(0, eqpos);
                    const string value = line.substr(eqpos + 1);
                    // hist.prop[name] = value;
                    //  cout << " value name: " << name << endl;
                    if (name == "XLabel") myplots.myXLabel=Latex2Root(value);
                    if (name == "YLabel") myplots.myYLabel=Latex2Root(value);
                    if (name == "Title")
                    {
                        myplots.myHistoTitle=Latex2Root(value);
                        cout << " Title found : " <<  myplots.myHistoTitle << endl;
                    }
                    if (name == "LogY") myplots.myHistoLogY=atoi(value.c_str());
                    if (name == "LogX") myplots.myHistoLogX=atoi(value.c_str());
                    if (name == "XAxisMin") myplots.myXAxisMin=atof(value.c_str());
                    if (name == "XAxisMax") myplots.myXAxisMax=atof(value.c_str());
                    if (name == "YAxisMin") myplots.myYAxisMin=atof(value.c_str());
                    if (name == "YAxisMax") myplots.myYAxisMax=atof(value.c_str());
                    if (name == "upperLeftLabel") myplots.myUpperLeftLabel=Latex2Root(value);
                    if (name == "upperRightLabel") myplots.myUpperRightLabel=Latex2Root(value);
                    if (name == "textField1") myplots.myTextField1=value;
                    if (name == "textField2") myplots.myTextField2=value;
                    if (name == "outputFileName") myplots.myOutputFileName=value;
                    if (name == "drawGrid") myplots.myDrawGrid=atoi(value.c_str());
                    if (name == "drawErrorBarX") myplots.myDrawErrorBarX=atoi(value.c_str());
                    if (name == "drawRatioPlot") myplots.myDrawRatioPlot=atoi(value.c_str());
                       
                }

            }


        }

     
        // histogram block
        if (line.find("# BEGIN HISTOGRAM") == 0) {
            //cout << " Begin found " << endl;
            Hist hist;
            // extract name
            //hist.name = line.substr(18); // index = length("# BEGIN HISTOGRAM") + 1
 
            // read histogram data
            while (getline(f, line)) {
                const size_t endpos = line.find("# END HISTOGRAM");

       
                if (endpos == 0) break;

                const size_t hashpos = line.find("#");
                if (hashpos != string::npos) continue;
       
                const size_t eqpos = line.find("=");
                if (eqpos != string::npos) {
                    // if where is '=' sign in line, this is a property string: name=value
                    const string name = line.substr(0, eqpos);
                    const string value = line.substr(eqpos + 1);
                    hist.prop[name] = value;
                    //  cout << " value name: " << name << endl;
                    if (name == "filename") hist.myfilename=value;
                    if (name == "pathtodatafile") hist.myPathToDataFile=value;
                    if (name == "markerStyle") hist.myMarkerStyle=atoi(value.c_str());
                    if (name == "markerSize") hist.myMarkerSize=atof(value.c_str());
                    if (name == "lineWidth") hist.myLineWidth=atof(value.c_str());
                    if (name == "lineStyle") hist.myLineStyle=atoi(value.c_str());
                    if (name == "color")
                    {
                        hist.myRGB=getRGBfromString(value.c_str());
                    }
                    if (name == "legend") hist.myHistoLegend=value;
                    if (name == "reference") hist.boolReference=atoi(value.c_str());;
                }
            }
            Hist retrivedHist = readDataFile(hist.myfilename);

            // Insert consistency check for histograms here.(And removing "problematic" part)
         
           
           
            //  hist.bins = checkAndCleanHisto(retrivedHist.bins);
            hist.bins = retrivedHist.bins;
            if (myplots.myXLabel=="") myplots.myXLabel=Latex2Root(retrivedHist.myXLabel);
            if (myplots.myYLabel=="") myplots.myYLabel=Latex2Root(retrivedHist.myYLabel);            
            histograms.push_back(hist);
        }
    }
    myplots.histos=histograms;

    return myplots;
}


void plot(Histograms myhists)
{

    cout << " Number of histograms " << myhists.size() << endl;;
   


}



int main (int argc, char *argv[] )
//int run(int argc, char *argv[] )
{

  // Definitions: left and right margin
  const double leftMargin = 0.125;
 
  // Default
  bool kLegendPositionRight=1;
 
  for (int ninputfile = 1;  ninputfile<argc; ninputfile++)
   
    {
     

   
    Plots myplots;
   
    myplots = read(argv[ninputfile]);
   


    cout << " Number of Histograms : " << myplots.histos.size() << endl;
    float numHist = (float)myplots.histos.size();
// Old    TLegend* legend  = new TLegend(0.45, 0.7, 0.85, 0.80);



    // Determining what is greater first or second part of function
        double maxYGlobal = -10000000;
        double minYGlobal = 100000000;
       
    double max1=-100000.;
    double max2=-100000.;
    for (uint ihist = 0; ihist<myplots.histos.size(); ihist++)
    {

        int nrealbin = 0;
        vector<Bin> currentBins = myplots.histos[ihist].bins;
        for (uint ibin = 0; ibin+1<currentBins.size(); ibin++)
        {
            if ( currentBins[ibin].yval > maxYGlobal ) maxYGlobal = currentBins[ibin].yval;
                        if ( currentBins[ibin].yval < minYGlobal && currentBins[ibin].yval>0.000001 ) minYGlobal = currentBins[ibin].yval;
            //  xfocusV[ibin]=currentBins[ibin].xfocus;
            //  yvalV[ibin]=currentBins[ibin].yval;
                        if (ibin<currentBins.size()-1){
                if (currentBins[ibin+1].xfocus>=currentBins[ibin].xfocus && currentBins[ibin+1].yval!=0.) nrealbin++;
            }  
        }
        int middle=nrealbin/2;

        for (uint ibin = 0; ibin<middle; ibin++)
        {
            if (max1<currentBins[ibin].yval) max1 = currentBins[ibin].yval;
        }
        for (uint ibin = middle; ibin<=nrealbin; ibin++)
        {
            if (max2<currentBins[ibin].yval) max2 = currentBins[ibin].yval;
        }
       
        cout << "nrealbin " << nrealbin << endl;
     
    }
      cout << " Max1 = " << max1 << " Max2 = "<< max2 << endl;
      if (max2>max1) kLegendPositionRight=0;
    // End of Determining what is greater first or second part of function
    cout << " Global Y Maximum : " << maxYGlobal << endl;
        cout << " Global Y Minimum : " << minYGlobal << endl;
       
        // Override myplots.myHistoLogY from file of log(maxYGlobal) - log(maxYGlobal) < 1.2
        if ( abs(Log10(maxYGlobal) - Log10(minYGlobal))<1.2)
        {
        myplots.myHistoLogY=0;
        cout << " Warning: myplots.myHistoLogY changed to 0" << endl;
        }
       
   
    TLegend* legend;
    double xLegWidth = 0.29;
    double xLegLo = 0.655;
    double xLegHi = xLegLo + xLegWidth;
    double yLegHi = 0.825;
   double yLegLo = yLegHi - 0.035*numHist;
    if (!kLegendPositionRight) {
      xLegLo = leftMargin + 0.075;
      xLegHi = xLegLo + xLegWidth;
    }
    legend = new TLegend(xLegLo,yLegLo,xLegHi,yLegHi);
   
    legend->SetTextFont(43);
    legend->SetTextSize(20);
    legend->SetFillColor(0);
    legend->SetBorderSize(0);

   

   
//c1->SetGrid();
//c1->GetFrame()->SetFillColor(21);
//c1->GetFrame()->SetBorderSize(12);
 

    TMultiGraph *mg = new TMultiGraph();
    TMultiGraph *mgRatio = new TMultiGraph();



    cout << " Size "<< myplots.histos.size() << endl;
   

    double xmaxData = -100000;
    double ymaxData = -100000;
    double xminData = 1000000;
    double yminData = 1000000;

    int ref=0;
    for (uint ihist = 0; ihist<myplots.histos.size(); ihist++)
    {
        if (myplots.histos[ihist].boolReference==1)
        {
            ref=ihist;
            break;
        }
    }
    cout << "My reference " << ref << endl;
    cout << " Number of bins in reference " << myplots.histos[ref].bins.size() << endl;;
    int nRefRatio=0;
    int nNotRefRatio=0;
    for (int ihist = 0; ihist<myplots.histos.size(); ihist++)
    {
     
        float xlowV[700];
        float xhighV[700];
        float xfocusV[700];
        float yvalV[700];
        float yerrminusV[700];
        float yerrplusV[700];
           
        float xerrplusV[700];
        float xerrminusV[700];
        float xerrplusVRatio[700];
        float xerrminusVRatio[700];        
       

        float ratioY[700];
        float ratioYErrorPlus[700];
        float ratioYErrorMinus[700];
        float ratioXfocus[700];
                       
        vector<Bin> currentBins = myplots.histos[ihist].bins;
        int numberOfPoints = currentBins.size();
        int numberOfPointsRatio = myplots.histos[ref].bins.size();
        int numberOfPointsRatioReal(0);
        cout << " Bine size " << currentBins.size() << " of histo " << ihist << endl;

       
       
        for (uint ibin = 0; ibin<currentBins.size(); ibin++)
        {
            xlowV[ibin]=currentBins[ibin].xlow;
            xhighV[ibin]=currentBins[ibin].xhigh;
            xfocusV[ibin]=currentBins[ibin].xfocus;
            yvalV[ibin]=currentBins[ibin].yval;
            yerrminusV[ibin]=currentBins[ibin].yerrminus;
            yerrplusV[ibin]=currentBins[ibin].yerrplus;

            for (int ibinref=0; ibinref<myplots.histos[ref].bins.size(); ibinref++ )
            {
               
                double xfocusref = myplots.histos[ref].bins[ibinref].xfocus;
                double xfocus =  xfocusV[ibin];
               
                if ( fabs(xfocus - xfocusref)<0.0001 && myplots.histos[ref].bins[ibinref].yval!=0)
                {
                   
                    cout << " Same bin found " << endl;
                    ratioXfocus[numberOfPointsRatioReal]=currentBins[ibin].xfocus;
                    ratioY[numberOfPointsRatioReal]=currentBins[ibin].yval/myplots.histos[ref].bins[ibinref].yval;
                    //ratioYErrorPlus[ibinref]=fabs(-ratioY[ibin]+(yvalV[ibin]+yerrplusV[ibin])/myplots.histos[ref].bins[ibinref].yval);
                    //ratioYErrorMinus[ibinref]=fabs(-ratioY[ibin]+(yvalV[ibin]-yerrminusV[ibin])/myplots.histos[ref].bins[ibinref].yval);
                    ratioYErrorPlus[numberOfPointsRatioReal]=yerrplusV[ibin]/myplots.histos[ref].bins[ibinref].yval;
                    ratioYErrorMinus[numberOfPointsRatioReal]=yerrminusV[ibin]/myplots.histos[ref].bins[ibinref].yval;
                    // ratioYErrorPlus[ibinref]=0;
                    // ratioYErrorMinus[ibinref]=0;        
                    xerrminusVRatio[numberOfPointsRatioReal] = 0;
                    xerrplusVRatio[numberOfPointsRatioReal]  = 0;
                    if (ihist==ref){
                        xerrminusVRatio[numberOfPointsRatioReal] = fabs(currentBins[ibinref].xfocus - currentBins[ibinref].xlow);
                        xerrplusVRatio[numberOfPointsRatioReal]  = fabs(currentBins[ibinref].xfocus - currentBins[ibinref].xhigh);
                        ratioYErrorPlus[numberOfPointsRatioReal]  =  yerrplusV[ibinref]/myplots.histos[ref].bins[ibinref].yval;
                        ratioYErrorMinus[numberOfPointsRatioReal] = yerrminusV[ibinref]/myplots.histos[ref].bins[ibinref].yval;
                       
                    }
                    numberOfPointsRatioReal++;
                }
            }
            if (ihist==ref && numberOfPointsRatioReal>3) nRefRatio++;
            if (ihist!=ref && numberOfPointsRatioReal>3) nNotRefRatio++;
            // check if ref is existed and number of filles ratio plots with good number of points
            // if not - supress frawing of ratio plots.
           

            /*              
            if (myplots.histos[ref].bins[ibin].yval!=0 && yvalV[ibin]!=0 && ibin<myplots.histos[ref].bins.size()  )
            {
                ratioY[ibin]=currentBins[ibin].yval/myplots.histos[ref].bins[ibin].yval;
                ratioYErrorPlus[ibin]=fabs(-ratioY[ibin]+(yvalV[ibin]+yerrplusV[ibin])/myplots.histos[ref].bins[ibin].yval);
                ratioYErrorMinus[ibin]=fabs(-ratioY[ibin]+(yvalV[ibin]-yerrminusV[ibin])/myplots.histos[ref].bins[ibin].yval);
            }
            else
            {
                ratioY[ibin]=0;
                ratioYErrorPlus[ibin]=0;
                ratioYErrorMinus[ibin]=0;
            }
            */

           
            if (myplots.myDrawErrorBarX)
            {
                xerrminusV[ibin] = fabs(currentBins[ibin].xfocus - currentBins[ibin].xlow);
                xerrplusV[ibin]  = fabs(currentBins[ibin].xfocus - currentBins[ibin].xhigh);
            }
            else
            {
                xerrminusV[ibin] = 0.;
                xerrplusV[ibin]  = 0.;
                //    xerrminusVRatio[ibin] = fabs(currentBins[ibin].xfocus - currentBins[ibin].xlow);
                //  xerrplusVRatio[ibin]  = fabs(currentBins[ibin].xfocus - currentBins[ibin].xhigh);
            }
           
            if (   myplots.myHistoLogY==1 ) {
                if (xfocusV[ibin]>xmaxData && yvalV[ibin]>0 ) xmaxData = xfocusV[ibin];
                if (yvalV[ibin]>ymaxData) ymaxData = yvalV[ibin];
                if (xfocusV[ibin]<xminData && yvalV[ibin]>0 ) xminData = xfocusV[ibin];
                if (yvalV[ibin]<yminData && yvalV[ibin]>0) yminData = yvalV[ibin];
            }
            else
            {
                if (xfocusV[ibin]>xmaxData) xmaxData = xfocusV[ibin];
                if (yvalV[ibin]>ymaxData) ymaxData = yvalV[ibin];
                if (xfocusV[ibin]<xminData ) xminData = xfocusV[ibin];
                if (yvalV[ibin]<yminData) yminData = yvalV[ibin];

            }

        }


       
        TGraphAsymmErrors *gr;
       
        gr = new TGraphAsymmErrors(numberOfPoints, xfocusV,yvalV, xerrminusV, xerrplusV , yerrminusV, yerrplusV);
     
       
        Int_t colorIndex = 1700+ihist; // color index
        TColor *color = new TColor(colorIndex, myplots.histos[ihist].myRGB.red,myplots.histos[ihist].myRGB.green,myplots.histos[ihist].myRGB.blue);
         
         

       
        gr->SetMarkerStyle(myplots.histos[ihist].myMarkerStyle);
        gr->SetMarkerColor(colorIndex);
        gr->SetMarkerSize(myplots.histos[ihist].myMarkerSize);
       
        gr->SetLineColor(colorIndex);
        gr->SetLineWidth(myplots.histos[ihist].myLineWidth);
        gr->SetLineStyle(myplots.histos[ihist].myLineStyle);

 
        legend->AddEntry(gr, (myplots.histos[ihist].myHistoLegend).c_str() , "LP");

       
        if (myplots.histos[ihist].myLineWidth ==0) mg->Add(gr,"P");
        if (myplots.histos[ihist].myLineWidth !=0) mg->Add(gr,"LP");

       
        TGraphAsymmErrors *grRatioPlot;

        // if (ihist!=ref) grRatioPlot = new TGraphAsymmErrors(numberOfPoints, xfocusV,ratioY, xerrminusV, xerrplusV , ratioYErrorMinus, ratioYErrorPlus);
        //    if (ihist==ref) grRatioPlot = new TGraphAsymmErrors(numberOfPoints, xfocusV,ratioY, xerrminusVRatio, xerrplusVRatio , ratioYErrorMinus, ratioYErrorPlus);
       
        //      grRatioPlot = new TGraphAsymmErrors(numberOfPointsRatio,ratioXfocus,ratioY, xerrminusVRatio, xerrplusVRatio , ratioYErrorMinus, ratioYErrorPlus);
        grRatioPlot = new TGraphAsymmErrors(numberOfPointsRatioReal,ratioXfocus,ratioY, xerrminusVRatio, xerrplusVRatio , ratioYErrorMinus, ratioYErrorPlus);
     
       
        grRatioPlot->SetMarkerStyle(myplots.histos[ihist].myMarkerStyle);
        grRatioPlot->SetMarkerColor(colorIndex);
        grRatioPlot->SetMarkerSize(myplots.histos[ihist].myMarkerSize);
        grRatioPlot->SetLineColor(colorIndex);
        grRatioPlot->SetLineWidth(myplots.histos[ihist].myLineWidth);
        grRatioPlot->SetLineStyle(myplots.histos[ihist].myLineStyle);
       
        grRatioPlot->GetXaxis()->SetTitle((myplots.myXLabel).c_str());
        grRatioPlot->GetYaxis()->SetTitle((myplots.myYLabel).c_str());

        grRatioPlot->GetXaxis()->SetNdivisions(20505);
        grRatioPlot->GetXaxis()->SetLabelSize(0.035);
        grRatioPlot->GetYaxis()->SetLabelSize(0.035);
        grRatioPlot->GetYaxis()->SetLabelOffset(0.025);
        grRatioPlot->GetXaxis()->SetLabelFont(42);
        grRatioPlot->GetYaxis()->SetLabelFont(42);
        cout << "ihist " << ihist << endl;
       
        if (ihist==ref) {
            cout << " Reference found " << ref << endl;
            grRatioPlot->SetFillStyle(1001);
            grRatioPlot->SetFillColor(kYellow);
            mgRatio->Add(grRatioPlot, "E2");
        }
        else
        {
           
            mgRatio->Add(grRatioPlot,"L");
        }
       
       
    }
   
    if ( !nRefRatio ) myplots.myDrawRatioPlot=0;
    if ( !nNotRefRatio ) myplots.myDrawRatioPlot=0;  

    TCanvas * c1;
    TPad * mypad1;
    TPad * mypad2;
    if (!myplots.myDrawRatioPlot)
    {
       
        c1 = new TCanvas("c1","A Simple Graph with error bars",750,750);
        mypad1 = new TPad("mypad","mypad",0.,0.,1.,1.);
        mypad1->SetGrid(myplots.myDrawGrid,myplots.myDrawGrid);
        mypad1->SetBorderMode(0);
        mypad1->SetFillColor(kWhite);
        mypad1->SetTopMargin(0.06);
        mypad1->SetRightMargin(0.035);
        mypad1->SetLeftMargin(leftMargin);
        mypad1->SetTickx();
        mypad1->SetTicky();
        c1->cd();
        mypad1->Draw();
        mypad1->cd();
    }

    if (myplots.myDrawRatioPlot)
    {
        c1 = new TCanvas("c1","A Simple Graph with error bars",750,1075);
        mypad1 = new TPad("mypad","mypad",0.,0.33333,1.,1.);
        mypad1->SetGrid(myplots.myDrawGrid,myplots.myDrawGrid);
        mypad1->SetBorderMode(0);
        mypad1->SetFillColor(kWhite);
        mypad1->SetTopMargin(0.06);
        mypad1->SetRightMargin(0.035);
        mypad1->SetLeftMargin(leftMargin);
        mypad1->SetTickx();
        mypad1->SetTicky();        
        mypad2 = new TPad("mypad","mypad",0.,0.,1.,0.33333);
        mypad2->SetGrid(myplots.myDrawGrid,myplots.myDrawGrid);
        mypad2->SetRightMargin(0.035);
        mypad2->SetLeftMargin(0.125);
        mypad2->SetBorderMode(0);
        mypad2->SetFillColor(kWhite);
        mypad2->SetTickx();
        mypad2->SetTicky();    

       
        mypad1->Draw();
        mypad1->cd();
           
    }
   
    if (myplots.myDrawGrid)
    {

        gStyle->SetGridColor(kOrange-9);
        gStyle->SetGridStyle(1);
        gStyle->SetGridWidth(0.01);    
    }



   
   
   if (myplots.myHistoLogY==1)  mypad1->SetLogy();
   if (myplots.myHistoLogX==1)  mypad1->SetLogx();
   mypad1->Draw();
   


   mg->Draw("A");
   
   mg->GetXaxis()->SetTitle((myplots.myXLabel).c_str());
   mg->GetYaxis()->SetTitle((myplots.myYLabel).c_str());
   mg->GetXaxis()->SetNdivisions(20505);
   mg->GetXaxis()->SetLabelSize(0.035);
   mg->GetYaxis()->SetLabelSize(0.035);
   mg->GetYaxis()->SetLabelOffset(0.015);
   mg->GetXaxis()->SetLabelFont(42);
   mg->GetYaxis()->SetLabelFont(42);
   mg->GetXaxis()->SetTitleFont(42);
   mg->GetYaxis()->SetTitleFont(42);
   mg->GetYaxis()->SetTitleOffset(1.35);


   // Axis Ranges
    float maxYAxis=-100;
    float minYAxis=-100;
    // maxXAxis
    if (myplots.myHistoLogY==1)
    {
        maxYAxis = Power(10, (Log10(ymaxData)+  (Log10(ymaxData)- Log10(yminData))*0.25) );
        minYAxis = Power(10, (Log10(yminData)-  (Log10(ymaxData)- Log10(yminData))*0.1) );

        float diff = log10(maxYAxis)-log10(minYAxis);
        if (diff<2.)
        {
            maxYAxis = Power(10, ceil(Log10(ymaxData)) +  0.15* ( ceil(Log10(ymaxData)) - floor(Log10(yminData)) )    );
            minYAxis = Power(10, floor(Log10(yminData)) );        


        }


       
    }
    else
    {
        maxYAxis = ymaxData + (ymaxData-yminData)*0.25;
        minYAxis = yminData - (ymaxData-yminData)*0.1;
    }
   
        mg->GetYaxis()->SetRangeUser(minYAxis , maxYAxis);
       
        mg->Draw();
        c1->Modified();
        mg->Draw();
   
    if ( myplots.myYAxisMin!=-999 && myplots.myYAxisMax!=-999 )
    {
       
        mg->GetYaxis()->SetRangeUser(myplots.myYAxisMin, myplots.myYAxisMax);
   
        mg->Draw();
        c1->Modified();
        mg->Draw();
    }

    if (myplots.myXAxisMin!=-999 && myplots.myXAxisMax!=-999 )
    {
        mg->GetXaxis()->SetLimits(myplots.myXAxisMin, myplots.myXAxisMax);
        c1->Modified();
        mypad1->Modified();
        mg->Draw();
        mypad1->Modified();
        c1->Modified();
    }

    if (myplots.myXAxisMin==-999 && myplots.myXAxisMax==-999 )
    {
        mg->GetXaxis()->SetLimits(xminData - 0.05*(xmaxData-xminData), xmaxData+ 0.05*(xmaxData-xminData));
        mypad1->Modified();
        c1->Modified();
        mg->Draw();
        mypad1->Modified();
        c1->Modified();  

    }    

    // Extract final plot ranges
    double xPlotLo = mg->GetXaxis()->GetXmin();
    double xPlotHi = mg->GetXaxis()->GetXmax();
    double yPlotLo = minYAxis;
    double yPlotHi = maxYAxis;

    // Compute coordinates for box and text inside box
    double xBoxLo    = xPlotLo - 0.001*(xPlotHi-xPlotLo);
    double xBoxHi    = xPlotHi + 0.001*(xPlotHi-xPlotLo);
    double yBoxLo    = yPlotHi;
    double yBoxHi    = yPlotHi + 0.064*(yPlotHi-yPlotLo);
    double yBoxT     = yPlotHi + 0.009*(yPlotHi-yPlotLo);
    double xBoxTlo   = xPlotLo + 0.025 *(xPlotHi-xPlotLo);
    double xBoxThi   = xPlotHi - 0.025 *(xPlotHi-xPlotLo);
    if (myplots.myHistoLogY==1) {
      double LogYplotLo = log10(yPlotLo);
      double LogYplotHi = log10(yPlotHi);
      double dLogY      = LogYplotHi - LogYplotLo;
      double LogYboxHi  = LogYplotHi + 0.064*dLogY;
      double LogYboxT   = LogYplotHi + 0.009*dLogY;
      yBoxHi = pow(10,LogYboxHi);
      yBoxT  = pow(10,LogYboxT);
    }

    legend->Draw();

    // Top Black box
    TBox * myBox = new TBox(xBoxLo,yBoxLo,xBoxHi,yBoxHi);
    myBox->SetFillColor(1);
    myBox->Draw();

    TText * myText1 = new TText(xBoxTlo,yBoxT,(myplots.myUpperLeftLabel).c_str());
    myText1->SetTextAlign(11);
    myText1->SetTextColor(0);
    myText1->SetTextSize(0.05);
    myText1->SetTextFont(42);
    myText1->Draw();

    TText * myText2 = new TText(xBoxThi,yBoxT,(myplots.myUpperRightLabel).c_str());
    myText2->SetTextAlign(31);
    myText2->SetTextColor(0);
    myText2->SetTextSize(0.04);
    myText2->SetTextFont(42);
    myText2->Draw();

    TText * myText3 = new TText(xBoxHi,yPlotLo,string("mcplots.cern.ch").c_str());
    myText3->SetTextAngle(90.);
    myText3->SetTextAlign(13);
    myText3->SetTextColor(kGray+1);
    myText3->SetTextSize(0.02);
    myText3->SetTextFont(42);
    myText3->Draw();

   
    // Monte Carlo Event Generator Version Numbers
    double xC_NDC = leftMargin +
      0.5 * (1.0-mypad1->GetRightMargin()-leftMargin);
    TText * mytextbox1 = new TText(xC_NDC,0.15,(myplots.myTextField1).c_str());
    mytextbox1->SetTextFont(40);
    mytextbox1->SetNDC(kTRUE);
    mytextbox1->SetTextAlign(22);
    mytextbox1->SetTextSize(0.0275);
    mytextbox1->SetTextColor(kGray+1);
    mytextbox1->Draw();

    // Data Reference
    TText * mytextbox2 = new TText(xC_NDC,0.2,(myplots.myTextField2).c_str());
    mytextbox2->SetNDC(kTRUE);
    mytextbox2->SetTextFont(40);
    mytextbox2->SetTextAlign(22);
    mytextbox2->SetTextSize(0.0275);
    mytextbox2->SetTextColor(kGray+1);
    mytextbox2->Draw();

    // Plot title (LaTeX)
    TLatex * myMainTitle = new TLatex(xC_NDC,0.87,(myplots.myHistoTitle).c_str());
    myMainTitle->SetNDC(kTRUE);
    myMainTitle->SetTextFont(42);
    myMainTitle->SetTextAlign(22);
    myMainTitle->SetTextSize(0.0275);
    myMainTitle->SetTextColor(1);
    myMainTitle->Draw();




    TPaveText * myPaveRatio = new TPaveText(0.125,0.899672 ,1-0.035,0.899672+0.1,"NDC");
    //upperLeftLabel
    TString nameRatio = "Ratio to ";
    nameRatio+=myplots.histos[ref].myHistoLegend;
    TText * ttextRatio = myPaveRatio->AddText(nameRatio);
    ttextRatio->SetTextAlign(22);
    ttextRatio->SetTextColor(0);
    myPaveRatio->SetFillColor(1);
    myPaveRatio->SetTextColor(0);
    myPaveRatio->SetTextFont(42);
    myPaveRatio->SetBorderSize(0.);
   
    if (myplots.myDrawRatioPlot)
    {

       
        c1->cd();
        mypad2->Draw();
        mypad2->cd();
        mgRatio->Draw("A");
        mgRatio->GetXaxis()->SetLabelSize(0.070);
        mgRatio->GetYaxis()->SetLabelSize(0.070);
        mgRatio->GetYaxis()->SetLabelOffset(0.015);
        mgRatio->GetXaxis()->SetLabelFont(42);
        mgRatio->GetYaxis()->SetLabelFont(42);
        mgRatio->GetYaxis()->SetTitleOffset(1.2);
        mgRatio->GetYaxis()->SetNdivisions(20505,-1);
        mgRatio->GetYaxis()->SetRangeUser(0.5,1.5);
        mgRatio->GetXaxis()->SetNdivisions(20505);


       
        //mgRatio->Draw();
        mypad2->Modified();
        mgRatio->Draw();
        if ( myplots.myXAxisMin!=-999 && myplots.myXAxisMax!=-999 )
        {
            mgRatio->GetXaxis()->SetLimits(myplots.myXAxisMin, myplots.myXAxisMax);
            mypad2->Modified();
            c1->Modified();
            mgRatio->Draw();
            mypad2->Modified();
            c1->Modified();
        }
        if (myplots.myXAxisMin==-999 && myplots.myXAxisMax==-999 )
        {
            mgRatio->GetXaxis()->SetLimits(xminData - 0.05*(xmaxData-xminData), xmaxData+ 0.05*(xmaxData-xminData));
            mypad2->Modified();
            c1->Modified();
            mgRatio->Draw();
            mypad2->Modified();
            c1->Modified();
        }






       
        myPaveRatio->Draw();
    }
   

    c1->SaveAs((myplots.myOutputFileName + ".pdf").c_str());
    c1->SaveAs((myplots.myOutputFileName + ".eps").c_str());


    }
   
    return 0;
}