ROOT logo
// $Id$
// 
//  Macro to overlay the histograms produced by  
//  HLT/QA/tasks/macros/drawTHnSparse.C
//  
//  It assumes a txt file where the input is specified in 
//  the following format:
//  
//   file1 legend1
//   file2 legend2
//  //file3 legend3
//  //file4 legend4
//  ...
//  So it is possible to "comment out" a file by a // in the beginning of the name. While reading the 
//  the names of the input files, the macro skips the ones that have the // in front of them.
//
//  Run it by:
//  aliroot overlayPlots.C++ 
// 
//  @ingroup alihlt_qa
//  @author Kalliopi.Kanaki@ift.uib.no 


#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TSystem.h"
#include "TROOT.h"
#include "TFile.h"
#include "TString.h"
#include "TList.h"
#include "THnSparse.h"
#include "TCanvas.h"
#include "TText.h"
#include "TPaveText.h"
#include "TPaveStats.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TH3D.h"
#include "TLegend.h"
#include "TStyle.h"
#include "TPad.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
using std::endl;
#endif

void printStats(TH1D *h1, TH1D *h2);
void defineYaxisMax(TH1D *h1, TH1D *h2);

void overlayPlots(TString plottype="track" /*or event*/,const char* option="HLT"/* or "OFF" */,  Bool_t bAddRunName=kTRUE, Bool_t bDrawNormalized=kTRUE, string fi="files.txt"){


  printf("test\n"); 
  gROOT->SetStyle("Plain");
  gStyle->SetPalette(1);
  gStyle->SetOptStat("emr");
  gStyle->SetTitleX(gStyle->GetPadLeftMargin());
 
  char filenames[100];
  sprintf(filenames,"%s",fi.c_str());
  ifstream infile;
  infile.open(filenames);
  if(!infile){
    printf("File %s does not exist", fi.data());
    return;
  }
  string c;
  string f;
  int nr_textfile = 0;

  vector<string> file;
  vector<string> cutnames;
  string plotname="";
  string t("//");

  while(1){
    infile >> f >> c;
    if(!infile.good()) break;
    if (f.compare(0, t.length(), t) == 0) continue;
    file.push_back(f);
    cutnames.push_back(c);
    nr_textfile++;
  }
  infile.close();
  cout << cutnames.size()<< endl;

  printf("Number of files: %d\n", nr_textfile);
  
  TCanvas *ca;
  TFile   *ff; 
  TPad    *pad; 
  TH1D    *g[nr_textfile];
  
  TCanvas *d = new TCanvas("d",Form("Compare %s distributions",option),1600,1000);
  d->Divide(4,2);
  //d->Divide(3,2);
  TLegend *l;
  if(plottype.Contains("event"))
    l = new TLegend(0,0,0.95,0.95);
  else
    l = new TLegend(0.35,0.3,0.89,0.8);
  l->SetFillColor(10);
  l->SetLineColor(10);
  
  char cut[100];  

  int nr_pads=0;
  if(plottype.Contains("event"))nr_pads=7;
  else nr_pads=9;

  for(int j=1; j<nr_pads; j++){ 
    for(int i=0; i<nr_textfile; i++){ 
       
      ff = TFile::Open(file[i].data());   
      if(!ff || ff->IsZombie()){
	printf("Non-existent, corrupted or zombie file %s\n", file[i].data());
	return;
      } 
      if(plottype.Contains("event"))
	ca  = (TCanvas*)ff->GetObjectUnchecked("can1");
      else		    
	ca  = (TCanvas*)ff->GetObjectUnchecked("can3");		    
      if(!ca){
	printf("Empty canvas in file %s.\n", file[i].data());
	continue;
      }
      if(plottype.Contains("event"))
	pad = (TPad*)ca->GetListOfPrimitives()->FindObject(Form("can1_%d",j));         	
      else
	pad = (TPad*)ca->GetListOfPrimitives()->FindObject(Form("can3_%d",j));         	
      if(!pad){
	printf("Empty pad in canvas %s.\n", ca->GetName());
	continue;	     
      }
      if(plottype.Contains("event"))
	g[i] =(TH1D*)pad->FindObject(Form("fEvent%s_proj_%d",option,j-1));
      else
	g[i] =(TH1D*)pad->FindObject(Form("fTrack%s_proj_%d",option,j-1));
      if(!g[i]){
	printf("Empty histogram for i=%d, file %s.\n", i, file[i].data());
	continue;
      }
        
      d->cd(j);
		
      if(i==0){
	g[i]->SetLineColor(kBlack);
	//	  defineYaxisMax(hlt, off);
	if(bDrawNormalized) 
	  g[i]->DrawNormalized();
	else
	  g[i]->Draw();
	if(option=="OFF"){
	  TPaveStats *st = (TPaveStats*)g[i]->FindObject("stats");
	  st->SetTextColor(kBlack);
	  d->Update();
	}
      }
      else { 
	if(i<4)
	  g[i]->SetLineColor(i+1);
	else
	  g[i]->SetLineColor(i+2);
	defineYaxisMax(g[0], g[i]);
	if(bDrawNormalized)  g[i]->DrawNormalized("sames");
	else g[i]->Draw("sames");

      }

      if(!bDrawNormalized)					 
	if(i>0) printStats(g[i-1], g[i]);
	
      ff->Close();
      sprintf( cut,"%s",cutnames[i].c_str() );
      if((j==2&&plottype.Contains("track")) || ((j==6&&plottype.Contains("event")))) {

	l->AddEntry(g[i],cut,"l");	    	
	//	  cutnames[i].resize(6);
	plotname+="_"+cutnames[i];
	//cout << "Adding Run: " << plotname <<endl;
	cout << "Adding Run: " << cutnames[i] <<endl;
      }
      else continue;
    }

    if(j==2 && plottype.Contains("track") ) l->Draw("same");
    if(plottype.Contains("event") && j==6) {
      d->cd(7);
      l->Draw();

    }
    d->Update();
  }

  d->Update(); 

  sprintf(filenames,"%s",plotname.c_str());
  cout << filenames << endl;
  if( bAddRunName){
    d->SaveAs(Form("overlay_%s_for%s.root",option,filenames));
    d->Print(Form("overlay_%s_for%s.png",option,filenames));
  }
  else{
    d->SaveAs(Form("overlay_%s.root",option));
    d->Print(Form("overlay_%s.png",option));
  }

  return;
}

void printStats(TH1D *h1, TH1D *h2){  
  gPad->Update();
  TPaveStats *st1 = (TPaveStats*)h1->FindObject("stats");
  st1->SetLineColor(0);

  gPad->Update();
  TPaveStats *st2 = (TPaveStats*)h2->FindObject("stats");
  st2->SetY2NDC(st1->GetY1NDC()-0.05);
  st2->SetY1NDC(st2->GetY2NDC()-TMath::Abs(st1->GetY1NDC()-st1->GetY2NDC()));
  st2->SetLineColor(0);
  st2->SetTextColor(h2->GetLineColor());
  st2->SetFillStyle(0);
  st2->Draw();
  return;
}

void defineYaxisMax(TH1D *h1, TH1D *h2){   
  //Y axis
  if(h1->GetMaximum() > h2->GetMaximum()) h2->SetMaximum(1.1*h1->GetMaximum());
  else h1->SetMaximum(1.1*h2->GetMaximum());
  
  h1->SetMinimum(0);
  h2->SetMinimum(0);
 
  // X axis  
  double xmin, xmax;  
  if(h1->GetBinLowEdge(1) > h2->GetBinLowEdge(1)) xmin = h1->GetBinLowEdge(1);
  else xmin = h2->GetBinLowEdge(1);
  if(h1->GetBinLowEdge(h1->GetNbinsX()+1) > h2->GetBinLowEdge(h1->GetNbinsX()+1)) xmax = h1->GetBinLowEdge(h1->GetNbinsX()+1);
  else xmax = h2->GetBinLowEdge(h2->GetNbinsX()+1);
  
  h2->SetAxisRange(xmin, xmax, "X");
  return;
}
 overlayPlots.C:1
 overlayPlots.C:2
 overlayPlots.C:3
 overlayPlots.C:4
 overlayPlots.C:5
 overlayPlots.C:6
 overlayPlots.C:7
 overlayPlots.C:8
 overlayPlots.C:9
 overlayPlots.C:10
 overlayPlots.C:11
 overlayPlots.C:12
 overlayPlots.C:13
 overlayPlots.C:14
 overlayPlots.C:15
 overlayPlots.C:16
 overlayPlots.C:17
 overlayPlots.C:18
 overlayPlots.C:19
 overlayPlots.C:20
 overlayPlots.C:21
 overlayPlots.C:22
 overlayPlots.C:23
 overlayPlots.C:24
 overlayPlots.C:25
 overlayPlots.C:26
 overlayPlots.C:27
 overlayPlots.C:28
 overlayPlots.C:29
 overlayPlots.C:30
 overlayPlots.C:31
 overlayPlots.C:32
 overlayPlots.C:33
 overlayPlots.C:34
 overlayPlots.C:35
 overlayPlots.C:36
 overlayPlots.C:37
 overlayPlots.C:38
 overlayPlots.C:39
 overlayPlots.C:40
 overlayPlots.C:41
 overlayPlots.C:42
 overlayPlots.C:43
 overlayPlots.C:44
 overlayPlots.C:45
 overlayPlots.C:46
 overlayPlots.C:47
 overlayPlots.C:48
 overlayPlots.C:49
 overlayPlots.C:50
 overlayPlots.C:51
 overlayPlots.C:52
 overlayPlots.C:53
 overlayPlots.C:54
 overlayPlots.C:55
 overlayPlots.C:56
 overlayPlots.C:57
 overlayPlots.C:58
 overlayPlots.C:59
 overlayPlots.C:60
 overlayPlots.C:61
 overlayPlots.C:62
 overlayPlots.C:63
 overlayPlots.C:64
 overlayPlots.C:65
 overlayPlots.C:66
 overlayPlots.C:67
 overlayPlots.C:68
 overlayPlots.C:69
 overlayPlots.C:70
 overlayPlots.C:71
 overlayPlots.C:72
 overlayPlots.C:73
 overlayPlots.C:74
 overlayPlots.C:75
 overlayPlots.C:76
 overlayPlots.C:77
 overlayPlots.C:78
 overlayPlots.C:79
 overlayPlots.C:80
 overlayPlots.C:81
 overlayPlots.C:82
 overlayPlots.C:83
 overlayPlots.C:84
 overlayPlots.C:85
 overlayPlots.C:86
 overlayPlots.C:87
 overlayPlots.C:88
 overlayPlots.C:89
 overlayPlots.C:90
 overlayPlots.C:91
 overlayPlots.C:92
 overlayPlots.C:93
 overlayPlots.C:94
 overlayPlots.C:95
 overlayPlots.C:96
 overlayPlots.C:97
 overlayPlots.C:98
 overlayPlots.C:99
 overlayPlots.C:100
 overlayPlots.C:101
 overlayPlots.C:102
 overlayPlots.C:103
 overlayPlots.C:104
 overlayPlots.C:105
 overlayPlots.C:106
 overlayPlots.C:107
 overlayPlots.C:108
 overlayPlots.C:109
 overlayPlots.C:110
 overlayPlots.C:111
 overlayPlots.C:112
 overlayPlots.C:113
 overlayPlots.C:114
 overlayPlots.C:115
 overlayPlots.C:116
 overlayPlots.C:117
 overlayPlots.C:118
 overlayPlots.C:119
 overlayPlots.C:120
 overlayPlots.C:121
 overlayPlots.C:122
 overlayPlots.C:123
 overlayPlots.C:124
 overlayPlots.C:125
 overlayPlots.C:126
 overlayPlots.C:127
 overlayPlots.C:128
 overlayPlots.C:129
 overlayPlots.C:130
 overlayPlots.C:131
 overlayPlots.C:132
 overlayPlots.C:133
 overlayPlots.C:134
 overlayPlots.C:135
 overlayPlots.C:136
 overlayPlots.C:137
 overlayPlots.C:138
 overlayPlots.C:139
 overlayPlots.C:140
 overlayPlots.C:141
 overlayPlots.C:142
 overlayPlots.C:143
 overlayPlots.C:144
 overlayPlots.C:145
 overlayPlots.C:146
 overlayPlots.C:147
 overlayPlots.C:148
 overlayPlots.C:149
 overlayPlots.C:150
 overlayPlots.C:151
 overlayPlots.C:152
 overlayPlots.C:153
 overlayPlots.C:154
 overlayPlots.C:155
 overlayPlots.C:156
 overlayPlots.C:157
 overlayPlots.C:158
 overlayPlots.C:159
 overlayPlots.C:160
 overlayPlots.C:161
 overlayPlots.C:162
 overlayPlots.C:163
 overlayPlots.C:164
 overlayPlots.C:165
 overlayPlots.C:166
 overlayPlots.C:167
 overlayPlots.C:168
 overlayPlots.C:169
 overlayPlots.C:170
 overlayPlots.C:171
 overlayPlots.C:172
 overlayPlots.C:173
 overlayPlots.C:174
 overlayPlots.C:175
 overlayPlots.C:176
 overlayPlots.C:177
 overlayPlots.C:178
 overlayPlots.C:179
 overlayPlots.C:180
 overlayPlots.C:181
 overlayPlots.C:182
 overlayPlots.C:183
 overlayPlots.C:184
 overlayPlots.C:185
 overlayPlots.C:186
 overlayPlots.C:187
 overlayPlots.C:188
 overlayPlots.C:189
 overlayPlots.C:190
 overlayPlots.C:191
 overlayPlots.C:192
 overlayPlots.C:193
 overlayPlots.C:194
 overlayPlots.C:195
 overlayPlots.C:196
 overlayPlots.C:197
 overlayPlots.C:198
 overlayPlots.C:199
 overlayPlots.C:200
 overlayPlots.C:201
 overlayPlots.C:202
 overlayPlots.C:203
 overlayPlots.C:204
 overlayPlots.C:205
 overlayPlots.C:206
 overlayPlots.C:207
 overlayPlots.C:208
 overlayPlots.C:209
 overlayPlots.C:210
 overlayPlots.C:211
 overlayPlots.C:212
 overlayPlots.C:213
 overlayPlots.C:214
 overlayPlots.C:215
 overlayPlots.C:216
 overlayPlots.C:217
 overlayPlots.C:218
 overlayPlots.C:219
 overlayPlots.C:220
 overlayPlots.C:221
 overlayPlots.C:222
 overlayPlots.C:223
 overlayPlots.C:224
 overlayPlots.C:225
 overlayPlots.C:226
 overlayPlots.C:227
 overlayPlots.C:228
 overlayPlots.C:229
 overlayPlots.C:230
 overlayPlots.C:231
 overlayPlots.C:232
 overlayPlots.C:233
 overlayPlots.C:234
 overlayPlots.C:235
 overlayPlots.C:236
 overlayPlots.C:237
 overlayPlots.C:238
 overlayPlots.C:239
 overlayPlots.C:240
 overlayPlots.C:241
 overlayPlots.C:242
 overlayPlots.C:243
 overlayPlots.C:244
 overlayPlots.C:245
 overlayPlots.C:246