Exemple #1
0
void Plot_2D(string filename){
  
  //string filename = "data/scan_CH1-50_masked.root";
 
  // string varXname = "VMM # [1-8]";
  // string varYname = "CH # [1-64]";

  string varXname = "Pulsed CH # [1-64]";
  string varYname = "Recorded CH # [1-64]";
  string varZname = "# Recorded Pulses [/100 pulsed]";
  //string varZname = "#sigma(PDO) / #bar{PDO}";
  // int Nx = 8;
  // double Xmin = 0.5;
  // double Xmax = 8.5;
  int Nx = 64;
  double Xmin = 0.5;
  double Xmax = 64.5;
  // int Ny = 64;
  // double Ymin = 0.5;
  // double Ymax = 64.5;
  int Ny = 4097;
  double Ymin = -0.5;
  double Ymax = 4096.5;

  ///////////////////////////////////////////////////////
  setstyle(0);
  
  TChain* tree = new TChain("VMM_data","VMM_data");

  tree->AddFile(filename.c_str());

  MMFE8Base* base = new MMFE8Base(tree);

  int N = tree->GetEntries();

  TH2D* hist = new TH2D("hist","hist",
			Nx, Xmin, Xmax,
			Ny, Ymin, Ymax);

  TH2D* hist2 = new TH2D("hist2","hist2",
			 Nx, Xmin, Xmax,
			 Ny, Ymin, Ymax);

  TH2D* histN = new TH2D("histN","histN",
			 Nx, Xmin, Xmax,
			 Ny, Ymin, Ymax);

  for(int i = 0; i < N; i++){
    base->GetEntry(i);

    if(base->VMM != 0)
      continue;

    if(base->CHword == 24)
      continue;

    if(base->Delay != 30)
      continue;
    
    // hist->Fill(base->CHpulse,base->CHword,base->PDO);
    //histN->Fill(base->CHpulse,base->CHword);
    histN->Fill(base->CHpulse,base->BCID);

    // if(base->CHpulse != 5)
    //   continue;
    // base->CHpulse = base->CHword;

    hist->Fill(base->VMM,base->CHpulse,base->PDO);
    hist2->Fill(base->VMM,base->CHpulse,base->PDO*base->PDO);
    // if(histN->GetBinContent(base->VMM,base->CHpulse) <= 0)
    //   histN->SetBinContent(base->VMM,base->CHpulse,base->BCID);
    // else
    //   if(histN->GetBinContent(base->VMM,base->CHpulse) > base->BCID)
    // histN->SetBinContent(base->VMM,base->CHpulse,base->BCID);
    // histN->Fill(base->VMM,base->CHpulse);
  
  }

  for(int x = 0; x < Nx; x++){
    for(int y = 0; y < Ny; y++){
      double v = hist->GetBinContent(x+1,y+1);
      double v2 = hist2->GetBinContent(x+1,y+1);
      double N = histN->GetBinContent(x+1,y+1);
      double vbar = v/max(int(N),1);
      double v2bar = v2/max(int(N),1);
      //hist->SetBinContent(x+1,y+1,sqrt(v2bar-vbar*vbar)/vbar);
      hist->SetBinContent(x+1,y+1,vbar);
    }
  }

  TCanvas* can = new TCanvas("can","can",600,500);
  can->SetLeftMargin(0.15);
  can->SetRightMargin(0.22);
  can->SetBottomMargin(0.15);
  can->SetTopMargin(0.08);

  can->Draw();
  can->SetGridx();
  can->SetGridy();
  
  can->cd();

  hist = histN;

  hist->Draw("COLZ");

  hist->GetXaxis()->CenterTitle();
  hist->GetXaxis()->SetTitleFont(132);
  hist->GetXaxis()->SetTitleSize(0.06);
  hist->GetXaxis()->SetTitleOffset(1.06);
  hist->GetXaxis()->SetLabelFont(132);
  hist->GetXaxis()->SetLabelSize(0.05);
  hist->GetXaxis()->SetTitle(varXname.c_str());
  hist->GetYaxis()->CenterTitle();
  hist->GetYaxis()->SetTitleFont(132);
  hist->GetYaxis()->SetTitleSize(0.06);
  hist->GetYaxis()->SetTitleOffset(1.12);
  hist->GetYaxis()->SetLabelFont(132);
  hist->GetYaxis()->SetLabelSize(0.05);
  hist->GetYaxis()->SetTitle(varYname.c_str());
  hist->GetZaxis()->CenterTitle();
  hist->GetZaxis()->SetTitleFont(132);
  hist->GetZaxis()->SetTitleSize(0.06);
  hist->GetZaxis()->SetTitleOffset(1.3);
  hist->GetZaxis()->SetLabelFont(132);
  hist->GetZaxis()->SetLabelSize(0.05);
  hist->GetZaxis()->SetTitle(varZname.c_str());
  hist->GetZaxis()->SetRangeUser(0.9*hist->GetMinimum(),1.1*hist->GetMaximum());

  TLatex l;
  l.SetTextFont(132);
  l.SetNDC();
  l.SetTextSize(0.05);
  l.SetTextFont(132);
  l.DrawLatex(0.5,0.943,"MMFE8 Analysis");
  l.SetTextSize(0.04);
  l.SetTextFont(42);
  l.DrawLatex(0.15,0.943,"#bf{#it{ATLAS}} Internal");

  l.SetTextSize(0.06);
  l.SetTextFont(132);
  l.DrawLatex(0.80,0.04, "VMM #6");

 
}
Exemple #2
0
void pidFitter::runDkl( TH2D* h, reporter * rp, string optPath ){

	gStyle->SetOptStat( 1 );

	int nS = config->getInt( optPath + ".dkl:nSpecies", 1 );
	int nIt = config->getInt( optPath + ".dkl:nRuns", 1 );
	double angle = config->getDouble( optPath + ".dkl:angle", 0 );

	if ( nS <= 0 )
		return;
	if ( nIt <= 0 )
		return;
	if ( NULL == h || NULL == rp )
		return;

	double axisMin = config->getDouble( "binning.nSig:min", 0 );
	double axisMax = config->getDouble( "binning.nSig:max", 0 );

	dklMinimizer *dkl = new dklMinimizer( h, nS, angle );
	dkl->run( nIt );

	rp->newPage( 1, 2 );

	rp->cd( 1, 1);
	gPad->SetLogz(1);
	dkl->viewInput( )->Draw("colz");

	rp->cd( 2, 1);
	gPad->SetLogz(1);
	//TH2D* ap = dkl->viewApproximation( axisMin, axisMax, axisMin, axisMax );
	TH2D* ap = dkl->viewApproximation( );
	double min = 0.01;//ap->GetMinimum() * 0.1;
	double max = ap->GetMaximum();
	ap->Draw("colz");
	rp->savePage();

	rp->newPage( 1, 3 );
	rp->cd( 1, 1);
	gPad->SetLogz(1);

	TH2D* s1 = NULL, *s2 = NULL, *s3 = NULL;
	s1 = dkl->viewSpecies( 0 );
	s1->GetZaxis()->SetRangeUser( min, max );
	s1->Draw("colz");

	if ( nS >= 2 ){
		rp->cd( 1, 2);
		gPad->SetLogz(1);
		s2 = dkl->viewSpecies( 1 );
		s2->GetZaxis()->SetRangeUser( min, max );
		s2->Draw("colz");
	}

	if ( nS >= 3 ){
		rp->cd( 1, 3);
		gPad->SetLogz(1);
		s3 = dkl->viewSpecies( 2 );
		s3->GetZaxis()->SetRangeUser( min, max );
		s3->Draw("colz");
	}

	rp->savePage();

	rp->newPage();
	uint ciS = dklMinimizer::speciesClosestTo( 0, 0, s1, s2, s3 );
	//TH2 * cSpecies = dklMinimizer::viewSpeciesClosestTo( 0, 0, s1, s2, s3 );
	//TH2D* ps1 = (TH2D*)cSpecies->Clone( "probabilityS1" );
	TH2D * ps1 = (TH2D*) dkl->speciesProbabilityMap( ciS );
	cout << " Input Yield: " << dkl->inputYield( ) << endl;;
	cout << " Species Yield: " << dkl->speciesYield( ciS ) << endl;;
	cout << " Total Approx Yield: " << dkl->approximationYield( ) << endl;;

	makeSquareCuts( ps1, "K_Fit.dklPostFitCut" );
	//ps1->Divide( ap );

	ps1->Draw( "colz" );
	ps1->GetZaxis()->SetRangeUser( 0, 1 );

	rp->savePage();

	//sDelete( s1 );
	//sDelete( s2 );
	//sDelete( s3 );
}
Exemple #3
0
void chipSummary(const char *dirName, int chipId)
{
        directory = TString(dirName);
	
	if (f && f->IsOpen()) f->Close();
	if (f1 && f1->IsOpen()) f1->Close();
	if (g && g->IsOpen()) g->Close();

	gROOT->SetStyle("Plain");
	gStyle->SetPalette(1);
	gStyle->SetOptStat(0);
	gStyle->SetTitle(0);

	gStyle->SetStatFont(132);
	gStyle->SetTextFont(132);
	gStyle->SetLabelFont(132, "X");
	gStyle->SetLabelFont(132, "Y");
	gStyle->SetLabelSize(0.08, "X");
	gStyle->SetLabelSize(0.08, "Y");
	gStyle->SetNdivisions(6, "X");
	gStyle->SetNdivisions(8, "Y");
	gStyle->SetTitleFont(132);

	gROOT->ForceStyle();

	tl = new TLatex;
	tl->SetNDC(kTRUE);
	tl->SetTextSize(0.09);

	ts = new TLatex;
	ts->SetNDC(kTRUE);
	ts->SetTextSize(0.08);

	line = new TLine;
	line->SetLineColor(kRed);
	line->SetLineStyle(kSolid);
	
	box = new TBox;
	box->SetFillColor(kRed);
	box->SetFillStyle(3002);

	f = new TFile(Form("%s/%s", dirName, fileName), "READ");
	
	if (strcmp(fileName, adFileName) == 0) f1 = f;
	else f1 = new TFile(Form("%s/%s", dirName, adFileName), "READ");
	
	if (strcmp(fileName, trimFileName) == 0) g = f;
	else g = new TFile(Form("%s/%s", dirName, trimFileName), "READ");
 
	sprintf(fname, "%s/../../macros/criteria-full.dat", dirName);
	if ( !readCriteria(fname) ) { 
	  
	  printf("\nchipSummary> ----> COULD NOT READ GRADING CRITERIA !!!");
	  printf("chipSummary> ----> Aborting execution of chipgSummaryPage.C ... \n\n", fileName, dirName);  
	  break;
	}

	TH1D *h1;
	TH2D *h2;

	c1 = new TCanvas("c1", "", 800, 800);
	c1->Clear();
	c1->Divide(4,4, 0.01, 0.04);

  //	shrinkPad(0.1, 0.1, 0.1, 0.3);

	FILE *sCurveFile, *phLinearFile, *phTanhFile;

        TString noslash(dirName);
        noslash.ReplaceAll("/", " ");
        noslash.ReplaceAll(".. ", "");
	
	char string[200];
	int pixel_alive;

	int nDeadPixel(0);
	int nIneffPixel(0);
	int nMaskDefect(0);
	int nNoisy1Pixel(0);
	int nDeadBumps(0);
	int nDeadTrimbits(0);
	int nAddressProblems(0);

	int nNoisy2Pixel(0);
	int nThrDefect(0);
	int nGainDefect(0);
	int nPedDefect(0);
	int nPar1Defect(0);

	int nRootFileProblems(0);

	int nDoubleFunctCounts(0);
	int nDoublePerfCounts(0);
	int nDoubleCounts(0);
	int nDoubleTrims(0);
	int nDoublePHs(0);

        int vcal = dac_findParameter(dirName, "Vcal", chipId);
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 1
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Dead pixels
	c1->cd(1);

	TH2D *hpm;
	hpm = (TH2D*)f->Get(Form("PixelMap_C%i", chipId));

        if (hpm) {

	  for (int icol = 0; icol < 52; ++icol) {
	    for (int irow = 0; irow < 80; ++irow) {      
	      
	      hpm->SetTitle("");
	      hpm->Draw("colz");
	      tl->DrawLatex(0.1, 0.92, "Pixel Map");
	    }
	  }

	} else { 
	  
	  ++nRootFileProblems; 
	}


	// -- sCurve width and noise level
	TH1D *hw = new TH1D("hw", "", 100, 0., 600.);
	TH1D *hd = new TH1D("hd", "", 100, 0., 600.);  // Noise in unbonded pixel (not displayed)
	TH2D *ht = new TH2D("ht", "", 52, 0., 52., 80, 0., 80.);
	TH1D *htmp;

	float mN(0.), sN(0.), nN(0.), nN_entries(0.);
	int over(0), under(0);

	double htmax(255.), htmin(0.);
	
	float thr, sig;
	int a,b;

	double minThrDiff(-5.);
	double maxThrDiff(5.);

	h2 = (TH2D*)f->Get(Form("vcals_xtalk_C%i", chipId));
        
	sprintf(string, "%s/SCurve_C%i.dat", dirName, chipId);
	sCurveFile = fopen(string, "r");

	if (!sCurveFile) {

	  printf("chipSummary> !!!!!!!!!  ----> SCurve: Could not open file %s to read fit results\n", string);
	
	} else {
	  
	  for (int i = 0; i < 2; i++) fgets(string, 200, sCurveFile);
	  
	  for (int icol = 0; icol < 52; ++icol)	{
	    for (int irow = 0; irow < 80; ++irow) {
	      
	      fscanf(sCurveFile, "%e %e %s %2i %2i", &thr, &sig, string, &a, &b);
	      //  				printf("chipSummary> sig %e thr %e\n", sig, thr);
	      
	      hw->Fill(sig);
	      thr = thr / 65.;
	      
	      ht->SetBinContent(icol+1, irow+1, thr); 
	      
	      if ( h2 ) {
		if( h2->GetBinContent(icol+1, irow+1)  >= minThrDiff) {
		  
		  hd->Fill(sig);
		}
	      }
	    }
	  }
	  
	  fclose(sCurveFile);

	  c1->cd(2);
	  hw->Draw();
	  tl->DrawLatex(0.1, 0.92, "S-Curve widths: Noise (e^{-})");
	  
	  
	  /*		c1->cd(15);
			hd->SetLineColor(kRed);
			hd->Draw();
			tl->DrawLatex(0.1, 0.92, "S-Curve widths of dead bumps");
			if ( hd->GetEntries() > 0 ) {
			ts->DrawLatex(0.55, 0.82, Form("entries: %4.0f", hd->GetEntries()));
			ts->DrawLatex(0.55, 0.74, Form("#mu:%4.2f", hd->GetMean()));
			ts->DrawLatex(0.55, 0.66, Form("#sigma: %4.2f", hd->GetRMS()));
			}
	  */
	  
	  mN =  hw->GetMean();
	  sN =  hw->GetRMS();
	  nN =  hw->Integral(hw->GetXaxis()->GetFirst(), hw->GetXaxis()->GetLast());
	  nN_entries =  hw->GetEntries();
	  
	  under = hw->GetBinContent(0);
	  over  = hw->GetBinContent(hw->GetNbinsX()+1);
	  
	  
	  ts->DrawLatex(0.65, 0.82, Form("N: %4.0f", nN));
	  ts->DrawLatex(0.65, 0.74, Form("#mu: %4.1f", mN));
	  ts->DrawLatex(0.65, 0.66, Form("#sigma: %4.1f", sN));
	  
	  if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
	  if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
	  
	  c1->cd(3);
	  if ( ht->GetMaximum() < htmax ) { 
	    htmax = ht->GetMaximum();
	  }
	  if ( ht->GetMinimum() > htmin ) {
	    htmin = ht->GetMinimum();
	  }
	  ht->GetZaxis()->SetRangeUser(htmin,htmax);
	  ht->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Vcal Threshold Untrimmed");
	  
	}
	
	// -- Noise level map
	c1->cd(4);
        gPad->SetLogy(1);
 	gStyle->SetOptStat(1);

	float mV(0.), sV(0.), nV(0.), nV_entries(0.);
	over = 0.; under = 0.;

	if (!g->IsZombie())
	{
	      h1 = (TH1D*)g->Get(Form("VcalThresholdMap_C%iDistribution;7", chipId));
              if (h1) {
		h1->SetTitle("");
		h1->SetAxisRange(0., 100.);
		h1->Draw();

		mV = h1->GetMean();
		sV = h1->GetRMS();
		nV = h1->Integral(h1->GetXaxis()->GetFirst(), h1->GetXaxis()->GetLast());
		nV_entries = h1->GetEntries();

		under = h1->GetBinContent(0);
		over  = h1->GetBinContent(h1->GetNbinsX()+1);
              }
              else {

	        ++nRootFileProblems;
		mV = 0.;
		sV = 0.;
               
              }

	      ts->DrawLatex(0.15, 0.82, Form("N: %4.0f", nV));
	      ts->DrawLatex(0.15, 0.74, Form("#mu: %4.1f", mV));
	      ts->DrawLatex(0.15, 0.66, Form("#sigma: %4.1f", sV));
	      
	      if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
	      if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
	}

	tl->DrawLatex(0.1, 0.92, "Vcal Threshold Trimmed");

	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 2
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Bump Map
	TH2D *hbm;

	c1->cd(5);
	gStyle->SetOptStat(0);
	hbm = (TH2D*)f->Get(Form("vcals_xtalk_C%i", chipId));
        
        if (hbm) {

	  h2->SetTitle("");
	  h2->GetZaxis()->SetRangeUser(minThrDiff, maxThrDiff);
	  h2->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Bump Bonding Problems");
	}

	else { ++nRootFileProblems; }

	// -- Bump Map
	c1->cd(6);  
	gPad->SetLogy(1);
	//gStyle->SetOptStat(1);
	h1 = (TH1D*)f->Get(Form("vcals_xtalk_C%iDistribution", chipId));

        if (h1) {
  	  h1->SetTitle("");
	  h1->GetXaxis()->SetRangeUser(-50., 50.);
	  h1->GetYaxis()->SetRangeUser(0.5, 5.0*h1->GetMaximum());
	  h1->DrawCopy();
	  tl->DrawLatex(0.1, 0.92, "Bump Bonding");
	
	} else { 
	  
	  ++nRootFileProblems; 
	}
	
	// -- Trim bits
	int trimbitbins(3);
	c1->cd(7); 
	gPad->SetLogy(1);
	h1 = (TH1D*)f->Get(Form("TrimBit14_C%i", chipId));
	if (h1) {
	  h1->SetTitle("");
	  h1->SetAxisRange(0., 60.);
	  h1->SetMinimum(0.5);
	  h1->Draw("");
	  tl->DrawLatex(0.1, 0.92, "Trim Bit Test");          
	}
	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit13_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kRed);
	  h1->Draw("same");
	}
	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit11_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kBlue);
	  h1->Draw("same");
	}
	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit7_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kGreen);
	  h1->Draw("same");
	}	
	else { ++nRootFileProblems; }
	
	// -- For numerics and titels see at end


	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 3
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Address decoding	
	// --------------------

	TH2D *ham;
	ham = (TH2D*)f1->Get(Form("AddressDecoding_C%i", chipId));

	c1->cd(9);
	gStyle->SetOptStat(0);
        if (ham) {

	  ham->SetTitle("");
	  ham->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Address decoding");
	}

	else { ++nRootFileProblems; }

	// -- Address levels

	c1->cd(10); 
	gPad->SetLogy(1);
	h1 = (TH1D*)f1->Get(Form("AddressLevels_C%i", chipId));
        if (h1) {
	  h1->SetTitle("");
	  h1->SetAxisRange(-1500., 1500.);
	  h1->Draw();
	  tl->DrawLatex(0.1, 0.92, "Address Levels");
	
	} else { 
	  
	  ++nRootFileProblems; 
	}


	// -- PHCalibration: Linear Fit (Gain & Pedesdtal)
	// -----------------------------------------------

	TH1D *hg = new TH1D("hg", "", 300, -2.0, 5.5);
	TH2D *hgm = new TH2D("hgm", "", 52, 0., 52., 80, 0., 80.);
	
	TH1D *hp = new TH1D("hp", "", 900, -300., 600.);
	hp->StatOverflows(kTRUE);

	TH1D *rp = new TH1D("rp", "", 900, -300., 600.);
	rp->StatOverflows(kFALSE);

	TH1D *htmp;

	float par0, par1, par2, par3, par4, par5; // Parameters of Vcal vs. Pulse Height Fit

	float mG(0.), sG(0.), nG(0.), nG_entries(0.);
	float mP(0.), sP(0.), nP(0.), nP_entries(0.); 
	over = 0.; under = 0.;

	float ped, gain;
	int a,b;
	
	int mPbin(0), xlow(-100), xup(255), extra(0);       // for restricted RMS
	float pedMin(0), pedMax(1000);
	double integral(0.);

	sprintf(string, "%s/phCalibrationFit_C%i.dat", dirName, chipId);
	phLinearFile = fopen(string, "r");

	if (!phLinearFile) {
	  
	  printf("chipSummary> !!!!!!!!!  ----> phCal: Could not open file %s to read fit results\n", string);
	
	} else {
		
	  for (int i = 0; i < 2; i++) fgets(string, 200, phLinearFile);
	  
	  for (int icol = 0; icol < 52; ++icol)	{
	    for (int irow = 0; irow < 80; ++irow) {
	      fscanf(phLinearFile, "%e %e %e %e %e %e %s %2i %2i", 
		     &par0, &par1, &par2, &par3, &par4, &par5, string, &a, &b);
	      
	      if (par2 != 0.)  {  // dead pixels have par2 == 0.
		
		gain = 1./par2;
		ped = par3;
		hp->Fill(ped);
		hg->Fill(gain);
		hgm->SetBinContent(icol + 1, irow + 1, gain);
	      }
	    }
	  }
	  
	  fclose(phLinearFile);
	  

	  // -- Gain

	  c1->cd(11);

	  mG =  hg->GetMean();
	  sG =  hg->GetRMS();
	  nG =  hg->Integral(hg->GetXaxis()->GetFirst(), hg->GetXaxis()->GetLast());
	  nG_entries = hg->GetEntries();
	  
	  under = hg->GetBinContent(0);
	  over  = hg->GetBinContent(hp->GetNbinsX()+1);
	  	  
	  gPad->SetLogy(1);
	  hg->GetYaxis()->SetRangeUser(0.5, 5.0*hg->GetMaximum());
	  hg->Draw();
	  tl->DrawLatex(0.1, 0.92, "PH Calibration: Gain (ADC/DAC)");
	  
	  if ( hg->GetMean() > 1.75 ) {

	    ts->DrawLatex(0.15, 0.80, Form("N: %4.0f", nG));
	    ts->DrawLatex(0.15, 0.72, Form("#mu: %4.2f", mG));
	    ts->DrawLatex(0.15, 0.64, Form("#sigma: %4.2f", sG));

	  } else {

	    ts->DrawLatex(0.65, 0.80, Form("N: %4.0f", nG));
	    ts->DrawLatex(0.65, 0.72, Form("#mu: %4.2f", mG));
	    ts->DrawLatex(0.65, 0.64, Form("#sigma: %4.2f", sG));
	  }
	  
	    
	  if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
	  if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
	  
	  c1->cd(15);

	  hgm->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "PH Calibration: Gain (ADC/DAC)");

	  // -- Pedestal

	  c1->cd(12);

	  mP =  hp->GetMean();
	  sP =  hp->GetRMS();
	  nP =  hp->Integral(hp->GetXaxis()->GetFirst(), hp->GetXaxis()->GetLast());
	  nP_entries = hp->GetEntries();
	  
	  if ( nP > 0 ) {
	    
	    // -- restricted RMS
	    integral = 0.;
	    mPbin = -1000; xlow = -1000; xup = 1000;
	    over = 0.; under = 0.;
	    
	    mPbin = hp->GetXaxis()->FindBin(mP);
	    
	    for (int i = 0; integral <  pedDistr; i++) { 
		    
	      xlow = mPbin-i;
	      xup =  mPbin+i;
	      integral = hp->Integral(xlow, xup)/nP;
		    
	    }
		  
	    extra = xup - xlow;
	  }
	  else {

	    xlow = -300; xup = 600; extra = 0;
	    over = 0.; under = 0.;
	  }

  	  under = hp->Integral(0, xlow - extra);
  	  over  = hp->Integral(xup + 1.5*extra, hp->GetNbinsX());
		  
	  hp->GetXaxis()->SetRange(xlow - extra, xup + 1.5*extra);

	  nP    = hp->Integral(hp->GetXaxis()->GetFirst(), hp->GetXaxis()->GetLast());

	  pedMin = hp->GetBinCenter(xlow-extra);
	  pedMax = hp->GetBinCenter(xup+1.5*extra);


	  cout<< " ========> Ped min  " << pedMin << " Ped max " << pedMax 
	      << ", over: " << over << " under: " << under << endl;		

	  hp->DrawCopy();

	  rp->Add(hp);
	  rp->GetXaxis()->SetRange(xlow, xup);

	  mP =  rp->GetMean();
	  sP =  rp->GetRMS();

	  // box->DrawBox( rp->GetBinCenter(xlow), 0, rp->GetBinCenter(xup), 1.05*rp->GetMaximum());
	  rp->SetFillColor(kRed);
	  rp->SetFillStyle(3002);
	  rp->Draw("same");
	  line->DrawLine(rp->GetBinCenter(xlow), 0, rp->GetBinCenter(xlow), 0.6*rp->GetMaximum());
	  line->DrawLine(rp->GetBinCenter(xup),  0, rp->GetBinCenter(xup),  0.6*rp->GetMaximum());
	 
	  tl->DrawLatex(0.1, 0.92, "PH Calibration: Pedestal (DAC)");
		
	  if ( hp->GetMean() < 126. ) {

	    ts->DrawLatex(0.65, 0.82, Form("N: %4.0f", nP));
	    ts->SetTextColor(kRed);
	    ts->DrawLatex(0.65, 0.74, Form("#mu: %4.1f", mP));
	    ts->DrawLatex(0.65, 0.66, Form("#sigma: %4.1f", sP));
				
	  } else {

	    ts->DrawLatex(0.16, 0.82, Form("N: %4.0f", nP));
	    ts->SetTextColor(kRed);
	    ts->DrawLatex(0.16, 0.74, Form("#mu: %4.1f", mP));
	    ts->DrawLatex(0.16, 0.66, Form("#sigma: %4.1f", sP));
	  }

	  if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
	  if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
	  ts->SetTextColor(kBlack);
	  

		
	}

	
	// -- PHCalibration: Tanh Fit (Parameter1)
	// ----------------------------------------
	
	c1->cd(11);

	over = 0.; under = 0.;

	float nPar1(0.), nPar1_entries(0.), mPar1(0.), sPar1(0.);
	
        TH1D *hPar1 = new TH1D("par1", "", 350, -1., 6.);

	sprintf(string, "%s/phCalibrationFitTan_C%i.dat", dirName, chipId);
	phTanhFile = fopen(string, "r");
	
	if (!phTanhFile) {
	  
	  printf("chipSummary> !!!!!!!!!  ----> phCal: Could not open file %s to read fit results\n", string);
	
	} else {
	  
	  for (int i = 0; i < 2; i++) fgets(string, 200, phTanhFile);
	  
	  for (int icol = 0; icol < 52; ++icol) {
	    for (int irow = 0; irow < 80; ++irow) {
	      
	      fscanf(phTanhFile, "%e %e %e %e %s %2i %2i", &par0, &par1, &par2, &par3, string, &a, &b);		
	      hPar1->Fill(par1);
	    }
	  }
	  
	  fclose(phTanhFile);	


	  // -- Parameter 1

	  hPar1->SetLineColor(kBlue);
	  hPar1->Draw("same");
	  
	  mPar1 =  hPar1->GetMean();
	  sPar1 =  hPar1->GetRMS();
	  nPar1 =  hPar1->Integral(hPar1->GetXaxis()->GetFirst(), hPar1->GetXaxis()->GetLast());	
	  nPar1_entries = hPar1->GetEntries();
	  
	  under = hPar1->GetBinContent(0);
	  over  = hPar1->GetBinContent(hPar1->GetNbinsX()+1);
	  
	  ts->SetTextColor(kBlue);
	  
	  if ( hg->GetMean() > 1.75 ) {
	  
	    ts->DrawLatex(0.15, 0.40, "Par1:");
	    ts->DrawLatex(0.15, 0.30, Form("N: %4.0f", nPar1));
	    ts->DrawLatex(0.15, 0.22, Form("#mu: %4.2f", mPar1));
	    ts->DrawLatex(0.15, 0.14, Form("#sigma: %4.2f", sPar1));
	  
	  } else {

	    ts->DrawLatex(0.65, 0.40, "Par1:");
	    ts->DrawLatex(0.65, 0.30, Form("N: %4.0f", nPar1));
	    ts->DrawLatex(0.65, 0.22, Form("#mu: %4.2f", mPar1));
	    ts->DrawLatex(0.65, 0.14, Form("#sigma: %4.2f", sPar1));
	  }
	  
	    
	  if ( under ) ts->DrawLatex(0.15, 0.48, Form("<= %i", under));			               
	  if ( over  ) ts->DrawLatex(0.75, 0.48, Form("%i =>", over ));
	  ts->SetTextColor(kBlack);
	}



	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 4
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Trim Bits
	// ----------

	TH2D *htm = new TH2D("htm", "", 80, 0., 80., 52, 0., 52.);

	c1->cd(13);
	
	gStyle->SetOptStat(0);
	h2 = (TH2D*)f->Get(Form("TrimMap_C%i;8", chipId));

        if (h2) {
	  for (int icol = 0; icol < 52; ++icol) {
	    for (int irow = 0; irow < 80; ++irow) {
	      
	      htm->SetBinContent(irow+1, icol+1, h2->GetBinContent(icol+1, irow+1));
	    }
	  }
  	  h2->SetTitle("");
	  h2->GetZaxis()->SetRangeUser(0., 16.);
	  h2->Draw("colz");
	}

	else { ++nRootFileProblems; }

	tl->DrawLatex(0.1, 0.92, "Trim Bits");


	FILE *tCalFile;
	sprintf(string, "%s/../T-calibration/TemperatureCalibration_C%i.dat", dirName, chipId);
	tCalFile = fopen(string, "r");
	char tCalDir[200];
	sprintf(tCalDir, "%s/../T-calibration", dirName);

	if ( tCalFile ) {
	
	  analyse(tCalDir, chipId);
	}
	else {

	  c1->cd(14);
	  TGraph *graph = (TGraph*)f->Get(Form("TempCalibration_C%i", chipId));
	  if ( graph ) { graph->Draw("A*"); }
	  else { ++nRootFileProblems; }
	  tl->DrawLatex(0.1, 0.92, "Temperature calibration");
	}



	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// -- Count defects and double counting
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	float fl0, fl1, fl2, fl3, fl4, fl5, tmp;
	int   i1, i2;
	char hname[200];

//  	TH2D *get = 0, *hget = 0, *htb0 = 0, *htb1 = 0, *htb2 = 0, *htb3 = 0, *htb4 = 0;

//  	for (int i = 1; i < 6; ++i) {
	  
//  	  get = (TH2D*)f->Get(Form("CalThresholdMap_C%i;%i", chipId, i));

//  	  if (get) {
//  	    hget = (TH2D*)get->Clone();
//  	    hget->SetName(Form("TB0C%i", i));

//  	    if (i == 1) htb0 = hget;
//  	    if (i == 2) htb1 = hget;
//  	    if (i == 3) htb2 = hget;
//  	    if (i == 4) htb3 = hget;
//  	    if (i == 5) htb4 = hget;

//  	  }
//  	}


 	TH2D *htb[5];
 	for (int i = 0; i < 5; ++i) {

 	  htb[i] = (TH2D*)f->Get(Form("CalThresholdMap_C%i;%i", chipId, i+1));
	  htb[i]->SetName(Form("tbC%i%i", chipId, i+1));
 	}

	TH2D *htthr = 0;
 	htthr = (TH2D*)f->Get(Form("VcalThresholdMap_C%d;8", chipId));

	sprintf(string, "%s/SCurve_C%i.dat", dirName, chipId);
	sCurveFile = fopen(string, "r");

	sprintf(string, "%s/phCalibrationFit_C%i.dat", dirName, chipId);
	phLinearFile = fopen(string, "r");
	
	sprintf(string, "%s/phCalibrationFitTan_C%i.dat", dirName, chipId);
	phTanhFile = fopen(string, "r");
	
	if (sCurveFile)   for (int i = 0; i < 2; i++) fgets(string, 200, sCurveFile);
	if (phLinearFile) for (int i = 0; i < 2; i++) fgets(string, 200, phLinearFile);
	if (phTanhFile)   for (int i = 0; i < 2; i++) fgets(string, 200, phTanhFile);

	int px_counted    = 0;
	int px_funct_counted    = 0;
	int px_perf_counted    = 0;
	int trim_counted    = 0;
	int ph_counted    = 0;
	float tb_diff = 0;
	float tb, tb0;
	    
	for (int icol = 0; icol < 52; ++icol) {
	  for (int irow = 0; irow < 80; ++irow) {
	    
	    pixel_alive   = 1;
	    px_funct_counted = 0;
	    px_perf_counted = 0;
	    px_counted = 0;

	    trim_counted = 0;
	    ph_counted = 0;
	    
	    // -- Pixel alive
	    if (hpm && hpm->GetBinContent(icol+1, irow+1)  == 0)  { 
	      
	      pixel_alive = 0; 
	      
	      ++nDeadPixel;   
	      cout << Form("chipSummary> dead pixel %3d %3d: %7.5f", 
			   icol, irow, hpm->GetBinContent(icol+1, irow+1)) << endl;
	    }

	    if (hpm && hpm->GetBinContent(icol+1,irow+1)  > 10) { ++nNoisy1Pixel; px_counted = 1; px_funct_counted = 1;}
	    if (hpm && hpm->GetBinContent(icol+1, irow+1)  < 0) { ++nMaskDefect;  px_counted = 1; px_funct_counted = 1;}

	    if (hpm && (hpm->GetBinContent(icol+1, irow+1) < 10) 
		  && (hpm->GetBinContent(icol+1, irow+1) > 0) ) { ++nIneffPixel;  px_counted = 1; px_funct_counted = 1;}
	    
	    
	    // -- Bump bonding
	    if ( pixel_alive && hbm ) {
	      if ( hbm->GetBinContent(icol+1, irow+1)  >= minThrDiff ) {

		if ( px_counted )      nDoubleCounts++;
		px_counted = 1;

		if ( px_funct_counted ) nDoubleFunctCounts++;
		px_funct_counted = 1;
		
		++nDeadBumps;	
		
		cout << Form("chipSummary> bump defect %3d %3d: %7.5f", 
			     icol, irow, hbm->GetBinContent(icol+1, irow+1)) << endl;
	      }
	    }
	    
	    
	    // -- Trim bits 1 - 4
	    if ( pixel_alive && htb[0] ) {

	      tb0 = htb[0]->GetBinContent(icol+1, irow+1);
	      
	      for ( int i = 1; i <= 4; i++ ) {

		if ( htb[i] ) {
		  
		  tb = htb[i]->GetBinContent(icol+1, irow+1);

		  tb_diff = TMath::Abs(tb-tb0);

		  if (tb_diff  <= 2) {
		    
		    if ( px_counted ) nDoubleCounts++;
		    px_counted = 1;
		    
		    if ( px_funct_counted ) nDoubleFunctCounts++;
		    px_funct_counted = 1;
		    
		    if ( trim_counted ) nDoubleTrims++;
		    trim_counted = 1;
		    
		    ++nDeadTrimbits;
		    
		    cout << Form("chipSummary> trim bit defect %3d %3d: %4.2f", icol, irow, tb_diff) << endl;
		  }
		}
	      }
	    }

	    // -- Address decoding
	    if (pixel_alive && ham) {

	      if( ham->GetBinContent(icol+1, irow+1) < 1 ) {

		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;
		    
		if ( px_funct_counted ) nDoubleFunctCounts++;
		px_funct_counted = 1;

		++nAddressProblems;

		cout << Form("chipSummary> address problem %3d %3d: %7.5f", 
			     icol, irow, ham->GetBinContent(icol+1, irow+1)) << endl;
	      }
	    }
	    
	    // -- Threshold
	    if (pixel_alive && htthr) {

	      if ( TMath::Abs(htthr->GetBinContent(icol+1, irow+1) - vcalTrim) > tthrTol ) {

		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;
		    
		if ( px_perf_counted ) nDoublePerfCounts++;
		px_perf_counted = 1;

		++nThrDefect;

		cout << Form("chipSummary> threshold problem %3d %3d: %7.5f", 
			     icol, irow, htthr->GetBinContent(icol+1, irow+1)) << endl;
	      }
	    }

	    // -- Noise
	    fscanf(sCurveFile, "%e %e %s %2i %2i", &fl1, &fl2, string, &i1, &i2);

	    if (pixel_alive) {
	      if ( (fl2 < noiseMin) 
		   || (fl2 > noiseMax) ) {
		
		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;
		    
		if ( px_perf_counted ) nDoublePerfCounts++;
		px_perf_counted = 1;
		
		++nNoisy2Pixel; 

		cout << Form("chipSummary> noise defect %3d %3d: %7.5f (%2i %2i)", 
			     icol, irow, fl2, i1, i2) << endl;
	      } 
	    }
	    
	    // -- Gain & Pedestal
	    fscanf(phLinearFile, "%e %e %e %e %e %e %s %2i %2i", 
		   &fl0, &fl1, &fl2, &fl3, &fl4, &fl5, string, &i1, &i2);

	    if (pixel_alive) {
	      
	      if (fl2 != 0) gain = 1./fl2;
	      ped = fl3;

	      if ( (gain < gainMin) || (gain > gainMax) ) {
		
		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;
		    
		if ( px_perf_counted ) nDoublePerfCounts++;
		px_perf_counted = 1;

		if ( ph_counted ) nDoublePHs++;
		ph_counted = 1;

		++nGainDefect;

		cout << Form("chipSummary> gain defect %3d %3d: %7.5f (%2i %2i)", 
			     icol, irow, gain, i1, i2) << endl;
	      }

	      if ( (ped < pedMin) 
		   || (ped > pedMax) ) {
		
		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;

		if ( px_perf_counted ) nDoublePerfCounts++;
		px_perf_counted = 1;

		if ( ph_counted ) nDoublePHs++;
		ph_counted = 1;

		++nPedDefect;

		cout << Form("chipSummary> pedestal defect %3d %3d: %7.5f (%2i %2i)", 
			     icol, irow, ped, i1, i2) << endl;
	      }
		
	      
	    }
	    

	    // -- Par1
	    fscanf(phTanhFile, "%e %e %e %e %s %2i %2i", 
		   &fl0, &fl1, &fl2, &fl3, string, &i1, &i2);
	    
	    if (pixel_alive && phTanhFile) {
	      
	      if ( (fl1 < par1Min) 
		   || (fl1 > par1Max) ) {
		
		if ( px_counted ) nDoubleCounts++;
		px_counted = 1;

		if ( px_perf_counted ) nDoublePerfCounts++;
		px_perf_counted = 1;

		if ( ph_counted ) nDoublePHs++;
		ph_counted = 1;

		++nPar1Defect;

		cout << Form("chipSummary> par1 defect %3d %3d: %7.5f (%2i %2i)", 
			     icol, irow, par1, i1, i2) << endl;
	      }
	    }
	    
	  }
	}
			
	fclose(sCurveFile); 
	fclose(phLinearFile);
	fclose(phTanhFile);
	      

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Numerics and Titles
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Compute the final verdict on this chip  //?? FIXME (below is pure randomness)
	float finalVerdict(0);
	if (nDeadTrimbits > 0)    finalVerdict += 1;
	if (nDeadPixel > 0)       finalVerdict += 10;
	if (nNoisy1Pixel > 0)     finalVerdict += 10;
	if (nAddressProblems > 0) finalVerdict += 10;
	if (nDeadBumps > 0)       finalVerdict += 100;
	if (nNoisy2Pixel > 0)     finalVerdict += 1000;
	if (nThrDefect > 0)       finalVerdict += 10000;
	if (nGainDefect > 0)      finalVerdict += 100000;
	if (nPedDefect > 0)       finalVerdict += 100000;
	if (nPar1Defect > 0)      finalVerdict += 100000;

	// -- Defects
	c1->cd(8);
	tl->SetTextSize(0.10);
	tl->SetTextFont(22);
	double y = 0.98;

	y -= 0.11;
	tl->DrawLatex(0.1, y, "Summary");
// 	tl->DrawLatex(0.6, y, Form("%06d", finalVerdict));

	tl->SetTextFont(132);
	tl->SetTextSize(0.09);
	y -= 0.11;
	tl->DrawLatex(0.1, y, Form("Dead Pixels: "));
	tl->DrawLatex(0.7, y, Form("%4d", nDeadPixel));

// 	y -= 0.10;
// 	tl->DrawLatex(0.1, y, Form("Noisy Pixels 1: "));
// 	tl->DrawLatex(0.7, y, Form("%4d", nNoisy1Pixel));
	
	y -= 0.10;
	tl->DrawLatex(0.1, y, "Mask defects: ");
	tl->DrawLatex(0.7, y, Form("%4d", nMaskDefect));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Dead Bumps: ");
	tl->DrawLatex(0.7, y, Form("%4d", nDeadBumps));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Dead Trimbits: ");
	tl->DrawLatex(0.7, y, Form("%4d", nDeadTrimbits));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Address Probl: ");
	tl->DrawLatex(0.7, y, Form("%4d", nAddressProblems));

	y -= 0.10;
	tl->DrawLatex(0.1, y, Form("Noisy Pixels 2: "));
	tl->DrawLatex(0.7, y, Form("%4d", nNoisy2Pixel));

	y -= 0.10;
	tl->DrawLatex(0.1, y, Form("Trim Probl.: "));
	tl->DrawLatex(0.7, y, Form("%4d", nThrDefect));

	y -= 0.10;
	tl->DrawLatex(0.1, y, Form("PH defects: "));
	tl->DrawLatex(0.5, y, Form("%4d/", nGainDefect));

	tl->SetTextColor(kRed);
	tl->DrawLatex(0.6, y, Form("%4d/",nPedDefect));
	tl->SetTextColor(kBlack);

	tl->SetTextColor(kBlue);
	tl->DrawLatex(0.7, y, Form("%4d",nPar1Defect));
	tl->SetTextColor(kBlack);

// 	y -= 0.10;
// 	tl->DrawLatex(0.1, y, Form("Par1 defect: "));
// 	tl->DrawLatex(0.7, y, Form("%4d", nPar1Defect));


	// -- Operation Parameters
	c1->cd(16);
	
	y = 0.92;
	tl->SetTextSize(0.10);
	tl->SetTextFont(22);
	y -= 0.11;
	tl->DrawLatex(0.1, y, Form("Op. Parameters"));

	tl->SetTextFont(132);
	tl->SetTextSize(0.09);

	y -= 0.11;
	int vana(-1.);
        vana = dac_findParameter(dirName, "Vana", chipId);
	tl->DrawLatex(0.1, y, "VANA: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3i DAC", vana));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int caldel(-1.);
        caldel = dac_findParameter(dirName, "CalDel", chipId);
	tl->DrawLatex(0.1, y, "CALDEL: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", caldel));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int vthrcomp(-1.);
        vthrcomp = dac_findParameter(dirName, "VthrComp", chipId);
	tl->DrawLatex(0.1, y, "VTHR: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", vthrcomp));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int vtrim(-1.);
        vtrim = dac_findParameter(dirName, "Vtrim", chipId);
	tl->DrawLatex(0.1, y, "VTRIM: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", vtrim));
	else tl->DrawLatex(0.7, y, "N/A");
	
	y -= 0.10;
	int ibias(-1.);
        ibias = dac_findParameter(dirName, "Ibias_DAC", chipId);
	tl->DrawLatex(0.1, y, "IBIAS_DAC: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", ibias));
	else tl->DrawLatex(0.7, y, "N/A");
       
	y -= 0.10;
	int voffset(-1.);
        voffset = dac_findParameter(dirName, "VoffsetOp", chipId);
	tl->DrawLatex(0.1, y, "VOFFSETOP: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", voffset));
	else tl->DrawLatex(0.7, y, "N/A");

	// -- Page title
	c1->cd(0);
	tl->SetTextSize(0.04);
	tl->SetTextFont(22);
	tl->DrawLatex(0.02, 0.97, Form("%s (C%i)", noslash.Data(), chipId));

	TDatime date;
	tl->SetTextSize(0.02);
	tl->DrawLatex(0.75, 0.97, Form("%s", date.AsString()));

	c1->SaveAs(Form("%s/chipSummary_C%i.ps", dirName, chipId));
	c1->SaveAs(Form("%s/C%i.png", dirName, chipId));
		

	// -- Dump into logfile
	ofstream OUT(Form("%s/summary_C%i.txt", dirName, chipId));
	OUT << "nDeadPixel: "         << nDeadPixel << endl;
	OUT << "nNoisy1Pixel: "       << nNoisy1Pixel << endl;
	OUT << "nDeadTrimbits: "      << nDeadTrimbits << endl;
	OUT << "nDeadBumps: "         << nDeadBumps << endl;
	OUT << "nMaskDefect: "        << nMaskDefect << endl;
	OUT << "nAddressProblems: "   << nAddressProblems << endl;
	OUT << "nNoisy2Pixel: "       << nNoisy2Pixel << endl;
	OUT << "nTThrDefect: "        << nThrDefect << endl;
	OUT << "nGainDefect: "        << nGainDefect << endl;
	OUT << "nPedDefect: "         << nPedDefect << endl;
	OUT << "nParDefect: "         << nPar1Defect << endl;
	OUT << "nDoubleCounts: "      << nDoubleCounts << endl;
	OUT << "nDoubleFunctCounts: " << nDoubleFunctCounts << endl;
	OUT << "nDoublePerfCounts: "  << nDoublePerfCounts << endl;
	OUT << "nDoubleTrims: "       << nDoubleTrims << endl;
	OUT << "nDoublePHs: "         << nDoublePHs << endl;
        OUT << "nRootFileProblems: "  << nRootFileProblems << endl;
	OUT << "SCurve "              << nN_entries    << " " << mN << " " << sN << endl;
	OUT << "Threshold "           << nV_entries    << " " << mV  << " " << sV << endl;
	OUT << "Gain "                << nG_entries    << " " << mG << " " << sG << endl;
	OUT << "Pedestal "            << nP_entries    << " " << mP << " " << sP << endl;
	OUT << "Parameter1 "          << nPar1_entries << " " << mPar1 << " " << sPar1 << endl;
	OUT.close();
	
}
// ----------------------------------------------------------------------
void moduleSummary(const char *dirName = "", const char *module_type) 
{

  printf("\nmoduleSummary> Starting ...\n");

  nChips = 16;
  startChip = 0;
	
  if ( !strcmp(module_type,"a") ) {
    
    nChips = 8; 
    startChip = 0; 
  }
  
  if ( !strcmp(module_type,"b") ) {
    
    nChips = 8; 
    startChip = 8; 
  }

  sprintf(fname, "%s/%s", dirName, fileName);
  inputFile = fopen(fname, "r");
  if (!inputFile) { 

    printf("\nmoduleSummary> ----> COULD NOT FIND %s IN DIRECTORY %s\n", fileName, dirName);
    printf("moduleSummary> ----> Aborting execution of moduleSummaryPage.C ... \n\n", fileName, dirName);   
    break; 
  }

  
  sprintf(fname, "%s/%s", dirName, adFileName);
  inputFile = fopen(fname, "r");
  if (!inputFile) {
    
    sprintf(adFileName,"%s", fileName); 
  }
  else {
    
    printf("moduleSummary> ----> found separate address decoding file: %s\n", adFileName); 
    fclose (inputFile); 
  }
  
  sprintf(fname, "%s/../../macros/criteria.dat", dirName);
  if ( !readCriteria(fname) ) {  
    
    printf("\nmoduleSummary> ----> COULD NOT READ GRADING CRITERIA !!!\n");
    printf("moduleSummary> ----> Aborting execution of moduleSummaryPage.C ... \n\n", fileName, dirName);  
    break;
  }

  TFile *f = new TFile(Form("%s/%s", dirName, fileName));

  gROOT->SetStyle("Plain");
  
  gStyle->SetPalette(1);
  gStyle->SetOptStat(0);
  gStyle->SetTitle(0);

  gStyle->SetStatFont(132);
  gStyle->SetTextFont(132);
  gStyle->SetLabelFont(132, "X");
  gStyle->SetLabelFont(132, "Y");
  gStyle->SetLabelSize(0.08, "X");
  gStyle->SetLabelSize(0.08, "Y");
  gStyle->SetTitleSize(0.08, "X");
  gStyle->SetTitleSize(0.08, "Y");
  gStyle->SetNdivisions(10, "X");
  gStyle->SetNdivisions(8, "Y");
  gStyle->SetTitleFont(132);
    
  gROOT->ForceStyle();
  
  tl = new TLatex;
  tl->SetNDC(kTRUE);
  tl->SetTextSize(0.1);
  
  ts = new TLatex;
  ts->SetNDC(kTRUE);
  ts->SetTextSize(0.09);

  line = new TLine;
  line->SetLineColor(kRed);
  line->SetLineStyle(kDashed);

  box = new TBox;
  box->SetFillColor(3);
  box->SetFillStyle(3004);

  c1 = new TCanvas("c1", "", 900, 700);
  c1->Clear();
  c1->Divide(1,4);
    
  int EColor[6]        = { 4, 8, 6, 1 };  
  int EMarkerStyle[10] = { 4, 25, 26, 23, 21, 27, 28, 20, 30, 29 };

  TH2D *mThreshold = new TH2D("mThreshold", "", 416, 0., 416., 160, 0., 160.);
  TH2D *mBumps     = new TH2D("mBumps",     "", 416, 0., 416., 160, 0., 160.);
  TH2D *mAddr      = new TH2D("mAddr",      "", 416, 0., 416., 160, 0., 160.);  

  double mThresholdmin(0.), mThresholdmax(255.);


  const int nfit = 4;  
  TString fitNames[] = {TString("Noise"), TString("Vcal Thr. Width"), TString("Rel. Gain Width"), TString("Pedestal Spread")};

  float limitB[] = { noiseB, trimmingB, gainB, pedestalB };   // limit for grading
  float limitC[] = { noiseC, trimmingC, gainC, pedestalC };   // limit for grading
  float max[]   = { noiseB + 100., trimmingB + 100., gainB + 0.05, pedestalB + 1000. };    // scaling of histogram

  TH1D *fit[nfit];
  TH1D *fitEntries[nfit];

  for(int ifit = 0; ifit < nfit; ++ifit) {

    fit[ifit] = new TH1D(Form("%s", fitNames[ifit].Data()),"", nChips, float(startChip), float(startChip+nChips));
    fitEntries[ifit] = new TH1D(Form("n%s", fitNames[ifit].Data()),"", nChips, float(startChip), float(startChip+nChips));

    fit[ifit]->SetLineColor(EColor[ifit]);
    fit[ifit]->SetMarkerColor(EColor[ifit]);
    fit[ifit]->SetMarkerStyle(EMarkerStyle[ifit]);
    fit[ifit]->SetMarkerSize(0.5);
  }
 
  for (int i = startChip; i < startChip+nChips; i++) { addVcalThreshold(dirName, i, mThreshold); }
  for (int i = startChip; i < startChip+nChips; i++) { addChip("vcals_xtalk", i, mBumps); }

  TFile *f1 = new TFile(Form("%s/%s", dirName, adFileName));
  for (int i = startChip; i < startChip+nChips; i++) { addChip("AddressDecoding", i, mAddr);}

  if ( nChips < 16 && startChip == 0 ) { 
    for (int i = 8; i < nChips+8; i++) { removeChip(i, mThreshold, -99); }
    for (int i = 8; i < nChips+8; i++) { removeChip(i, mBumps, -99); }
    for (int i = 8; i < nChips+8; i++) { removeChip(i, mAddr, -99); }
  }

  if ( nChips < 16 && startChip == 8 ) { 
    for (int i = 0; i < nChips; i++) { removeChip(i, mThreshold, -99); }
    for (int i = 0; i < nChips; i++) { removeChip(i, mBumps, -99); }
    for (int i = 0; i < nChips; i++) { removeChip(i, mAddr, -99); }
  }

  TString noslash(dirName);
  noslash.ReplaceAll("/", "");
  noslash.ReplaceAll("..", "");
  
  c1->cd(1);

  if ( mThreshold->GetMaximum() < mThresholdmax ) { 
    mThresholdmax = mThreshold->GetMaximum();
  }
  if ( mThreshold->GetMinimum() > mThresholdmin ) {
    mThresholdmin = mThreshold->GetMinimum();
  }
  mThreshold->GetZaxis()->SetRangeUser(mThresholdmin,mThresholdmax);
  mThreshold->DrawCopy("colz");
  tl->DrawLatex(0.1, 0.92, "Vcal threshold");
  tl->DrawLatex(0.75, 0.92, Form("%s",noslash.Data()));

  if ( nChips < 16 && startChip == 0 ) { 
   
    box->SetFillColor(29);
    box->DrawBox( 0, 0,  416,  80);
  }

  if ( nChips < 16  && startChip == 8 ) { 
   
    box->SetFillColor(29);
    box->DrawBox( 0, 80,  416,  160);
  }



  c1->cd(2);
  mBumps->SetMaximum(2.);
  mBumps->SetMinimum(-2.);  
  mBumps->DrawCopy("colz");
  tl->DrawLatex(0.1, 0.92, "Bump bonding map");

  if ( nChips < 16 && startChip == 0 ) { 
   
    box->SetFillColor(29);
    box->DrawBox( 0, 0,  416,  80);
  }

  if ( nChips < 16 && startChip == 8 ) { 
   
    box->SetFillColor(29);
    box->DrawBox( 0, 80,  416,  160);
  }
  
  c1_3->Divide(3,1);
  c1_3->cd(1);
  gPad->SetBottomMargin(0.2);
  gPad->SetLogy(1);
  gPad->SetLeftMargin(0.20);
  gPad->SetRightMargin(0.01);
  
  float V, A;
  float x_V[250], y_A[250];
  int i(0); 
  float iv100(0.);
  float iv150(0.);
  float iv150_17(0.);
  float iv100_17(0.);
  float variation(0.);
  float variation_17(0.);
  
  FILE *ivFile, *sumWrite, *sumRead, *gradWrite; 
    
  sprintf(fname, "%s/iv.dat", dirName);
  ivFile = fopen(fname, "r");
  
  if (!ivFile)
  {
    printf("moduleSummary> !!!!!!!!!  ----> Could not open file %s to read data\n", fname);
  }
  
  else {
  
    fclose(ivFile);
    ifstream is(fname);
    
    char  buffer[200];
    
    while (is.getline(buffer, 200, '\n')) {
      
      // check that line starts with a number
      if (buffer[0] != '1' && buffer[0] != '2' && buffer[0] != '3' && buffer[0] != '4' && 
	  buffer[0] != '5' && buffer[0] != '6' && buffer[0] != '7' && buffer[0] != '8' && buffer[0] != '9'  )  {continue;} 
      
      sscanf(buffer, "%e %e", &V, &A);
      
      x_V[i] = V;
      y_A[i] = 1e6*A;
      
      if ( i > 0 ) {
	
        // check that voltage is increasing & find current at 150 V 
        if ( x_V[i] < x_V[i-1] ) { continue; }
	if ( x_V[i] >= 100. && x_V[i-1] <= 100. ) { iv100 = y_A[i-1] + (100. - x_V[i-1])*(y_A[i] - y_A[i-1])/(x_V[i] - x_V[i-1]); }
	if ( x_V[i] >= 150. && x_V[i-1] <= 150. ) { iv150 = y_A[i-1] + (150. - x_V[i-1])*(y_A[i] - y_A[i-1])/(x_V[i] - x_V[i-1]); }
	
      }
      
      i++;
      
    }
 
 
    if ( iv100 != 0. ) { variation = iv150/iv100; }
    else               { variation = 0; }

    if ( i > 0 ) {
    
      TGraph *g1 = new TGraph(i,x_V,y_A);

      g1->Draw("aC");
      g1->SetTitle("");
      g1->SetLineColor(4);
      g1->SetLineWidth(2);
    
      g1->GetXaxis()->SetTitle("Voltage [V]");
      g1->GetYaxis()->SetTitle("Current [#muA]");
      g1->GetYaxis()->SetDecimals();
      g1->GetYaxis()->SetTitleOffset(1.2);
      g1->GetYaxis()->CenterTitle();
  
      tl->DrawLatex(0.2, 0.92, "I-V-Curve");
      ts->DrawLatex(0.25, 0.78, Form("I(150 V) = %.2f #muA", iv150));
      ts->DrawLatex(0.25, 0.65, Form("I_{150}/I_{100} =  %.2f ", variation));

    }   
  }
    
  char mod[20] = noslash.Data(), waf[20] = "",  test[20] = "",tmon[20], trim[20], ph[20], cycl[20];
  int tday;
  int dp(0), dm(0), db(0), dt(0), da(0);
  int root(0), a(0), b(0), c(0); 
  int badRocs[3] = {0, 0, 0};
  char iv;
  float voltage, current; 
  float temp, tempSigma, sollTemp;
  float cyclMean, cyclSigma;
  char  string[1000];
  
  c1_3->cd(2);
  
  sprintf(fname, "%s/summaryTest.txt", dirName);
  sumRead = fopen(fname, "r");

  if (!sumRead)
  {
    printf("\nmoduleSummary> !!!!!!!!!  ----> File %s does not exist yet...\n", fname);
    printf("moduleSummary> !!!!!!!!!  ----> Module summary not complete!\n\n");
  }
  else
  {
    fgets(string, 200, sumRead);
    //   fscanf(sumRead, "%s %s", string, mod);
    fscanf(sumRead, "%s %s %s %s", string, string, waf, test);
    fscanf(sumRead, "%s %i %i %i %i %i", string, &dp, &dm, &db, &dt, &da);
    fscanf(sumRead, "%s %s %s %s %s %i %i %i", string, string, string, string, string, &a, &b, &c);
    badRocs[0]=a;  badRocs[1]=b;  badRocs[2]=c;
    fscanf(sumRead, "%s %s %i", string, string, &root);
    fscanf(sumRead, "%s %s %s %s %s %i %s %s", string, string, string, string, tmon, &tday, string, string);

    fgets(string, 200, sumRead);
    fscanf(sumRead, "%s %s", string, trim);
    fgets(string, 200, sumRead); 
    fscanf(sumRead, "%s %s", string, ph);
    fgets(string, 200, sumRead);
    
    fscanf(sumRead, "%s %f %f %s %f", string, &temp, &tempSigma, string, &sollTemp); 

    fscanf(sumRead, "%s %s %s %f %f", string, string, cycl, &cyclMean, &cyclSigma);

    fclose(sumRead);
    
    tl->SetTextSize(0.09);
    tl->SetTextFont(22);
    double y = 0.92;
    tl->DrawLatex(0.01, y, Form("Test Summary of %s     %s", waf, test));
    tl->SetTextFont(132); 
    tl->SetTextSize(0.09);

    y -= 0.16;
    tl->DrawLatex(0.01, y, "ROCs > 1% defects: ");
    tl->DrawLatex(0.5, y, Form("%i", badRocs[0]));
    
    y -= 0.12;
    tl->DrawLatex(0.01, y, Form("Dead Pixel: "));
    tl->DrawLatex(0.5, y, Form("%i", dp));

    y -= 0.11;
    tl->DrawLatex(0.01, y, "Mask Defects: ");
    tl->DrawLatex(0.5, y, Form("%i", dm));

    y -= 0.11;
    tl->DrawLatex(0.01, y, "Dead Bumps: ");
    tl->DrawLatex(0.5, y, Form("%i", db));

    y -= 0.11;
    tl->DrawLatex(0.01, y, "Dead Trimbits: ");
    tl->DrawLatex(0.5, y, Form("%i", dt));

    y -= 0.11;
    tl->DrawLatex(0.01, y, "Address Probl: ");
    tl->DrawLatex(0.5, y, Form("%i", da));

    y = 0.76;
    tl->DrawLatex(0.72, y, Form("Tested on:"));
    y -= 0.11;
    tl->DrawLatex(0.72, y, "Temp. [^{o}C]:  ");
    y -= 0.11;
    tl->DrawLatex(0.72, y, "Trim / phCal: ");
    y -= 0.11;
    tl->DrawLatex(0.72, y, "Therm. cycl.: ");
    y -= 0.11;
    tl->DrawLatex(0.72, y, "TBM1: ");
    y -= 0.11;
    tl->DrawLatex(0.72, y, "TBM2: ");

    c1_3->cd(3);
    y = 0.76;
    tl->DrawLatex(0.01, y, Form("%s %i", tmon, tday));
    
    y -= 0.11;
    tl->DrawLatex(0.01, y, Form("%.1f +- %.1f", temp, tempSigma));
              
    y -= 0.11;
    tl->DrawLatex(0.01, y, Form("%s / %s", trim, ph));

    y -= 0.11;
    tl->DrawLatex(0.01, y, Form("%s", cycl));

  }

  int result;
  int tbm1(1), tbm2(1);
  TParameter<int>* par;
  
  y -= 0.11;
  par = (TParameter<int>*)f->Get("TBM1");
  if (par)
  {
  	tbm1 = par->GetVal();
  	if (tbm1 == 0) tl->DrawLatex(0.01, y, "ok");   
  	else tl->DrawLatex(0.01, y, Form("Err%i", tbm1));   
  }

  y -= 0.11;
  par = (TParameter<int>*)f->Get("TBM2");
  if (par)
  {
	tbm2 = par->GetVal();
	if (tbm2 == 0) tl->DrawLatex(0.01, y, "ok");   
	else tl->DrawLatex(0.01, y, Form("Err%i", tbm2));   
  }


  // Convert current to currents at room temperature
  double Tk = 273.15;
  double egap = 1.12;
  double kB = 8.617343E-5;
  double tTest;
  // tTest = temp;  // --> averaged temperature
  tTest = sollTemp;
  
  double expnt  = egap*(1/(Tk+tTest) - 1/(Tk+17))/(2*kB);
  double fctr   = (Tk+17)*(Tk+17)/((Tk+tTest)*(Tk+tTest));
  
  iv150_17 = iv150*fctr*TMath::Exp(expnt);
  iv100_17 = iv100*fctr*TMath::Exp(expnt);
  if ( iv100_17 != 0 ) variation_17 = iv150_17/iv100_17;
    
  printf("\nmoduleSummary> converted I(150 V, %.0f C)    = %.4f       to  I(150 V, 17 C)    = %.4f \n", tTest, iv150, iv150_17);
  printf("moduleSummary> converted I(100 V, %.0f C)    = %.4f       to  I(100 V, 17 C)    = %.4f \n\n", tTest, iv100, iv100_17);
  
  if ( iv150_17 != 0 ) {
    c1_3->cd(3);
    y = 0.32;
    tl->DrawLatex(0.25, y, "I(150 V) [T = 17 ^{o}C]");
    tl->DrawLatex(0.72, y, Form("%.2f #muA", iv150_17));
    c1_3->cd(2);
    
  }

  if ( iv100_17 != 0 ) {
    c1_3->cd(3);
    y = 0.21;
    tl->DrawLatex(0.25, y, "I_{150}/I_{100}   [T = 17 ^{o}C]");
    tl->DrawLatex(0.72, y, Form("%.2f", variation_17));
    c1_3->cd(2);
  }

  sprintf(fname, "%s/summaryTest.txt", dirName);
  sumWrite = fopen(fname, "a");
  fputs(Form("TBM1 %i\n", tbm1), sumWrite);
  fputs(Form("TBM2 %i\n", tbm2), sumWrite);
  fputs(Form("I 150 %f \n", iv150_17), sumWrite);
  fputs(Form("I150/I100 %f \n", variation_17), sumWrite);
  fputs(Form("iv datapoints %i \n", i), sumWrite);

//   c1->cd(4);
//   mAddr->DrawCopy("colz");
//   mAddr->SetMaximum(1.);
//   mAddr->SetMinimum(0.);
//   tl->DrawLatex(0.1, 0.92, "Address decoding map");



  c1_4->Divide(4,1);
  qualification(dirName, fit, fitEntries);
  

  for (int i = 0; i < 4; i++) {

  // makePlot(TH1 *h, const char *title, int pad, double Ymin, double Ymax, double Ylimit)
    makePlot(fit[i], fitNames[i].Data(), i+1, 0, max[i], limitB[i], limitC[i]); 
  
  }

  int grad(0);

  FILE *missingData; 
  sprintf(fname, "%s/comment_3.txt", dirName);
  missingData = fopen(fname, "r");

  if ( missingData ) {

    printf("\nmoduleSummary> !!!!!!!!!  ----> Found file for missing data: comment_3.txt => GRADE C!\n\n");
    grad = 3;
    fclose(missingData);

  } else {
  
    grad = grading(badRocs, iv150_17, variation_17, fit, fitEntries, limitB, limitC, test);
  }
  
  c1_3->cd(3);    
  tl->SetTextSize(0.09);
  tl->SetTextFont(22);
 
  if (grad == 1) {  tl->DrawLatex(0.6, 0.92, "GRADE:  A");  fputs("Grade A\n", sumWrite); }
  if (grad == 2) {  tl->DrawLatex(0.6, 0.92, "GRADE:  B");  fputs("Grade B\n", sumWrite); }
  if (grad == 3) {  tl->DrawLatex(0.6, 0.92, "GRADE:  C");  fputs("Grade C\n", sumWrite); }  


  sprintf(fname, "%s/gradingTest.txt", dirName);
  gradWrite = fopen(fname, "a");
 
  if (!gradWrite)
  {
    printf("\nmoduleSummary> !!!!!!!!!  ----> File %s does not exist yet...\n", fname);
    printf("moduleSummary> !!!!!!!!!  ----> Grading data could not be written to file!\n\n");
  }
  else
  {

    fputs(Form("Noise %i %i\n", fitsProblemB[0], fitsProblemC[0]), gradWrite);
    fputs(Form("VcalThrWidth %i %i\n", fitsProblemB[1], fitsProblemC[1]), gradWrite);
    fputs(Form("RelGainWidth %i %i\n", fitsProblemB[2], fitsProblemC[2]), gradWrite);
    fputs(Form("PedSpread %i %i\n", fitsProblemB[3], fitsProblemC[3]), gradWrite);
    fputs(Form("I150V %i %i\n", currentProblemB, currentProblemC), gradWrite);
    fputs(Form("Iratio %i 0\n",  slopeProblemB), gradWrite);
  }
  
  c1->SaveAs(Form("%s/moduleSummary_%s%s.ps", dirName, waf, test));
  c1->SaveAs(Form("%s/%s%s.gif", dirName, waf, test));

  printf("\nmoduleSummary> ................................................ finished\n");
}
void plotXY(char* fname){

  // input: root file from GBL

  gStyle->SetOptStat(0);
  Double_t w = 1.2; // number of RMS for gaussian fit
  TString filename = fname;
  TCanvas *cTop = new TCanvas("cTop","Residuals in lab frame, TOP", 800, 1000);
  TCanvas *cBot = new TCanvas("cBot","Residuals in lab frame, BOTTOM",800, 1000);
  cTop->SetFillColor(kBlue-10); cTop->SetFillStyle(3001);
  cBot->SetFillColor(kMagenta-10); cBot->SetFillStyle(3001);
  Double_t halfWid = 38.34/2.;
  Double_t halfLen = 98.33/2.;
  TBox *module = new TBox(-halfLen, -halfWid, halfLen, halfWid);
  module->SetLineColor(kMagenta);
  module->SetFillStyle(0);

  Int_t ic=0;
  TFile *f = new TFile(filename.Data());
  // extract string 
  Int_t idx2 = filename.Index("/");
  Int_t idx1 = filename.Index("_");
  TString outn = filename(idx1+1,idx2-(idx1+1));
  TString outname ="predXY_"; outname += outn.Data(); outname += ".txt";
  TString topName ="predXY_"; topName += outn.Data(); topName += "_topO.gif";
  TString botName ="predXY_"; botName += outn.Data(); botName += "_botO.gif";
  //  ofstream outf(outname.Data());
  // top
  Int_t nrow = 13;
  Double_t space = 0.007;
  Double_t space1 = 0.03;
  Double_t y2 = 1./13*nrow-space1;
  Double_t y1 = 1./13*(--nrow)-space1;
  TPad *tp1 = new TPad("tp1","tp1",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp2 = new TPad("tp2","tp2",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *tp3 = new TPad("tp3","tp3",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp4 = new TPad("tp4","tp4",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *tp5 = new TPad("tp5","tp5",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp6 = new TPad("tp6","tp6",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  Double_t off = 0.12;
  off = 0.165;
  y1 -= space1; y2 -= space1;
  space1 *= 2;
  TPad *tp7 = new TPad("tp7","tp7",0.+off,y1,0.33+off,y2);
  TPad *tp8 = new TPad("tp8","tp8",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp9 = new TPad("tp9","tp9",0.+off,y1,0.33+off,y2);
  TPad *tp10 = new TPad("tp10","tp10",0.66-off,y1,1.-off,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *tp11 = new TPad("tp11","tp11",0.+off,y1,0.33+off,y2);
  TPad *tp12 = new TPad("tp12","tp12",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp13 = new TPad("tp13","tp13",0.+off,y1,0.33+off,y2);
  TPad *tp14 = new TPad("tp14","tp14",0.66-off,y1,1.-off,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *tp15 = new TPad("tp15","tp15",0.+off,y1,0.33+off,y2);
  TPad *tp16 = new TPad("tp16","tp16",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *tp17 = new TPad("tp17","tp17",0.+off,y1,0.33+off,y2);
  TPad *tp18 = new TPad("tp18","tp18",0.66-off,y1,1.-off,y2);
  cout << " TOP " << endl;

  cTop->cd();
  tp1->Draw();
  tp2->Draw();
  tp3->Draw();
  tp4->Draw();
  tp5->Draw();
  tp6->Draw();
  tp7->Draw();
  tp8->Draw();
  tp9->Draw();
  tp10->Draw();
  tp11->Draw();
  tp12->Draw();
  tp13->Draw();
  tp14->Draw();
  tp15->Draw();
  tp16->Draw();
  tp17->Draw();
  tp18->Draw();

  ic = 2;
  Int_t ipad=0;
  for(Int_t i=1; i<4; i++){
    TString hisname = "h_xy_module_L"; hisname  += i;
    hisname += "t_halfmodule_axial_sensor0";
    TString layer = "L"; layer+=i; layer+= "TA"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    TH1D *his = (TH1D*)his2->ProjectionY();
    ipad++;
    TString pd = "tp"; pd+=ipad;
    TPad *pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    Double_t low = his->GetMean()-w*his->GetRMS();
    Double_t up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up);
    his2->Draw("colz");
    module->Draw("same");
    TF1 *fit = his->GetFunction("gaus");
    // cout << "sensor " << i << " axial mean :  " << his->GetMean()*1000. << " - RMS : " << his->GetRMS()*1000. << " (um) " << " " << his->GetEntries() << endl;
    // cout << "sensor " << i << " axial mu :  " << fit->GetParameter(1)*1000. << " - sigma : " << fit->GetParameter(2)*1000. << " (um) " << endl;
    // outf << layer.Data() << " " << fit->GetParameter(1)*1000. << " " << fit->GetParameter(2)*1000. << " " << his->GetEntries() << endl;

    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "t_halfmodule_stereo_sensor0";
    layer = "L"; layer+=i; layer+= "TS"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    his = (TH1D*)his2->ProjectionY();
    ipad++;
    pd = "tp"; pd+=ipad;
    pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
  }
  //  ic = 19;
  for(Int_t i=4; i<7; i++){
    TString hisname = "h_xy_module_L"; hisname  += i;
    TString hisname2 = hisname;
    hisname += "t_halfmodule_axial_hole_sensor0";
    hisname2 += "t_halfmodule_axial_slot_sensor0";
    TH2D *hisOther = (TH2D*) f->Get(hisname2.Data());
    Double_t norma2 = hisOther->GetMaximum();
    TString layer = "L"; layer+=i; layer+= "TAHo"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    TH1D *his = (TH1D*)his2->ProjectionY();     
    Double_t norma1 = his2->GetMaximum();
    Double_t normamax = TMath::Max(norma1,norma2);
    his2->SetMaximum(normamax);
    ipad++;
    TString pd = "tp"; pd+=ipad;
    TPad *pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    Double_t low = his->GetMean()-w*his->GetRMS();
    Double_t up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    TF1 *fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname2 = hisname;
    hisname += "t_halfmodule_stereo_hole_sensor0";
    hisname2 += "t_halfmodule_stereo_slot_sensor0";
    TH2D *hisOther = (TH2D*) f->Get(hisname2.Data());
    norma2 = hisOther->GetMaximum();
    TString layer = "L"; layer+=i; layer+= "TAHo"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    TH1D *his = (TH1D*)his2->ProjectionY();     
    norma1 = his2->GetMaximum();
    normamax2 = TMath::Max(norma1,norma2);
    his2->SetMaximum(normamax2);
    layer = "L"; layer+=i; layer+= "TSHo"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    his = (TH1D*)his2->ProjectionY();
    ipad++; ipad++;
    pd = "tp"; pd+=ipad;
    pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "t_halfmodule_axial_slot_sensor0";
    layer = "L"; layer+=i; layer+= "TASl";
    his2 = (TH2D*) f->Get(hisname.Data());
    his2->SetMaximum(normamax);
    his = (TH1D*)his2->ProjectionY();     
    ipad--;
    pd = "tp"; pd+=ipad;
    pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "t_halfmodule_stereo_slot_sensor0";
    layer = "L"; layer+=i; layer+= "TSSl"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    his2->SetMaximum(normamax2);
    his = (TH1D*)his2->ProjectionY();     
    ipad++; ipad++;
    pd = "tp"; pd+=ipad;
    pad = (TPad*) cTop->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
  }
  cout << endl;
  cout << "------------------------" << endl;
  cout << endl;
  cout << " BOTTOM " << endl;
  nrow = 13;
  space = 0.007;
  space1 = 0.03;
  y2 = 1./13*nrow-space1;
  y1 = 1./13*(--nrow)-space1;
  TPad *bp1 = new TPad("bp1","bp1",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp2 = new TPad("bp2","bp2",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *bp3 = new TPad("bp3","bp3",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp4 = new TPad("bp4","bp4",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *bp5 = new TPad("bp5","bp5",0.33,y1,0.66,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp6 = new TPad("bp6","bp6",0.33,y1,0.66,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  off = 0.165;
  y1 -= space1; y2 -= space1;
  space1 *= 2;
  TPad *bp7 = new TPad("bp7","bp7",0.+off,y1,0.33+off,y2);
  TPad *bp8 = new TPad("bp8","bp8",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp9 = new TPad("bp9","bp9",0.+off,y1,0.33+off,y2);
  TPad *bp10 = new TPad("bp10","bp10",0.66-off,y1,1.-off,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *bp11 = new TPad("bp11","bp11",0.+off,y1,0.33+off,y2);
  TPad *bp12 = new TPad("bp12","bp12",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp13 = new TPad("bp13","bp13",0.+off,y1,0.33+off,y2);
  TPad *bp14 = new TPad("bp14","bp14",0.66-off,y1,1.-off,y2);
  y2 = y1-space; y1 = 1./13.*(--nrow)-space1;
  TPad *bp15 = new TPad("bp15","bp15",0.+off,y1,0.33+off,y2);
  TPad *bp16 = new TPad("bp16","bp16",0.66-off,y1,1.-off,y2);
  y2 = y1; y1 = 1./13.*(--nrow)-space1;
  TPad *bp17 = new TPad("bp17","bp17",0.+off,y1,0.33+off,y2);
  TPad *bp18 = new TPad("bp18","bp18",0.66-off,y1,1.-off,y2);

  cBot->cd();
  bp1->Draw();
  bp2->Draw();
  bp3->Draw();
  bp4->Draw();
  bp5->Draw();
  bp6->Draw();
  bp7->Draw();
  bp8->Draw();
  bp9->Draw();
  bp10->Draw();
  bp11->Draw();
  bp12->Draw();
  bp13->Draw();
  bp14->Draw();
  bp15->Draw();
  bp16->Draw();
  bp17->Draw();
  bp18->Draw();

  cBot->cd();
  bp1->Draw();
  bp2->Draw();
  bp3->Draw();
  bp4->Draw();
  bp5->Draw();
  bp6->Draw();
  bp7->Draw();
  bp8->Draw();
  bp9->Draw();
  bp10->Draw();
  bp11->Draw();
  bp12->Draw();
  bp13->Draw();
  bp14->Draw();
  bp15->Draw();
  bp16->Draw();
  bp17->Draw();
  bp18->Draw();

  ic=0;
  ipad = 0;
  for(Int_t i=1; i<4; i++){
    TString hisname = "h_xy_module_L"; hisname  += i;
    TString hisname2 = hisname;
    hisname += "b_halfmodule_stereo_sensor0";
    hisname2 += "b_halfmodule_axial_sensor0";
    TH2D *hisOther = (TH2D*) f->Get(hisname2.Data());
    Double_t norma2 = hisOther->GetMaximum();
    TString layer = "L"; layer+=i; layer+= "BS"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    Double_t norma1 = his2->GetMaximum();
    Double_t normamax = TMath::Max(norma1,norma2);
    //    his2->SetMaximum(normamax);
    TH1D *his = (TH1D*)his2->ProjectionY();     
    if(layer.Contains("1")){his->SetFillColor(kRed);}
    else if(layer.Contains("2")){his->SetFillColor(kOrange);}
    else if(layer.Contains("3")){his->SetFillColor(kYellow);}
    else if(layer.Contains("4")){his->SetFillColor(kGreen);}
    else if(layer.Contains("5")){his->SetFillColor(kCyan);}
    else if(layer.Contains("6")){his->SetFillColor(kBlue);}
    if(layer.Contains("A")){his->SetFillStyle(3007);}
    else if(layer.Contains("S")){his->SetFillStyle(3004);}
    //    cBot->cd(++ic);
    ipad++;
    TString pd = "bp"; pd+=ipad;
    TPad *pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    Double_t low = his->GetMean()-w*his->GetRMS();
    Double_t up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    TF1 *fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "b_halfmodule_axial_sensor0";
    layer = "L"; layer+=i; layer+= "BA"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    //   his2->SetMaximum(normamax);
    his = (TH1D*)his2->ProjectionY();     
    ipad++;
    pd = "bp"; pd+=ipad;
    pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
  }
  for(Int_t i=4; i<7; i++){
    TString hisname = "h_xy_module_L"; hisname  += i;
    TString hisname2 = hisname;
    hisname += "b_halfmodule_stereo_hole_sensor0";
    hisname2 += "b_halfmodule_stereo_slot_sensor0";
    TH2D *hisOther = (TH2D*) f->Get(hisname2.Data());
    Double_t norma2 = hisOther->GetMaximum();
    TString layer = "L"; layer+=i; layer+= "BSHo"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    TH1D *his = (TH1D*)his2->ProjectionY();     
    Double_t norma1 = his2->GetMaximum();
    Double_t normamax1 = TMath::Max(norma1,norma2);
    his2->SetMaximum(normamax1);
    ipad++; 
    TString pd = "bp"; pd+=ipad;
    TPad *pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    Double_t low = his->GetMean()-w*his->GetRMS();
    Double_t up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    TF1 *fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    TString hisname2 = hisname;
    hisname += "b_halfmodule_axial_hole_sensor0";
    hisname2 += "b_halfmodule_axial_slot_sensor0";
    TH2D *hisOther = (TH2D*) f->Get(hisname2.Data());
    Double_t norma2 = hisOther->GetMaximum();
    TString layer = "L"; layer+=i; layer+= "BAHo"; 
    TH2D *his2 = (TH2D*) f->Get(hisname.Data());
    TH1D *his = (TH1D*)his2->ProjectionY();     
    Double_t norma1 = his2->GetMaximum();
    Double_t normamax2 = TMath::Max(norma1,norma2);
    his2->SetMaximum(normamax2);
    ipad++; ipad++;
    pd = "bp"; pd+=ipad;
    pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "b_halfmodule_stereo_slot_sensor0";
    layer = "L"; layer+=i; layer+= "BSSl"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    his2->SetMaximum(normamax1);
    his = (TH1D*)his2->ProjectionY();     
    ipad--;
    pd = "bp"; pd+=ipad;
    pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
    hisname = "h_xy_module_L"; hisname  += i;
    hisname += "b_halfmodule_axial_slot_sensor0";
    layer = "L"; layer+=i; layer+= "BASl"; 
    his2 = (TH2D*) f->Get(hisname.Data());
    his2->SetMaximum(normamax2);
    his = (TH1D*)his2->ProjectionY();     
    ipad++; ipad++;
    pd = "bp"; pd+=ipad;
    pad = (TPad*) cBot->GetListOfPrimitives()->FindObject(pd.Data());
    pad->cd();
    low = his->GetMean()-w*his->GetRMS();
    up = his->GetMean()+w*his->GetRMS();    
    his->Fit("gaus","Q0","",low,up); his2->Draw("colz");
    module->Draw("same");
    fit = his->GetFunction("gaus");
  }

  //  outf.close();
  cTop->SaveAs(topName.Data());
  cBot->SaveAs(botName.Data());

}
Exemple #6
0
// -----------------------------------------------------------------------------
//
void talk() {
  
  time_t start = TTimeStamp().GetSec();

  bool draw = true;
  bool debug = true;
  
  // Binning
  int xbins = 100;
  int ybins = 100;
  double xmin = 0.0;
  double xmax = 1.0;
  double ymin = 0.0;
  double ymax = 1.0;
  double xbin_centre = ( ( xmax - xmin ) / xbins ) / 2.;
  double ybin_centre = ( ( ymax - ymin ) / ybins ) / 2.;
  
  // AlphaT values
  const int nat = 1;
  double at[nat];
  for ( int ii = 0; ii < nat; ++ii ) { at[ii] = 0.55 + ii * 0.001; } 
  
  // HT regions
  const int nht = 3;
  double ht_min[nht] = { 250., 300., 350. };

  // Jet pT thresholds
  double pt1_min[nht] = { 71.4, 85.7, 100. };
  double pt2_min[nht] = { 71.4, 85.7, 100. };
  double pt3_min[nht] = { 35.7, 42.9., 50. };

  // x fractions
  double x1_min[nht];
  double x2_min[nht];
  double x3_max[nht];
  for ( int ii = 0; ii < nht; ++ii ) { x1_min[ii] = ( 2. * pt1_min[ii]) / ( ht_min[ii] + pt3_min[ii] ); }
  for ( int ii = 0; ii < nht; ++ii ) { x2_min[ii] = ( 2. * pt2_min[ii]) / ( ht_min[ii] + pt3_min[ii] ); }
  for ( int ii = 0; ii < nht; ++ii ) { x3_max[ii] = ( 2. * pt3_min[ii]) / ( ht_min[ii] + pt3_min[ii] ); }
  
  // Loop through bins 
  int loop = 0;
  int nloops = nht;
  for ( int iht = 0; iht < nht; ++iht ) {
    std::cout << "Completed " 
	      << 100.*float(loop)/float(nloops) 
	      << "%..." 
	      << std::endl; 
    loop++;
    
    // Labeling
    std::stringstream ss;
    ss << "HT" << int(ht_min[iht]);
	  
    // New canvas for plots
    TCanvas* c1 = 0;
    if (draw) c1 = new TCanvas( TString("Canvas"+ss.str()), "" );
	  
    // Pad for cross-section plot
    TPad* pad = 0;
    if (draw) pad = new TPad(TString("Pad"+ss.str()),"",0.,0.,1.,1.);
    if (pad) {
      pad->SetGrid();
      pad->Draw();
      pad->cd();
      pad->SetLogz();
    }
    TH1F* hr = 0;
    if (draw) hr = pad->DrawFrame(0.,0.,1.,1.);

    // Histo title
    if (hr) {
      std::stringstream sss;
      sss << "H_{T}=" << ht_min[iht]
	  << "(p_{T1},p_{T2},p_{T3})=" 
	  << pt1_min[iht] << ","
	  << pt2_min[iht] << ","
	  << pt3_min[iht] << ")"
	  << ", (x_{1},x_{2},x_{3})=" 
	  << x1_min[iht] << ","
	  << x2_min[iht] << ","
	  << x3_max[iht] << ")";
      hr->SetTitle( sss.str().c_str() );
      hr->GetXaxis()->SetTitle( "x_{2}" );
      hr->GetYaxis()->SetTitle( "x_{1}" );
    }
	  
    // Create 2D cross-section plot
    TH2D* his = 0;
    if (draw) his = new TH2D(TString("Histo"+ss.str()),"",
			     xbins,xmin,xmax,
			     ybins,ymin,ymax);
	  
    // Fill 2D cross-section plot
    for ( int xbin = 0; xbin < xbins; ++xbin ) { 
      for ( int ybin = 0; ybin < ybins; ++ybin ) { 
	double x2 = ( ( xmax - xmin ) / xbins ) * xbin + xmin;
	double x1 = ( ( ymax - ymin ) / ybins ) * ybin + ymin;
	double val = ( x1*x1 + x2*x2 ) / ( ( 1 - x1 ) * ( 1 - x2 ) ); 
	      
	if ( !constrain( x1, x2, x3 ) ) { continue; }
	      
	if ( x1 < x1_cut[ix1] ) { continue; }
	      
	double alpha_t =  x2 / ( 2 * sqrt(x1+x2-1) );
	if (his) his->Fill( x2+xbin_centre, x1+ybin_centre, val ); 
      }
    }
	  
    // Draw 2D cross-section plot
    gStyle->SetPalette(1);
    if (his) {
      his->SetMaximum( his->GetMaximum() );
      his->SetMinimum( his->GetMinimum(1.e-12) );
      his->Draw("COLZsame");
    }

    // Pad for AlphaT contours
    if (c1) c1->cd();
    TPad* overlay = 0;
    if (draw) overlay = new TPad(TString("Overlay"+ss.str()),"",0.,0.,1.,1.);
    if (overlay) {
      overlay->SetFillStyle(4000);
      overlay->SetFillColor(0);
      overlay->SetFrameFillStyle(4000);
      overlay->Draw();
      overlay->cd();
    }
    TH1F* hframe = 0;
    if (draw) overlay->DrawFrame(pad->GetUxmin(),
				 pad->GetUymin(),
				 pad->GetUxmax(),
				 pad->GetUymax());
	  
    // Graphs of AlphaT contours
    TMultiGraph* mg = 0;
    if (draw) {
      mg = new TMultiGraph();
      for ( Int_t iat = 0; iat < nat; iat++ ) {
	Double_t alpha_t = at[iat];
	const Int_t n_ = 100;
	Double_t x1_[n_];
	Double_t x2_[n_];
	for ( Int_t j = 0; j < 100; j++ ) {
	  x2_[j] = j*0.01;
	  Double_t temp = ( x2_[j] - 2. * alpha_t * alpha_t ) / ( 2. * alpha_t );
	  x1_[j] = temp * temp + 1 - alpha_t * alpha_t;
	}
	TGraph* gr = new TGraph(n_,x2_,x1_);
	mg->Add(gr,"l");
      }
      mg->Draw();
    }
	  
    if (c1) c1->cd();
    if (c1) c1->SaveAs(TString(ss.str()+".png"));
	  
  }

}
Exemple #7
0
// -----------------------------------------------------------------------------
//
void rob() {

  // Binning
  
  int xbins = 100;
  int ybins = 100;
  
  double xmax = 1.0;
  double xmin = 0.0;
//   double xrange = xmax - xmin;
//   xmax += xrange / xbins / 2;
//   xmin -= xrange / xbins / 2;
//   //xbins++;
  
  double ymax = 1.0;
  double ymin = 0.0;
//   double yrange = ymax - ymin;
//   ymax += yrange / ybins / 2;
//   ymin -= yrange / ybins / 2;
//   //ybins++;

  std::cout << " Binning: "
	    << " xbins: " << xbins
	    << " ybins: " << xbins
	    << " xmin: " << xmin
	    << " xmax: " << xmax
	    << " ymin: " << ymin
	    << " ymax: " << ymax
	    << std::endl;
  
  // Examples values of pt1, pt2, mht, x1, x2, x3, sigma and alpha_t
//   double pt1 = 50.;
//   double pt2 = 50.;
//   double mht = 50.;
//   double x1 = ( 2. * pt1 ) / ( pt1 + pt2 + mht );
//   double x2 = ( 2. * pt2 ) / ( pt1 + pt2 + mht );
//   double x3 = 2 - x1 - x2;
//   double sigma =  ( x1*x1 + x2*x2 ) / ( (1-x1) * (1-x2) );
//   double alpha_t =  x2 / ( 2 * sqrt(x1+x2-1) );
//   std::cout << " pt1: " << pt1
// 	    << " pt2: " << pt2
// 	    << " mht: " << mht 
// 	    << " x1: " << x1
// 	    << " x2: " << x2
// 	    << " x3: " << x3
// 	    << " sigma: " << sigma
// 	    << " alpha_t: " << alpha_t
// 	    << std::endl;
  
  // Cross section
  TCanvas* c1 = new TCanvas( "Contours", "" );
  //c1->SetGridx(1);
  //c1->SetGridy(1);

  TPad* pad = new TPad("pad","",0.,0.,1.,1.);
  pad->SetGrid();
  pad->Draw();
  pad->cd();
  pad->SetLogz(1);

  TH1F* hr = pad->DrawFrame(0.,0.,1.,1.);
  
  const int nx = 3;
  const int ny = 10;
  double pt[nx] = { 30., 50., 100. };
  double ht[ny];
  for ( int ii = 0; ii < ny; ++ii ) { ht[ii] = 150. + ii * 50.; }
  double ratio[nx][ny];
  
  double xbin_centre = ( ( xmax - xmin ) / xbins ) / 2.;
  double ybin_centre = ( ( ymax - ymin ) / ybins ) / 2.;
  TH2D* his = new TH2D("Contours","",xbins,xmin,xmax,ybins,ymin,ymax);
  for ( int ii = 0; ii < nx; ++ii ) {
    for ( int jj = 0; jj < ny; ++jj ) {
      double x3 = ( 2. * pt[ii] ) / ( ht[jj] + pt[ii] );
      double n = 0.;
      double d = 0.;
      for ( int xbin = 0; xbin < xbins; ++xbin ) { 
	for ( int ybin = 0; ybin < ybins; ++ybin ) { 
	  double x2 = ( ( xmax - xmin ) / xbins ) * xbin + xmin;
	  double x1 = ( ( ymax - ymin ) / ybins ) * ybin + ymin;
	  double val = ( x1*x1 + x2*x2 ) / ( ( 1 - x1 ) * ( 1 - x2 ) ); 
	  if ( x1 < x2 ||      // jet ordering by Pt
	       x1 + x2 > 2. || // from relation "x1 + x2 + x3 = 2"
	       x1 > 1.0 ||     // from "lost jet" and relation "xmiss = -x1 -x2"
	       x1 + x2 < 1.    // from "lost jet" and relation "xmiss = -x1 -x2"
	       ) { continue; }
	  if ( ( x1 + x2 ) < ( 2 - x3 ) ) { continue; }
	  d += val;
	  double alpha_t =  x2 / ( 2 * sqrt(x1+x2-1) );
	  if ( alpha_t > 0.5 ) n+= val;
	  if ( ii == 2 && jj == 9 ) { his->Fill( x2+xbin_centre, x1+ybin_centre, val ); }
	}
      }
      double r = 0.;
      if ( d > 0. ) { r = n/d; }
      ratio[ii][jj] = r;
      std::cout << " Pt: " << pt[ii]
		<< " ht: " << ht[jj]
		<< " x3: " << x3
		<< " r: " << r
		<< " n: " << n
		<< " d: " << d
		<< std::endl;
    }
  }
  gStyle->SetPalette(1);
  his->SetMaximum( his->GetMaximum() );
  his->SetMinimum( his->GetMinimum(1.e-12) );
  his->Draw("COLZsame");
  

//   c1->cd();
//   TPad* overlay = new TPad("overlay","",0.,0.,1.,1.);
//   overlay->SetFillStyle(4000);
//   overlay->SetFillColor(0);
//   overlay->SetFrameFillStyle(4000);
//   overlay->Draw();
//   overlay->cd();

//   Double_t pxmin = pad->GetUxmin();
//   Double_t pymin = pad->GetUymin();
//   Double_t pxmax = pad->GetUxmax();
//   Double_t pymax = pad->GetUymax();
//   TH1F* hframe = overlay->DrawFrame(pxmin,pymin,pxmax,pymax);

//   TMultiGraph* mg = new TMultiGraph();
//   for ( Int_t i = 0; i < 10; i++ ) {
//     Double_t alpha_t = 0.5 + i*0.01;
//     const Int_t n_ = 100;
//     Double_t x1_[n_];
//     Double_t x2_[n_];
//     for ( Int_t j = 0; j < 100; j++ ) {
//       x2_[j] = j*0.01;
//       Double_t temp = ( x2_[j] - 2. * alpha_t * alpha_t ) / ( 2. * alpha_t );
//       x1_[j] = temp * temp + 1 - alpha_t * alpha_t;
//     }
//     TGraph* gr = new TGraph(n_,x2_,x1_);
//     mg->Add(gr,"l");
//   }
//   mg->Draw();


//   TCanvas* c2 = new TCanvas( "Contours1", "" );
//   c2->cd();
//   TMultiGraph* mg2 = new TMultiGraph();
//   for ( Int_t ii = 0; ii < 3; ii++ ) {
//     TGraph* gr = new TGraph(ny,ht,ratio[ii]);
//     mg2->Add(gr,"l");
//   }
//   mg2->Draw("a");
  
//   c1->cd();
//   c1->SaveAs("c1.png");
//   c2->cd();
//   c2->SaveAs("c2.png");

}
int main(int argc, char *argv[])
{
  ApplyLHCbStyle();
  double maxrate;
  switch(argc)
  {
    case 2:
      maxrate=-1;
      break;
    case 3:
      if(atoi(argv[2]))
        maxrate=atoi(argv[2]);
      else
      {
        cout << "Argument not a number" << endl;
        return 1;
      }
      break;
    default:
      cout << "Usage: " << argv[0] << " <Filename> [<Max rate>]" << endl;
      return 1;
  }
  if(system("[ -a figs ]") != 0)
  {
    cout << "Directory ./figs/ does not exist" << endl;
    const int mkdirerr = system("mkdir figs");
    if(mkdirerr != 0)
    {
      cout << "Error code " << mkdirerr << " while creating ./figs/" << endl;
    }
    else
    {
      cout << "Directory ./figs/ created" << endl;
    }
  }
  TFile* file = new TFile(argv[1]);
  if(!file->IsOpen())
  {
    cout << "Exiting" << endl;
    return 1;
  }
  TTree* tree = (TTree*)file->Get("metatree");
  int iHV; tree->SetBranchAddress("HV", &iHV);
  vector<int> HVs;
  string HV;
  int n = tree->GetEntries();
  TCanvas* mapscan, * histcan, * graphcan, * indivgraphcan[4];
  TPad* mapspad[4], * histpad[4];
  TH1D* hist;
  TH2D* map;
  string title[4] = {"JB", "JT", "ST", "SB"};
//  string title[4] = {"FA0026", "DA0024", "FA0019", "FA0006"};
  TGraphErrors* graph[4];
  TMultiGraph* mg;
  TLegend* lg;
  float xlo, xhi, ylo, yhi;
//  rainbowgradient();
  heatmapgradient();
//  DarkBodyRadiator();
//  DarkBodyRadiator2();
//  redbluegradient();
//  heatmapRB();
  string plotname;
  stringstream plotnamestream;
  double* x[4], * y[4], * xe[4], * ye[4];
  for(int j = 0; j < 4; j++)
  {
    x[j]  = new double[n];
    y[j]  = new double[n];
    xe[j] = new double[n];
    ye[j] = new double[n];
  }

  Double_t maxima[4];
  cout << "Entering loop of " << n << " events." << endl;
  for(int i = 0; i < n; i++)
  {
    tree->GetEntry(i);
    HVs.push_back(iHV);
    HV = itoa(iHV);
    for(int j = 0; j < 4; j++)
    {
      map = (TH2D*)file->Get(("DarkMap"+HV+"V"+itoa(j)).c_str());
      maxima[j] = map->GetMaximum();
    }
  }
  sort(HVs.begin(), HVs.end());
  if(maxrate==-1)
  {
    for(int i = 1; i < 4; i++)
    {
      maxrate = TMath::Max(maxima[i-1], maxima[i]);
      maxima[i]=maxrate;
    }
  }
//  cout << "Plotting with a maximum rate of " << maxrate << endl;
  for(int i = 0; i < n; i++)
  {
    HV = itoa(HVs[i]);
    plotnamestream << "DarkRate" << setfill('0') << setw(4) << HVs[i];
    plotnamestream >> plotname;
    histcan = new TCanvas(plotname.c_str(), "", 800, 800);
    plotnamestream.clear();
    histcan->Draw();
    plotnamestream << "DarkMap" << setfill('0') << setw(4) << HVs[i];
    plotnamestream >> plotname;
    mapscan = new TCanvas(plotname.c_str(), "", 800, 800);
    plotnamestream.clear();
    mapscan->Draw();
    for(int j = 0; j < 4; j++)
    {
      // Set limits
      xlo = 0.5*((j/2)%2);
      xhi = xlo+0.5;
      ylo = 0.5*((j+((j/2)%2))%2);
      yhi = ylo+0.5;
      // 2D maps
      mapscan->cd();
      mapspad[j] = new TPad("", "", xlo, ylo, xhi, yhi);
      mapspad[j]->SetLeftMargin(  0.05);
      mapspad[j]->SetRightMargin( 0.15);
      mapspad[j]->SetTopMargin(   0.05);
      mapspad[j]->SetBottomMargin(0.05);
      mapspad[j]->Draw();
      mapspad[j]->cd();
      map = (TH2D*)file->Get(("DarkMap"+HV+"V"+itoa(j)).c_str());
      map->SetTitle("");
      map->SetMaximum(maxrate);
      map->SetMinimum(0);
      map->Draw("COLZ");
      // 1D histogram
      histcan->cd();
      histpad[j] = new TPad("", "", xlo, ylo, xhi, yhi);
      histpad[j]->SetLeftMargin(  0.05);
      histpad[j]->SetRightMargin( 0.15);
      histpad[j]->SetTopMargin(   0.05);
      histpad[j]->SetBottomMargin(0.05);
      histpad[j]->Draw();
      histpad[j]->SetLogy();
      histpad[j]->cd();
      hist = new TH1D(("hist"+itoa(i)+itoa(j)).c_str(), title[j].c_str(), 100, 0, maxrate);
      tree->Draw(("rate"+itoa(j)+">>"+hist->GetName()).c_str(), "", "", 1, i);
      // Graph
      x[j][i]  = HVs[i];
      xe[j][i] = 0;
      y[j][i]  = 0;
      ye[j][i] = 0;
      double yval = 0, yerr = 0;
      double npix = 0;
      for(int k = 0; k < 64; k++)
      {
        yval = map->GetBinContent((k/8)+1, (k%8)+1);
        yerr = map->GetBinError((k/8)+1, (k%8)+1);
        if(yval<0) continue;
        y[j][i]  += yval;
        ye[j][i] += yerr*yerr;
        npix++;
      }
      y[j][i]  /= npix;
      ye[j][i] = sqrt(ye[j][i])/npix;
      cout << npix << "\t" <<  y[j][i] << "±" << ye[j][i] << endl;
    }
    gStyle->SetOptStat("m");
    histcan->SaveAs(("./figs/"+(string)histcan->GetName()+".pdf").c_str());
    histcan->SaveAs(("./figs/"+(string)histcan->GetName()+".png").c_str());
    mapscan->SaveAs(("./figs/"+(string)mapscan->GetName()+".pdf").c_str());
    mapscan->SaveAs(("./figs/"+(string)mapscan->GetName()+".png").c_str());
  }
  mg = new TMultiGraph();
  lg = new TLegend(0.17,0.6,0.45,0.82);
  lg->SetFillStyle(0);
  lg->SetLineWidth(0);
  int linecolour[4] = {kBlue,kRed+1,kGreen+1,kViolet};
  int linestyle[4]  = {2,9,3,5};
  int index[4] = {1, 0, 2, 3}; // More sensible order
  for(int k = 0; k < 4; k++)
  {
    int j = index[k];
    graph[j] = new TGraphErrors(n, x[j], y[j], xe[j], ye[j]);
    graph[j]->SetTitle(title[j].c_str());
    graph[j]->SetLineWidth(2);
    indivgraphcan[j] = new TCanvas((title[j]+"graph").c_str(),"",1000,900);
    indivgraphcan[j]->Draw();
    graph[j]->GetXaxis()->SetTitle("High Voltage [V]");
    graph[j]->GetYaxis()->SetTitle("Dark count rate [Hz]");
    graph[j]->GetYaxis()->SetTitleOffset(1.25);
    graph[j]->SetMinimum(0);
    graph[j]->Draw("APL");
    indivgraphcan[j]->SaveAs(("./figs/graph"+title[j]+".pdf").c_str());
    graph[j]->SetLineColor(linecolour[j]);
    graph[j]->Fit("pol1");
    graph[j]->GetFunction("pol1")->SetLineColor(graph[j]->GetLineColor());
    graph[j]->GetFunction("pol1")->SetLineStyle(linestyle[j]);
    lg->AddEntry(graph[j]->GetFunction("pol1"),title[j].c_str(),"L");
    mg->Add(graph[j], "P");
  }
  graphcan = new TCanvas("graph", "", 900, 900);
  graphcan->cd();
  graphcan->Draw();
  mg->SetMinimum(0);
  mg->Draw("APL");
  mg->GetXaxis()->SetTitle("High Voltage [V]");
  mg->GetYaxis()->SetTitle("Dark count rate [Hz]");
  mg->GetXaxis()->SetTitleOffset(1.2);
  mg->GetYaxis()->SetTitleOffset(1.2);
  GetLHCbName()->Draw();
  lg->Draw();
  graphcan->SaveAs("figs/graph.pdf");

  cout << "\\hline" << endl << "Voltage";
  for(int j = 0; j < 4; j++)
  {
    cout << " & " << title[j];
  }
  cout << "\\\\\\hline" << endl;
  for(int i = 0; i < n; i++)
  {
    cout << HVs[i];
    for(int j = 0; j < 4; j++)
    {
      cout << setprecision(3) << " & " << y[j][i];
    }
    cout << "\\\\\\hline" << endl;
  }
  return 0;
}
void chipSummary(const char *dirName, int chipId, int TrimVcal)
{
	if (f && f->IsOpen()) f->Close();
	//	if (f1 && f1->IsOpen()) f1->Close();
	if (g && g->IsOpen()) g->Close();

	gROOT->SetStyle("Plain");
	gStyle->SetPalette(1);
	gStyle->SetOptStat(0);
	gStyle->SetTitle(0);

	gStyle->SetStatFont(132);
	gStyle->SetTextFont(132);
	gStyle->SetLabelFont(132, "X");
	gStyle->SetLabelFont(132, "Y");
	gStyle->SetLabelSize(0.08, "X");
	gStyle->SetLabelSize(0.08, "Y");
	gStyle->SetNdivisions(6, "X");
	gStyle->SetNdivisions(8, "Y");
	gStyle->SetTitleFont(132);

	gROOT->ForceStyle();

	tl = new TLatex;
	tl->SetNDC(kTRUE);
	tl->SetTextSize(0.09);

	ts = new TLatex;
	ts->SetNDC(kTRUE);
	ts->SetTextSize(0.08);

	line = new TLine;
	line->SetLineColor(kRed);
	line->SetLineStyle(kSolid);
	
	box = new TBox;
	box->SetFillColor(kRed);
	box->SetFillStyle(3002);

	f = new TFile(Form("%s/%s", dirName, fileName), "READ");
	
	//		if (strcmp(fileName, adFileName) == 0) f1 = f;
	// 	else f1 = new TFile(Form("%s/%s", dirName, adFileName), "READ");
	
	sprintf(trimFileName,"Trim%i.root",TrimVcal);
	if (strcmp(fileName, trimFileName) == 0) g = f;
	else g = new TFile(Form("%s/%s", dirName, trimFileName), "READ");
 
	//sprintf(fname, "%s/../../criteria.dat", dirName);
	sprintf(fname, "criteria.dat", dirName);
	if ( !readCriteria(fname) ) { 
	  
	  printf("\nchipSummary> ----> COULD NOT READ GRADING CRITERIA !!!");
	  printf("chipSummary> ----> Aborting execution of chipgSummaryPage.C ... \n\n", fileName, dirName);  
	  break;
	}

	TH1D *h1;
	TH2D *h2;

	c1 = new TCanvas("c1", "", 800, 800);
	c1->Clear();
	c1->Divide(4,4, 0.01, 0.04);

  //	shrinkPad(0.1, 0.1, 0.1, 0.3);


        TString noslash(dirName);
        noslash.ReplaceAll("/", " ");
        noslash.ReplaceAll(".. ", "");
	
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 1
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Dead pixels
	TH2D *hpm = new TH2D("hpm", "", 80, 0., 80., 52, 0., 52.);

	int nDeadPixel(0);
	int nMaskDefect(0);
	int nNoisyPixel(0);
	int nRootFileProblems(0);

	c1->cd(1);
	h2 = (TH2D*)f->Get(Form("PixelMap_C%i", chipId));
        if (h2) {
	  for (int icol = 0; icol < 52; ++icol)
	  {
		for (int irow = 0; irow < 80; ++irow)
		{
		        hpm->SetBinContent(irow+1, icol+1, h2->GetBinContent(icol+1, irow+1));

			if (h2->GetBinContent(icol+1, irow+1)  == 0)
			{
				++nDeadPixel;
			}
			if (h2->GetBinContent(icol+1, irow+1)  > 10)
			{
				++nNoisyPixel;
			}
			if (h2->GetBinContent(icol+1, irow+1)  < 0)
			{
				++nMaskDefect;
			}
		}
	  }
	  h2->SetTitle("");
	  h2->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Pixel Map");
	}

	else { ++nRootFileProblems; }

	// -- sCurve width and noise level
	TH1D *hw = new TH1D("hw", "", 100, 0., 600.);
	TH1D *hd = new TH1D("hd", "", 100, 0., 600.);  // Noise in unbonded pixel (not displayed)
	TH2D *ht = new TH2D("ht", "", 52, 0., 52., 80, 0., 80.);
	TH1D *htmp;

	float mN(0.), sN(0.), nN(0.), nN_entries(0.);
	int over(0), under(0);

	double htmax(255.), htmin(0.);
	
	float thr, sig;
	int a,b;
	FILE *inputFile;
	char string[200];
	sprintf(string, "%s/SCurve_C0%i.dat", dirName, TrimVcal);
	inputFile = fopen(string, "r");

	double minThrDiff(-5.);
	double maxThrDiff(5.);
	h2 = (TH2D*)f->Get(Form("vcals_xtalk_C%i", chipId));
        
	if (!inputFile)
	{
		printf("chipSummary> !!!!!!!!!  ----> SCurve: Could not open file %s to read fit results\n", string);
	}
	else
	{
	  for (int i = 0; i < 2; i++) fgets(string, 200, inputFile);

		for (int icol = 0; icol < 52; ++icol)
		{
			for (int irow = 0; irow < 80; ++irow)
			{
				fscanf(inputFile, "%e %e %s %2i %2i", &thr, &sig, string, &a, &b);  //comment
//  				printf("chipSummary> sig %e thr %e\n", sig, thr);
				hw->Fill(sig);
				thr = thr / 65;

				ht->SetBinContent(icol+1, irow+1, thr); 
			
				if ( h2 ) {
				  if( h2->GetBinContent(icol+1, irow+1)  > minThrDiff)
				  {
				    hd->Fill(sig);
				  }
				}
			}
		}
		c1->cd(2);
		hw->Draw();
		tl->DrawLatex(0.1, 0.92, "S-Curve widths: Noise (e^{-})");
		

		/*		c1->cd(15);
		hd->SetLineColor(kRed);
		hd->Draw();
		tl->DrawLatex(0.1, 0.92, "S-Curve widths of dead bumps");
		if ( hd->GetEntries() > 0 ) {
		  ts->DrawLatex(0.55, 0.82, Form("entries: %4.0f", hd->GetEntries()));
		  ts->DrawLatex(0.55, 0.74, Form("#mu:%4.2f", hd->GetMean()));
		  ts->DrawLatex(0.55, 0.66, Form("#sigma: %4.2f", hd->GetRMS()));
		}
		*/

		mN =  hw->GetMean();
		sN =  hw->GetRMS();
		nN =  hw->Integral(hw->GetXaxis()->GetFirst(), hw->GetXaxis()->GetLast());
		nN_entries =  hw->GetEntries();

		under = hw->GetBinContent(0);
		over  = hw->GetBinContent(hw->GetNbinsX()+1);


		ts->DrawLatex(0.65, 0.82, Form("N: %4.0f", nN));
		ts->DrawLatex(0.65, 0.74, Form("#mu: %4.1f", mN));
		ts->DrawLatex(0.65, 0.66, Form("#sigma: %4.1f", sN));
			
		if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
		if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));

		c1->cd(3);
		if ( ht->GetMaximum() < htmax ) { 
		  htmax = ht->GetMaximum();
		}
		if ( ht->GetMinimum() > htmin ) {
		  htmin = ht->GetMinimum();
		}
		ht->GetZaxis()->SetRangeUser(htmin,htmax);
		ht->Draw("colz");
		tl->DrawLatex(0.1, 0.92, "Vcal Threshold from SCurve");
	}

	// -- Noise level map
	c1->cd(4);
        gPad->SetLogy(1);
 	gStyle->SetOptStat(1);
	
	float mV(0.), sV(0.), nV(0.), nV_entries(0.);
	over = 0.; under = 0.;

	if (!g->IsZombie())
	{
	      h1 = (TH1D*)g->Get(Form("VcalThresholdMap_C%iDistribution;7", chipId));
              if (h1) {
		h1->SetTitle("");
		h1->SetAxisRange(0., 100.);
		h1->Draw();

		mV = h1->GetMean();
		sV = h1->GetRMS();
		nV = h1->Integral(h1->GetXaxis()->GetFirst(), h1->GetXaxis()->GetLast());
		nV_entries = h1->GetEntries();

		under = h1->GetBinContent(0);
		over  = h1->GetBinContent(h1->GetNbinsX()+1);
              }
              else {

	        ++nRootFileProblems;
		mV = 0.;
		sV = 0.;
               
              }

	      ts->DrawLatex(0.15, 0.82, Form("N: %4.0f", nV));
	      ts->DrawLatex(0.15, 0.74, Form("#mu: %4.1f", mV));
	      ts->DrawLatex(0.15, 0.66, Form("#sigma: %4.1f", sV));
	      
	      if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
	      if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
	}

	tl->DrawLatex(0.1, 0.92, "Vcal Threshold Trimmed");
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 2
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Bump Map
	TH2D *hbm = new TH2D("hbm", "", 80, 0., 80., 52, 0., 52.);

	int nDeadBumps(0);

	c1->cd(5);
	gStyle->SetOptStat(0);
	h2 = (TH2D*)f->Get(Form("vcals_xtalk_C%i", chipId));
        
        if (h2) {

	  for (int icol = 0; icol < 52; ++icol)
	  {
		for (int irow = 0; irow < 80; ++irow)
		{
		        hbm->SetBinContent(irow+1, icol+1, h2->GetBinContent(icol+1, irow+1));

			if ( h2->GetBinContent(icol+1, irow+1)  >= minThrDiff )
			{
			   cout << Form("chipSummary> dead %3d %3d: %7.5f", icol, irow, h2->GetBinContent(icol, irow)) << endl;
				++nDeadBumps;
			}
		}
	  }

	  h2->SetTitle("");
	  h2->GetZaxis()->SetRangeUser(minThrDiff, maxThrDiff);
	  h2->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Bump Bonding Problems");
	}

	else { ++nRootFileProblems; }

	// -- Bump Map
	c1->cd(6);  
	gPad->SetLogy(1);
	//gStyle->SetOptStat(1);
	h1 = (TH1D*)f->Get(Form("vcals_xtalk_C%iDistribution", chipId));
        if (h1) {
  	  h1->SetTitle("");
	  h1->SetAxisRange(-50., 50.); //DOES NOT WORK!!!???
	  h1->Draw();
	  tl->DrawLatex(0.1, 0.92, "Bump Bonding");
	}
	
	else { ++nRootFileProblems; }
	
	// -- Trim bits
	int trimbitbins(3);
	int nDeadTrimbits(0);
	c1->cd(7); 
	gPad->SetLogy(1);
	h1 = (TH1D*)f->Get(Form("TrimBit14_C%i", chipId));
	if (h1) {
	  h1->SetTitle("");
	  h1->SetAxisRange(0., 60.);
	  h1->SetMinimum(0.5);
	  h1->Draw("");
	  tl->DrawLatex(0.1, 0.92, "Trim Bit Test");
	  for (int i = 1; i <= trimbitbins; ++i) nDeadTrimbits += h1->GetBinContent(i);
	}

	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit13_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kRed);
	  h1->Draw("same");
	  for (int i = 1; i <= trimbitbins; ++i) nDeadTrimbits += h1->GetBinContent(i);
	}

	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit11_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kBlue);
	  h1->Draw("same");
	  for (int i = 1; i <= trimbitbins; ++i) nDeadTrimbits += h1->GetBinContent(i);
	}

	else { ++nRootFileProblems; }

	h1 = (TH1D*)f->Get(Form("TrimBit7_C%i", chipId));
	if (h1) {
	  h1->SetLineColor(kGreen);
	  h1->Draw("same");
	  for (int i = 1; i <= trimbitbins; ++i) nDeadTrimbits += h1->GetBinContent(i);
	}
	
	else { ++nRootFileProblems; }
	
	// -- For numerics and titels see at end


	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 3
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Address decoding
	TH2D *ham = new TH2D("ham", "", 80, 0., 80., 52, 0., 52.);

	int nAddressProblems(0);
	
	c1->cd(9);
	gStyle->SetOptStat(0);
	h2 = (TH2D*)f->Get(Form("AddressDecoding_C%i", chipId));
        if (h2) {
	  for (int icol = 0; icol < 52; ++icol) {
	    for (int irow = 0; irow < 80; ++irow) {
	      
	      ham->SetBinContent(irow+1, icol+1, h2->GetBinContent(icol+1, irow+1));

	      if (h2 && h2->GetBinContent(icol+1, irow+1) < 1) {
	        cout << Form("chipSummary> address problem %3d %3d: %7.5f", icol, irow, h2->GetBinContent(icol, irow))
		     << endl;
	        ++nAddressProblems;
	      }
	    }
	  }
	  h2->SetTitle("");
	  h2->Draw("colz");
	  tl->DrawLatex(0.1, 0.92, "Address decoding");
	}

	else { ++nRootFileProblems; }

	// -- Address levels
	c1->cd(10); 
	gPad->SetLogy(1);
	h1 = (TH1D*)f->Get(Form("AddressLevels_C%i", chipId));
        if (h1) {
	  h1->SetTitle("");
	  h1->SetAxisRange(-1200., 1000.);
	  h1->Draw();
	  tl->DrawLatex(0.1, 0.92, "Address Levels");
	}

	else { ++nRootFileProblems; }
		
	// -- PHCalibration (Gain & Pedesdtal)

	TH1D *hg = new TH1D("hg", "", 250, -1., 5.5);
	TH2D *hgm = new TH2D("hgm", "", 52, 0., 52., 80, 0., 80.);
	TH1D *hp1 = new TH1D("hp", "", 100,0,5);	
	TH1D *hp = new TH1D("hp", "", 900, -600., 600.);
	hp->StatOverflows(kTRUE);
	TH1D *rp = new TH1D("rp", "", 900, -600., 600.);
	rp->StatOverflows(kFALSE);

	TH1D *htmp;
	
	float mG(0.), sG(0.), nG(0.), nG_entries(0.);
	float mP(0.), sP(0.), nP(0.), nP_entries(0.); 
	over = 0.; under = 0.;

	float par0, par1, par2, par3, par4, par5; // Parameters of Vcal vs. Pulse Height Fit
	float ped, gain;
	int a,b;
	
	int mPbin(0), xlow(-100), xup(255), extra(0);       // for restricted RMS
	double integral(0.);


	FILE *inputFile;
	char string[200];  
	sprintf(string, "%s/phCalibrationFitTan_C0%i.dat", dirName, TrimVcal);
	inputFile = fopen(string, "r");

	if (!inputFile)
	{
		printf("chipSummary> !!!!!!!!!  ----> phCal: Could not open file %s to read fit results\n", string);
	}
	else
	{
		for (int i = 0; i < 2; i++) fgets(string, 200, inputFile);

		for (int icol = 0; icol < 52; ++icol)
		{
			for (int irow = 0; irow < 80; ++irow)
			{
			  //	fscanf(inputFile, "%e %e %e %e %e %e %s %2i %2i", &par0, &par1, &par2, &par3, &par4, &par5, string, &a, &b);
			  fscanf(inputFile, "%e %e %e %e %s %2i %2i", &par0, &par1, &par2, &par3, string, &a, &b);

				if (par2 != 0.)  // dead pixels have par2 == 0.
				{
				  // ped = -par3/par2;
					gain = 1./par2;
					//	ped = par3;
					ped=par3+par2*(tanh(-par1));
					hp->Fill(ped);
					hg->Fill(gain);
					hp1->Fill(par1);
					//	cout <<gain<<" " << ped<<endl;
					hgm->SetBinContent(icol + 1, irow + 1, gain);
				}
			}
		}

		mG =  hg->GetMean();
		sG =  hg->GetRMS();
		nG =  hg->Integral(hg->GetXaxis()->GetFirst(), hg->GetXaxis()->GetLast());
		nG_entries = hg->GetEntries();

		under = hg->GetBinContent(0);
		over  = hg->GetBinContent(hp->GetNbinsX()+1);
		
		c1->cd(11);

		//	hg->Draw();
		tl->DrawLatex(0.1, 0.92, "PH Calibration: Gain (ADC/DAC)");
		
		if ( hg->GetMean() > 1.75 ) {
		  ts->DrawLatex(0.15, 0.82, Form("N: %4.0f", nG));
		  ts->DrawLatex(0.15, 0.74, Form("#mu: %4.2f", mG));
		  ts->DrawLatex(0.15, 0.66, Form("#sigma: %4.2f", sG));

		  if ( under ) ts->DrawLatex(0.15, 0.82, Form("<= %i", under));			               
		  if ( over  ) ts->DrawLatex(0.75, 0.82, Form("%i =>", over ));
		}
		else {
		  ts->DrawLatex(0.65, 0.82, Form("N: %4.0f", nG));
		  ts->DrawLatex(0.65, 0.74, Form("#mu: %4.2f", mG));
		  ts->DrawLatex(0.65, 0.66, Form("#sigma: %4.2f", sG));
	
		  if ( under ) ts->DrawLatex(0.15, 0.82, Form("<= %i", under));			               
		  if ( over  ) ts->DrawLatex(0.75, 0.82, Form("%i =>", over ));
		}


		mP =  hp->GetMean();
		sP =  hp->GetRMS();
		nP =  hp->Integral(hp->GetXaxis()->GetFirst(), hp->GetXaxis()->GetLast());
		nP_entries = hp->GetEntries();

		if ( nP > 0 ) {

		  // -- restricted RMS
		  integral = 0.;
		  mPbin = -1000; xlow = -1000; xup = 1000;
		  over = 0.; under = 0.;
		  
		  mPbin = hp->GetXaxis()->FindBin(mP);
				  
		  for (int i = 0; integral <  pedDistr; i++) { 
		    
		    xlow = mPbin-i;
		    xup =  mPbin+i;
		    integral = hp->Integral(xlow, xup)/nP;
		    
		  }
		  
		  extra = xup - xlow;
		}
		else {

		  xlow = -300; xup = 600; extra = 0;
		  over = 0.; under = 0.;
		}

		  
		hp->GetXaxis()->SetRange(xlow - extra, xup + extra);

		nP    = hp->Integral(hp->GetXaxis()->GetFirst(), hp->GetXaxis()->GetLast());
		under = hp->GetBinContent(0);
		over  = hp->GetBinContent(hp->GetNbinsX()+1);


		c1->cd(15);

		//	hgm->Draw("colz");
		hp1->Draw();
		tl->DrawLatex(0.1, 0.92, "PH Calibration: P1");






		
		c1->cd(12);

		hp->DrawCopy();

		rp->Add(hp);
		rp->GetXaxis()->SetRange(xlow, xup);

       		mP =  rp->GetMean();
		sP =  rp->GetRMS();

		// box->DrawBox( rp->GetBinCenter(xlow), 0, rp->GetBinCenter(xup), 1.05*rp->GetMaximum());
		rp->SetFillColor(kRed);
		rp->SetFillStyle(3002);
		rp->Draw("same");
		line->DrawLine(rp->GetBinCenter(xlow), 0, rp->GetBinCenter(xlow), 0.6*rp->GetMaximum());
		line->DrawLine(rp->GetBinCenter(xup),  0, rp->GetBinCenter(xup),  0.6*rp->GetMaximum());
	 
		tl->DrawLatex(0.1, 0.92, "PH Calibration: Pedestal (DAC)");
		
		if ( hp->GetMean() < 126. ) {

		  ts->DrawLatex(0.65, 0.82, Form("N: %4.0f", nP));
		  ts->SetTextColor(kRed);
		  ts->DrawLatex(0.65, 0.74, Form("#mu: %4.1f", mP));
		  ts->DrawLatex(0.65, 0.66, Form("#sigma: %4.1f", sP));
		  ts->SetTextColor(kBlack);

		  if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
		  if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
				
		}
		else {

		  ts->DrawLatex(0.16, 0.82, Form("N: %4.0f", nP));
		  ts->SetTextColor(kRed);
		  ts->DrawLatex(0.16, 0.74, Form("#mu: %4.1f", mP));
		  ts->DrawLatex(0.16, 0.66, Form("#sigma: %4.1f", sP));
		  ts->SetTextColor(kBlack);

		  if ( under ) ts->DrawLatex(0.15, 0.55, Form("<= %i", under));			               
		  if ( over  ) ts->DrawLatex(0.75, 0.55, Form("%i =>", over ));
		}


		
	}
	
	

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Numerics and Titles
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// -- Compute the final verdict on this chip  //?? FIXME (below is pure randomness)
	char finalVerdict(0);
	if (nDeadTrimbits > 40) finalVerdict += 1;
	if (nDeadPixel > 40) finalVerdict += 10;
	if (nNoisyPixel > 40) finalVerdict += 10;
	if (nAddressProblems > 40) finalVerdict += 10;
	if (nDeadBumps > 40) finalVerdict += 100;


	// -- Defects
	c1->cd(8);
	tl->SetTextSize(0.10);
	tl->SetTextFont(22);
	double y = 0.92;
	y -= 0.11;
	tl->DrawLatex(0.1, y, "Summary");
	tl->DrawLatex(0.7, y, Form("%d", finalVerdict));

	tl->SetTextFont(132);
	tl->SetTextSize(0.09);
	y -= 0.11;
	tl->DrawLatex(0.1, y, Form("Dead Pixels: "));
	tl->DrawLatex(0.7, y, Form("%4d", nDeadPixel));

	y -= 0.10;
	tl->DrawLatex(0.1, y, Form("Noisy Pixels: "));
	tl->DrawLatex(0.7, y, Form("%4d", nNoisyPixel));
	
	y -= 0.10;
	tl->DrawLatex(0.1, y, "Mask defects: ");
	tl->DrawLatex(0.7, y, Form("%4d", nMaskDefect));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Dead Bumps: ");
	tl->DrawLatex(0.7, y, Form("%4d", nDeadBumps));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Dead Trimbits: ");
	tl->DrawLatex(0.7, y, Form("%4d", nDeadTrimbits));

	y -= 0.10;
	tl->DrawLatex(0.1, y, "Address Probl: ");
	tl->DrawLatex(0.7, y, Form("%4d", nAddressProblems));

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Row 4
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	TH2D *htm = new TH2D("htm", "", 80, 0., 80., 52, 0., 52.);

	c1->cd(13);
	
	gStyle->SetOptStat(0);
	h2 = (TH2D*)g->Get(Form("TrimMap_C%i;8", chipId));

        if (h2) {
	  for (int icol = 0; icol < 52; ++icol) {
	    for (int irow = 0; irow < 80; ++irow) {
	      
	      htm->SetBinContent(irow+1, icol+1, h2->GetBinContent(icol+1, irow+1));
	    }
	  }
  	  h2->SetTitle("");
	  h2->GetZaxis()->SetRangeUser(0., 16.);
	  h2->Draw("colz");
	}

	else { ++nRootFileProblems; }

	tl->DrawLatex(0.1, 0.92, "Trim Bits");


	FILE *tCalFile;
	sprintf(string, "%s/../T-calibration/TemperatureCalibration_C%i.dat", dirName, chipId);
	tCalFile = fopen(string, "r");
	char tCalDir[200];
	sprintf(tCalDir, "%s/../T-calibration", dirName);

	if ( tCalFile ) {
	
	  analyse(tCalDir, chipId);
	}
	else {

	  c1->cd(14);
	  TGraph *graph = (TGraph*)f->Get(Form("TempCalibration_C%i", chipId));
	  if ( graph ) { graph->Draw("A*"); }
	  else { ++nRootFileProblems; }
	  tl->DrawLatex(0.1, 0.92, "Temperature calibration");
	}


	// -- Operation Parameters
	c1->cd(16);
	
	y = 0.92;
	tl->SetTextSize(0.10);
	tl->SetTextFont(22);
	y -= 0.11;
	tl->DrawLatex(0.1, y, Form("Op. Parameters"));

	tl->SetTextFont(132);
	tl->SetTextSize(0.09);

	y -= 0.11;
	int vana(-1.);
        vana = dac_findParameter(dirName, "Vana", chipId);
	tl->DrawLatex(0.1, y, "VANA: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3i DAC", vana));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int caldel(-1.);
        caldel = dac_findParameter(dirName, "CalDel", chipId);
	tl->DrawLatex(0.1, y, "CALDEL: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", caldel));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int vthrcomp(-1.);
        vthrcomp = dac_findParameter(dirName, "VthrComp", chipId);
	tl->DrawLatex(0.1, y, "VTHR: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", vthrcomp));
	else tl->DrawLatex(0.7, y, "N/A");

	y -= 0.10;
	int vtrim(-1.);
        vtrim = dac_findParameter(dirName, "Vtrim", chipId);
	tl->DrawLatex(0.1, y, "VTRIM: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", vtrim));
	else tl->DrawLatex(0.7, y, "N/A");
	
	y -= 0.10;
	int ibias(-1.);
        ibias = dac_findParameter(dirName, "Ibias_DAC", chipId);
	tl->DrawLatex(0.1, y, "IBIAS_DAC: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", ibias));
	else tl->DrawLatex(0.7, y, "N/A");
       
	y -= 0.10;
	int voffset(-1.);
        voffset = dac_findParameter(dirName, "VoffsetOp", chipId);
	tl->DrawLatex(0.1, y, "VOFFSETOP: ");
	if (vana >= 0.) tl->DrawLatex(0.6, y, Form("%3d DAC", voffset));
	else tl->DrawLatex(0.7, y, "N/A");

	// -- Page title
	c1->cd(0);
	tl->SetTextSize(0.025);
	tl->SetTextFont(22);
	tl->DrawLatex(0.02, 0.97, Form("%s (Trim%i)", noslash.Data(),TrimVcal));

	TDatime date;
	tl->SetTextSize(0.02);
	tl->DrawLatex(0.75, 0.97, Form("%s", date.AsString()));

	c1->SaveAs(Form("%s/chipSummary_C%i_%i.ps", dirName, chipId,TrimVcal));
	c1->SaveAs(Form("%s/C%i_%i.gif", dirName, chipId,TrimVcal));
	

	// -- Dump into logfile
	ofstream OUT(Form("%s/summary_C%i_%i.txt", dirName, chipId,TrimVcal));
	OUT << "nDeadPixel: "         << nDeadPixel << endl;
	OUT << "nNoisyPixel: "        << nNoisyPixel << endl;
	OUT << "nDeadTrimbits: "      << nDeadTrimbits << endl;
	OUT << "nDeadBumps: "         << nDeadBumps << endl;
	OUT << "nMaskDefect: "        << nMaskDefect << endl;
	OUT << "nAddressProblems: "   << nAddressProblems << endl;
        OUT << "nRootFileProblems: "  << nRootFileProblems << endl;
	OUT << "SCurve "              << nN_entries << " " << mN << " " << sN << endl;
	OUT << "Threshold "           << nV_entries << " " << mV  << " " << sV << endl;
	OUT << "Gain "                << nG_entries << " " << mG << " " << sG << endl;
	OUT << "Pedestal "            << nP_entries << " " << mP << " " << sP << endl;
	OUT.close();
	
}
Exemple #10
0
void plotXY(){
  gROOT->Reset();
  gROOT->SetStyle("Plain");
  gStyle->SetHistMinimumZero(kFALSE);

  Int_t status = gSystem->Load("../rootils_C.so");
  std::cout << "status: " << status << std::endl;

  // TString dir("allStat_6hits/");
  // TString dir("allStat_noWeiBug/");
  TString dir("./");

  //Agguanta gli istogrammi
  TFile * SimF = new TFile(dir+"Sim_XY.root");
  TH2D * SimH = (TH2D*) SimF->Get("Sim_XY");;
  //
  TFile * MCF = new TFile(dir+"MC_XY.root");
  TH2D * MCH = (TH2D*) MCF->Get("MC_XY");;
  //
  //  TFile * MCFsF = new TFile(dir+"rMCFs_XY.root");
  //  TH2D * MCFsH = (TH2D*) MCFsF->Get("rMCFs_XY");;
  //
  TFile * FakeF = new TFile(dir+"MCFake_XY.root");
  TH2D * FakeH = (TH2D*) FakeF->Get("MCFake_XY");;
  //
  TFile * DataF = new TFile(dir+"Data_XY.root");
  TH2D * DataH = (TH2D*) DataF->Get("Data_XY");;
  //
  //  TFile * DataFsF = new TFile(dir+"rDataFs_XY.root");
  //  TH2D * DataFsH = (TH2D*) DataFsF->Get("rDataFs_XY");;
  //
  
  cout << MCH->GetEntries() << " " << MCH->GetEffectiveEntries() << " " << MCH->Integral() << endl;
  cout << DataH->GetEntries() << " " << DataH->GetEffectiveEntries() << " " << DataH->Integral() << endl;

  //Normalize to plot entries
  SimH->Scale(1./SimH->Integral()); 
  MCH->Scale(1./MCH->Integral());
  FakeH->Scale(1./FakeH->Integral());
  DataH->Scale(1./DataH->Integral());

  cout << MCH->GetEntries() << " " << MCH->GetEffectiveEntries() << " " << MCH->Integral() << endl;
  cout << DataH->GetEntries() << " " << DataH->GetEffectiveEntries() << " " << DataH->Integral() << endl;

  //Max
  Double_t simMax=SimH->GetMaximum();
  Double_t simPxlMax=GetMaxWithinRadius(SimH, 15.);
  Double_t simPxlInt=GetIntWithinRZ(SimH, 15., 0.);
  Double_t MCMax=MCH->GetMaximum();
  Double_t MCPxlMax=GetMaxWithinRadius(MCH, 15.);
  Double_t MCPxlInt=GetIntWithinRZ(MCH, 15., 0.);
  Double_t DataMax=DataH->GetMaximum();
  Double_t DataPxlMax=GetMaxWithinRadius(DataH, 15.);
  Double_t DataPxlInt=GetIntWithinRZ(DataH, 15., 0.);
  Double_t mAx=max(MCMax,DataMax);


  std::cout << " max     >>>> mc " << MCMax << " data " << DataMax << " Sim " << simMax << std::endl;
  std::cout << " pxl max >>>> mc " << MCPxlMax << " data " << DataPxlMax << " Sim " << simPxlMax << std::endl;
  std::cout << " pxl int >>>> mc " << MCPxlInt << " data " << DataPxlInt << " Sim " << simPxlInt << std::endl;

  // This would be need to normalize SimH to the pixel region material (taking into account that SimH and DataH/MCH have different binning!)
  //
  //  SimH->Scale(DataPxlInt*(DataH->GetXaxis()->GetBinWidth(1)*DataH->GetYaxis()->GetBinWidth(1))/simPxlInt/(SimH->GetXaxis()->GetBinWidth(1)*SimH->GetYaxis()->GetBinWidth(1)));
  // I prefer to have Sim plots and Data/MC plot to look similar by appropriately scaling the SimH range, not by scaling the histo

  mAx=0.001;
  //  mAx=0.011;
  //  mAx=0.15*0.5*(MCPxlMax+DataPxlMax);

  Double_t simRangeScale = 1./(DataPxlInt*(DataH->GetXaxis()->GetBinWidth(1)*DataH->GetYaxis()->GetBinWidth(1))/simPxlInt/(SimH->GetXaxis()->GetBinWidth(1)*SimH->GetYaxis()->GetBinWidth(1)));

  std::cout << simRangeScale << endl;
  std::cout << (DataH->GetXaxis()->GetBinWidth(1)*DataH->GetYaxis()->GetBinWidth(1)) << endl;
  std::cout << (SimH->GetXaxis()->GetBinWidth(1)*SimH->GetYaxis()->GetBinWidth(1)) << endl;

  SimH->SetMaximum(mAx*simRangeScale); 
  MCH->SetMaximum(mAx);  
  DataH->SetMaximum(mAx);

  //

  makeColorTable();

  Plot2D(SimH, "#gamma conv., MC Truth", dir);
  Plot2D(MCH, "#gamma conv., MC Reco #sqrt{s}=8TeV", dir);
  Plot2D(DataH, "#gamma conv., Data #sqrt{s}=8TeV", dir);

  //

  /*
  TH2D *MCCoarse = MCH->Rebin2D(2,2,"MCCoarse");
  TH2D *DataCoarse = DataH->Rebin2D(2,2,"DataCoarse");
  */

  /*
  cout << MCCoarse->GetEntries() << " " << MCCoarse->GetEffectiveEntries() << endl;
  cout << DataCoarse->GetEntries() << " " << DataCoarse->GetEffectiveEntries() << endl;
  */

  /*
  TH2D *diff = new TH2D("diff","diff", MCCoarse->GetNbinsX(),-60.,60.,MCCoarse->GetNbinsY(),-60.,60.);
  for (Int_t iBin = 0; iBin < MCCoarse->GetNbinsX(); iBin++){
    for (Int_t jBin = 0; jBin < MCCoarse->GetNbinsY(); jBin++){
      Double_t mc = MCCoarse->GetBinContent(iBin+1, jBin+1); 
      Double_t data = DataCoarse->GetBinContent(iBin+1, jBin+1); 
      cout << mc << " " << data << endl;
      if ( mc ) diff->SetBinContent(iBin+1,jBin+1,0.5+(mc-data)/mc);
    }
  }
  */

  MCH->Rebin2D(2,2);
  DataH->Rebin2D(2,2);

  TH2D *diff = new TH2D("diff","diff", MCH->GetNbinsX(),-60.,60.,MCH->GetNbinsY(),-60.,60.);
  for (Int_t iBin = 0; iBin < MCH->GetNbinsX(); iBin++){
    for (Int_t jBin = 0; jBin < MCH->GetNbinsY(); jBin++){
      Double_t mc = MCH->GetBinContent(iBin+1, jBin+1); 
      Double_t data = DataH->GetBinContent(iBin+1, jBin+1); 
      if ( mc+data ) {
      if ( ! mc ) mc=0.0000001;
      Double_t val = data/mc;
      //      Double_t val = (mc-data)/mc;
      Double_t valup = 1.2;
      Double_t vallo = 0.8;
      Double_t offset = 0.;
      if ( val > valup ) diff->SetBinContent(iBin+1,jBin+1,offset+valup);
      if ( val < vallo ) diff->SetBinContent(iBin+1,jBin+1,offset+vallo);
      if ( val > vallo && val < valup ) diff->SetBinContent(iBin+1,jBin+1,offset+val);
      }
    }
  }


  diff->SetMinimum(0.);
  diff->SetMaximum(2.);

  makeColorTableRB();

  Plot2D(diff,"pippo", dir);

}