#include <Riostream.h>

void flux_gerr(char *string){
  gROOT->Reset();
  gROOT->ProcessLine(".L /home/nfields/scripts/dateaxis.C");

  gStyle->SetPalette(1,0);
  gStyle->SetOptStat("nie");
  gStyle->SetPadBorderMode(0);
  gStyle->SetPadColor(0);
  gStyle->SetCanvasColor(0);  
  gStyle->SetTitleColor(1);
  gStyle->SetStatColor(0);
  gStyle->SetFillColor(0);
  
  char infileB[500], infileV[500], infileR[500], outeps[500];
  
  Float_t JDB[100], fluxB[100], JDV[100], fluxV[100], JDR[100], fluxR[100];
  Float_t xerr[100], fgerrB[100], fgerrV[100], fgerrR[100];
  Int_t countB, countV, countR;
  Float_t xmax, xmin, ymax, ymin;
   
  sprintf(infileB,"%s-B.fgerr",string);
  sprintf(infileV,"%s-V.fgerr",string);
  sprintf(infileR,"%s-R.fgerr",string);
    
  
  sprintf(outeps,"../plots/%s.fg.eps",string);
    
  ifstream inB, inV, inR;
  inB.open(infileB);
  inV.open(infileV);
  inR.open(infileR);
    
  for(int n=0;n<100;n++){xerr[n]=0.0;}
 
  countB = 0;   
  countV = 0;   
  countR = 0;
  
  while(1){
    inB >> JDB[countB] >> fluxB[countB] >> fgerrB[countB];
    if (!inB.good()) break;
    countB++;}

  while(1){
    inV >> JDV[countV] >> fluxV[countV] >> fgerrV[countV];
    if (!inV.good()) break;
      countV++;}
  
  while(1){
    inR >> JDR[countR] >> fluxR[countR] >> fgerrR[countR];
    if (!inR.good()) break;
    countR++;}
    
  inB.close();
  inV.close();
  inR.close();
  
  c1 = new TCanvas("c1","c1",200,10,700,500);

  c1->cd(1);

  gPad->SetBottomMargin(.15);
  gPad->SetLeftMargin(.15);

  TGraphErrors *gr0 = new TGraphErrors(countB,JDB,fluxB,xerr,fgerrB);
  
  TGraphErrors *gr1 = new TGraphErrors(countV,JDV,fluxV,xerr,fgerrV);

  TGraphErrors *gr2 = new TGraphErrors(countR,JDR,fluxR,xerr,fgerrR);

  TMultiGraph *mg = new TMultiGraph("",string);
 
  TLegend *leg = new TLegend(.80,0.02,.99,0.10);
  leg->SetFillColor(0);
  leg->AddEntry(gr0,"B filter","P");
  leg->AddEntry(gr1,"V filter","P");
  leg->AddEntry(gr2,"R filter","P");

 
  gr0->SetMarkerStyle(8);
  gr1->SetMarkerStyle(8);
  gr2->SetMarkerStyle(8);
  
  gr0->SetMarkerColor(kBlue);
  gr1->SetMarkerColor(kGreen);
  gr2->SetMarkerColor(kRed);
  
  gr0->SetMarkerSize(0.5);  
  gr1->SetMarkerSize(0.5);
  gr2->SetMarkerSize(0.5);

  mg->Add(gr0,"P");
  mg->Add(gr1,"P");
  mg->Add(gr2,"P");

  mg->Draw("A");

  c1->Modified(); c1->Update();
 
  mg->GetXaxis()->SetTitle("Modified Julian Date");
  mg->GetXaxis()->CenterTitle();
  mg->GetXaxis()->SetNdivisions(5);
  mg->GetYaxis()->SetTitle("Flux (Jy)");
  mg->GetYaxis()->CenterTitle();
  mg->GetYaxis()->SetTitleOffset(1.7);

  mg->Draw("A");

  DateAxis(c1);

  leg->Draw();

  c1->SaveAs(outeps);

  
}

