gROOT->Reset();

/* Program to:
   Make distribution of gains histogram for every eta bin

   Input:  B2Csummary.csv (file_sum)
   Output: plot4.ps

   Author:  J. Millane

*/

#include "Riostream.h"

    const char file1[100]="tables/F2Gsummary.dat";
    const char file2[100]="plot4.ps";
const int nEtabin = 12, nSector = 12, nSubsector = 5, nTower = 720;

    float gainMin[nEtabin+1], gainMax[nEtabin+1];
    float oldGain[nSector+1][nSubsector+1][nEtabin+1], newGain[nSector+1][nSubsector+1][nEtabin+1];

Plot4() {

    // Initialize arrays
    for(int eta = 1; eta <= nEtabin; eta++) {
	gainMin[eta]=1000;
	gainMax[eta]=0;
	for(int sub = 1; sub <= nSubsector; sub++)
	    for(int sec = 1; sec <= nSector; sec++)
		oldGain[eta][sub][sec]=newGain[eta][sub][sec]=0;
    }
    readSum();
    // Graphics
    TCanvas *c1 = new TCanvas("c1","Type A",1);
    c1->Divide(4,6);

    for(int eta = 1; eta <= nEtabin; eta++) {
    
	// Decide which pad to draw histogram onto
	if(eta < 5) c1->cd(eta);
	else if(eta < 9) c1->cd(eta+4);
	else c1->cd(eta+8);

	plot(&eta,oldGain,"Old Gain");

	// Decide which pad to draw histogram onto
	if(eta < 5) c1->cd(eta+4);
	else if(eta < 9) c1->cd(eta+8);
	else c1->cd(eta+12);

	plot(&eta,newGain,"New Gain");

    } 
    // begin testing
    for(int i = 1; i <= nEtabin; i++) printf("gainMin[%d]=%2.2f, gainMin[%d]=%2.2f\n",i,gainMin[i],i,gainMax[i]);
    // end testing

    // clean up
    c1->Print(file2);
    cout << endl;

    return;
}
readSum() {
    char trash_ca[100], trash_c, subsector_holder;
    int sec, sub, eta;
    
    // open summary file
    ifstream ifile(file1,ios::in);

    if (!ifile) {
	cerr << file1 << " could not be opened" << endl;
	exit(1);
    }
    for(int i = 0; i < 14; i++) ifile >> trash_ca;   // discard header

    // read in oldGain and newGain
    for(int i = 0; i < nTower; i++) {
	
	ifile >> eta;
	for(int j = 0; j < 4; j++) ifile >> trash_ca;
	ifile >> sec >> trash_c >> subsector_holder >> trash_ca;  // read in tower
	sub = subChar2num(subsector_holder);
	for(int j = 0; j < 2; j++) ifile >> trash_ca;
	ifile >> oldGain[sec][sub][eta] >> newGain[sec][sub][eta];
	for(int j = 0; j < 4; j++) ifile >> trash_ca;
	// begin testing
	if(i==1) {
	    printf("tower %2.2dT%c%2.2d : oldGain = %2.2f, newGain = %2.2f\n",sec,subsector_holder,eta,oldGain[sec][sub][eta],newGain[sec][sub][eta]);

	}
	// end testing
	
	// Determine gain Min, gain max
	if(oldGain[sec][sub][eta] > gainMax[eta]) gainMax[eta]=oldGain[sec][sub][eta];
	if(oldGain[sec][sub][eta] < gainMin[eta]) gainMin[eta]=oldGain[sec][sub][eta];
    }
    ifile.close();
    
    return;
}
int subChar2num(char holder) {
    int sub = 13;

    if(holder=='A') sub = 1;
    else if(holder=='B') sub = 2;
    else if(holder=='C') sub = 3;
    else if(holder=='D') sub = 4;
    else if(holder=='E') sub = 5;
    
    return sub;
}
void plot(int *eta, float g[nSector+1][nSubsector+1][nEtabin+1],char t[]) {
    TString title = "Gain Distribution of etabin ";

    title+=*eta;
    
    TH1F *histo = new TH1F("histo",title,40,gainMin[*eta]-1,gainMax[*eta]+1);
    // TH1F *histo = new TH1F("histo",title,40,0,20);
    // setFillColor
    if(t[0]=='O') histo->SetFillColor(4);
    else histo->SetFillColor(2);
    
    for(int sec = 1; sec <= nSector; sec++)
	for(int sub = 1; sub <= nSubsector; sub++)
	    histo->Fill(g[sec][sub][*eta]);

    // begin testing
    //histo->SetAxisRange(0,20);
    //cout << "gainMin[*eta]=" << gainMin[*eta] << endl;
    // end testing
    //histo->Draw("bar");
    histo->Draw();
    histo->GetXaxis()->SetTitle(t);
    histo->Draw("same");

    return;
}