Exemplo n.º 1
0
bool Bezier::checkForProblems() const {
	MathVector<float,3> corners[4];
	corners[0] = points[0][0];
	corners[1] = points[0][3];
	corners[2] = points[3][3];
	corners[3] = points[3][0];

	bool problem = false;

	for (int i = 0; i < 4; ++i) {
		MathVector<float, 3> leg1(corners[(i+1)%4] - corners[i]);
		MathVector<float, 3> leg2(corners[(i+2)%4] - corners[i]);
		MathVector<float, 3> leg3(corners[(i+3)%4] - corners[i]);

		MathVector<float, 3> dir1 = leg1.cross(leg2);
		MathVector<float, 3> dir2 = leg1.cross(leg3);
		MathVector<float, 3> dir3 = leg2.cross(leg3);

		if (dir1.dot(dir2) < -0.0001) problem = true;
		if (dir1.dot(dir3) < -0.0001) problem = true;
		if (dir3.dot(dir2) < -0.0001) problem = true;
	}

	return problem;
}
Exemplo n.º 2
0
bool BEZIER::CheckForProblems() const
{
	//MATHVECTOR<float,3> fl (points[0][0]), fr(points[0][3]), br(points[3][3]), bl(points[3][0]);
	
	MATHVECTOR<float,3> corners[4];
	corners[0] = points[0][0];
	corners[1] = points[0][3];
	corners[2] = points[3][3];
	corners[3] = points[3][0];
	
	bool problem = false;
	
	for (int i = 0; i < 4; i++)
	{
		MATHVECTOR<float,3> leg1(corners[(i+1)%4] - corners[i]);
		MATHVECTOR<float,3> leg2(corners[(i+2)%4] - corners[i]);
		MATHVECTOR<float,3> leg3(corners[(i+3)%4] - corners[i]);
		
		MATHVECTOR<float,3> dir1 = leg1.cross(leg2);
		MATHVECTOR<float,3> dir2 = leg1.cross(leg3);
		MATHVECTOR<float,3> dir3 = leg2.cross(leg3);
		
		if (dir1.dot(dir2) < -0.0001)
			problem = true;
		if (dir1.dot(dir3) < -0.0001)
			problem = true;
		if (dir3.dot(dir2) < -0.0001)
			problem = true;
		
		/*if (problem)
		{
			std::cout << *this;
			std::cout << "i: " << i << ", " << (i+1)%4 << ", " << (i+2)%4 << std::endl;
			std::cout << corners[0] << std::endl;
			std::cout << corners[1] << std::endl;
			std::cout << corners[2] << std::endl;
			std::cout << corners[3] << std::endl;
			std::cout << leg1 << std::endl;
			std::cout << leg2 << std::endl;
			std::cout << dir1 << std::endl;
			std::cout << dir1.dot(dir2) << ", " <<dir1.dot(dir3) << ", " << dir3.dot(dir2) <<std::endl;
		}*/
	}
	
	//if (problem) cout << "Degenerate bezier patch detected" << endl;
	
	return problem;
}
Exemplo n.º 3
0
void plot(const char* type = "eps")
{
  gROOT->ProcessLine(".x ../../style.C");

  TFile *f = new TFile("../../SharedRootFiles/Osc.root");

  TH1D *data[6];
  TH1D *predOsc[6];
  TH1D *acc[6];
  TH1D *li9[6];
  TH1D *fsn[6];
  TH1D *afn[6];
  TH1D *amc[6];

  data[0] = (TH1D*)f->Get("ibd_spectrum_site1_det1");
  data[1] = (TH1D*)f->Get("ibd_spectrum_site1_det2");
  data[2] = (TH1D*)f->Get("ibd_spectrum_site2_det1");
  data[3] = (TH1D*)f->Get("ibd_spectrum_site4_det1");
  data[4] = (TH1D*)f->Get("ibd_spectrum_site4_det2");
  data[5] = (TH1D*)f->Get("ibd_spectrum_site4_det3");

  predOsc[0] = (TH1D*)f->Get("total_spectrum_expected_AD1");
  predOsc[1] = (TH1D*)f->Get("total_spectrum_expected_AD2");
  predOsc[2] = (TH1D*)f->Get("total_spectrum_expected_AD3");
  predOsc[3] = (TH1D*)f->Get("total_spectrum_expected_AD4");
  predOsc[4] = (TH1D*)f->Get("total_spectrum_expected_AD5");
  predOsc[5] = (TH1D*)f->Get("total_spectrum_expected_AD6");

  acc[0] = (TH1D*)f->Get("acc_spectrum_expected_AD1");
  acc[1] = (TH1D*)f->Get("acc_spectrum_expected_AD2");
  acc[2] = (TH1D*)f->Get("acc_spectrum_expected_AD3");
  acc[3] = (TH1D*)f->Get("acc_spectrum_expected_AD4");
  acc[4] = (TH1D*)f->Get("acc_spectrum_expected_AD5");
  acc[5] = (TH1D*)f->Get("acc_spectrum_expected_AD6");

  li9[0] = (TH1D*)f->Get("li9he8_spectrum_expected_AD1");
  li9[1] = (TH1D*)f->Get("li9he8_spectrum_expected_AD2");
  li9[2] = (TH1D*)f->Get("li9he8_spectrum_expected_AD3");
  li9[3] = (TH1D*)f->Get("li9he8_spectrum_expected_AD4");
  li9[4] = (TH1D*)f->Get("li9he8_spectrum_expected_AD5");
  li9[5] = (TH1D*)f->Get("li9he8_spectrum_expected_AD6");

  fsn[0] = (TH1D*)f->Get("fastn_spectrum_expected_AD1");
  fsn[1] = (TH1D*)f->Get("fastn_spectrum_expected_AD2");
  fsn[2] = (TH1D*)f->Get("fastn_spectrum_expected_AD3");
  fsn[3] = (TH1D*)f->Get("fastn_spectrum_expected_AD4");
  fsn[4] = (TH1D*)f->Get("fastn_spectrum_expected_AD5");
  fsn[5] = (TH1D*)f->Get("fastn_spectrum_expected_AD6");

  amc[0] = (TH1D*)f->Get("amc_spectrum_expected_AD1");
  amc[1] = (TH1D*)f->Get("amc_spectrum_expected_AD2");
  amc[2] = (TH1D*)f->Get("amc_spectrum_expected_AD3");
  amc[3] = (TH1D*)f->Get("amc_spectrum_expected_AD4");
  amc[4] = (TH1D*)f->Get("amc_spectrum_expected_AD5");
  amc[5] = (TH1D*)f->Get("amc_spectrum_expected_AD6");

  afn[0] = (TH1D*)f->Get("alphan_spectrum_expected_AD1");
  afn[1] = (TH1D*)f->Get("alphan_spectrum_expected_AD2");
  afn[2] = (TH1D*)f->Get("alphan_spectrum_expected_AD3");
  afn[3] = (TH1D*)f->Get("alphan_spectrum_expected_AD4");
  afn[4] = (TH1D*)f->Get("alphan_spectrum_expected_AD5");
  afn[5] = (TH1D*)f->Get("alphan_spectrum_expected_AD6");


  TH1D *data_site1 = data[0]->Clone("data_site1");
  data_site1->GetXaxis()->SetTitle("Prompt Energy [MeV]");
  data_site1->GetYaxis()->SetTitle("Events / 50KeV");
  data_site1->SetTitle("IBDs at EH1");

  TH1D *data_site2 = data[2]->Clone("data_site2");
  data_site2->GetXaxis()->SetTitle("Prompt Energy [MeV]");
  data_site2->GetYaxis()->SetTitle("Events / 50KeV");
  data_site2->SetTitle("IBDs at EH2");

  TH1D *data_site3 = data[3]->Clone("data_site3");
  data_site3->GetXaxis()->SetTitle("Prompt Energy [MeV]");
  data_site3->GetYaxis()->SetTitle("Events / 50KeV");
  data_site3->SetTitle("IBDs at EH3");

  TH1D *pred_site1 = predOsc[0]->Clone("pred_site1");
  THStack *hs_site1 = new THStack("hs_site1", "");
  TH1D *acc_site1  = acc[0]->Clone("acc_site1");
  TH1D *li9_site1  = li9[0]->Clone("li9_site1");
  TH1D *fsn_site1  = fsn[0]->Clone("fsn_site1");
  TH1D *amc_site1  = amc[0]->Clone("amc_site1");
  TH1D *afn_site1  = afn[0]->Clone("afn_site1");

  TH1D *pred_site2 = predOsc[2]->Clone("pred_site2");
  THStack *hs_site2 = new THStack("hs_site2", "");
  TH1D *acc_site2  = acc[2]->Clone("acc_site2");
  TH1D *li9_site2  = li9[2]->Clone("li9_site2");
  TH1D *fsn_site2  = fsn[2]->Clone("fsn_site2");
  TH1D *amc_site2  = amc[2]->Clone("amc_site2");
  TH1D *afn_site2  = afn[2]->Clone("afn_site2");

  TH1D *pred_site3 = predOsc[3]->Clone("pred_site3");
  THStack *hs_site3 = new THStack("hs_site3", "");
  TH1D *acc_site3  = acc[3]->Clone("acc_site3");
  TH1D *li9_site3  = li9[3]->Clone("li9_site3");
  TH1D *fsn_site3  = fsn[3]->Clone("fsn_site3");
  TH1D *amc_site3  = amc[3]->Clone("amc_site3");
  TH1D *afn_site3  = afn[3]->Clone("afn_site3");

  data_site1->Reset();
  pred_site1->Reset();
  acc_site1->Reset();
  li9_site1->Reset();
  fsn_site1->Reset();
  amc_site1->Reset();
  afn_site1->Reset();

  data_site2->Reset();
  pred_site2->Reset();
  acc_site2->Reset();
  li9_site2->Reset();
  fsn_site2->Reset();
  amc_site2->Reset();
  afn_site2->Reset();

  data_site3->Reset();
  pred_site3->Reset();
  acc_site3->Reset();
  li9_site3->Reset();
  fsn_site3->Reset();
  amc_site3->Reset();
  afn_site3->Reset();


  pred_site1->SetLineColor(2);
  pred_site1->SetMarkerColor(2);

  acc_site1->SetLineColor(9);
  acc_site1->SetMarkerColor(9);
  acc_site1->SetFillColor(9);

  li9_site1->SetLineColor(5);
  li9_site1->SetMarkerColor(5);
  li9_site1->SetFillColor(5);

  fsn_site1->SetLineColor(6);
  fsn_site1->SetMarkerColor(6);
  fsn_site1->SetFillColor(6);

  amc_site1->SetLineColor(7);
  amc_site1->SetMarkerColor(7);
  amc_site1->SetFillColor(7);

  afn_site1->SetLineColor(8);
  afn_site1->SetLineColor(8);
  afn_site1->SetFillColor(8);



  pred_site2->SetLineColor(2);
  pred_site2->SetMarkerColor(2);

  acc_site2->SetLineColor(9);
  acc_site2->SetMarkerColor(9);
  acc_site2->SetFillColor(9);

  li9_site2->SetLineColor(5);
  li9_site2->SetMarkerColor(5);
  li9_site2->SetFillColor(5);

  fsn_site2->SetLineColor(6);
  fsn_site2->SetMarkerColor(6);
  fsn_site2->SetFillColor(6);

  amc_site2->SetLineColor(7);
  amc_site2->SetMarkerColor(7);
  amc_site2->SetFillColor(7);

  afn_site2->SetLineColor(8);
  afn_site2->SetLineColor(8);
  afn_site2->SetFillColor(8);


  pred_site3->SetLineColor(2);
  pred_site3->SetMarkerColor(2);

  acc_site3->SetLineColor(9);
  acc_site3->SetMarkerColor(9);
  acc_site3->SetFillColor(9);

  li9_site3->SetLineColor(5);
  li9_site3->SetMarkerColor(5);
  li9_site3->SetFillColor(5);

  fsn_site3->SetLineColor(6);
  fsn_site3->SetMarkerColor(6);
  fsn_site3->SetFillColor(6);

  amc_site3->SetLineColor(7);
  amc_site3->SetMarkerColor(7);
  amc_site3->SetFillColor(7);

  afn_site3->SetLineColor(8);
  afn_site3->SetLineColor(8);
  afn_site3->SetFillColor(8);


  for (int i=0; i<2; i++) {
    data_site1->Add(data[i]);
    pred_site1->Add(predOsc[i]);
    acc_site1->Add(acc[i]);
    li9_site1->Add(li9[i]);
    fsn_site1->Add(fsn[i]);
    amc_site1->Add(amc[i]);
    afn_site1->Add(afn[i]);
  }

  for (int i=2; i<3; i++) {
    data_site2->Add(data[i]);
    pred_site2->Add(predOsc[i]);
    acc_site2->Add(acc[i]);
    li9_site2->Add(li9[i]);
    fsn_site2->Add(fsn[i]);
    amc_site2->Add(amc[i]);
    afn_site2->Add(afn[i]);
  }

  for (int i=3; i<6; i++) {
    data_site3->Add(data[i]);
    pred_site3->Add(predOsc[i]);
    acc_site3->Add(acc[i]);
    li9_site3->Add(li9[i]);
    fsn_site3->Add(fsn[i]);
    amc_site3->Add(amc[i]);
    afn_site3->Add(afn[i]);
  }

  hs_site1->Add(fsn_site1);
  hs_site1->Add(afn_site1);
  hs_site1->Add(amc_site1);
  hs_site1->Add(li9_site1);
  hs_site1->Add(acc_site1);

  hs_site2->Add(fsn_site2);
  hs_site2->Add(afn_site2);
  hs_site2->Add(amc_site2);
  hs_site2->Add(li9_site2);
  hs_site2->Add(acc_site2);

  hs_site3->Add(fsn_site3);
  hs_site3->Add(afn_site3);
  hs_site3->Add(amc_site3);
  hs_site3->Add(li9_site3);
  hs_site3->Add(acc_site3);

  TCanvas c1("c1", "c1", 800, 800);
  TPad *can_1 = new TPad("can_1", "can_1",0, 0.66, 1, 0.99);
  TPad *can_2 = new TPad("can_2", "can_2",0, 0.33, 1, 0.66);
  TPad *can_3 = new TPad("can_3", "can_3",0, 0, 1, 0.33);

  can_1->Draw();
  can_1->cd();
  can_1->SetBottomMargin(1e-05);
  can_1->SetFrameBorderMode(0);
  can_1->SetBorderMode(0);
  data_site1->Draw();
  data_site1->SetTitle("");
  hs_site1->Draw("same");
  data_site1->Draw("same");
  // pred_site1->Draw("same");
  data_site1->GetXaxis()->SetLabelSize(0.09);
  data_site1->GetYaxis()->SetNdivisions(508);
  data_site1->GetYaxis()->SetTitleSize(0.08);
  data_site1->GetYaxis()->SetLabelSize(0.08);
  data_site1->GetYaxis()->SetTitleOffset(0.6);
  data_site1->GetYaxis()->CenterTitle(true);
  TLegend leg(0.65,0.85-0.20,0.85,0.85);
  leg.AddEntry(data_site1, " EH1", "lp");
  leg.AddEntry(acc_site1, " Acc. Bkgd", "f");
  leg.SetFillColor(kWhite);
  leg.Draw();
  can_1->Modified();
  c1.cd();

  can_2->Draw();
  can_2->cd();
  can_2->SetTopMargin(1e-05);
  can_2->SetBottomMargin(1e-05);
  can_2->SetFrameBorderMode(0);
  can_2->SetBorderMode(0);
  data_site2->Draw();
  hs_site2->Draw("same");
  data_site2->SetTitle("");
  // data_site2->Draw("same");
  // pred_site2->Draw("same");
  data_site2->GetXaxis()->SetLabelSize(0.09);
  data_site2->GetYaxis()->SetNdivisions(508);
  data_site2->GetYaxis()->SetTitleSize(0.08);
  data_site2->GetYaxis()->SetLabelSize(0.08);
  data_site2->GetYaxis()->SetTitleOffset(0.6);
  data_site2->GetYaxis()->CenterTitle(true);
  TLegend leg2(0.65,0.85-0.20,0.85,0.85);
  leg2.AddEntry(data_site2, " EH2", "lp");
  leg2.AddEntry(acc_site2, " Acc. Bkgd", "f");
  leg2.SetFillColor(kWhite);
  leg2.Draw();
  can_2->Modified();
  c1.cd();

  can_3->Draw();
  can_3->cd();
  can_3->SetTopMargin(1e-05);
  can_3->SetFrameBorderMode(0);
  can_3->SetBottomMargin(0.3);
  can_3->SetBorderMode(0);
  data_site3->Draw();
  hs_site3->Draw("same");
  data_site3->SetTitle("");
  // data_site3->Draw("same");
  // pred_site3->Draw("same");
  data_site3->GetXaxis()->SetTitle("Reconstructed Energy [MeV]");
  data_site3->GetXaxis()->SetTitleSize(0.09);
  data_site3->GetXaxis()->SetTitleOffset(1.45);
  data_site3->GetXaxis()->SetLabelSize(0.09);
  data_site3->GetXaxis()->Draw();
  data_site3->GetYaxis()->SetNdivisions(508);
  data_site3->GetYaxis()->SetLabelSize(0.08);
  data_site3->GetYaxis()->SetTitleSize(0.08);
  data_site3->GetYaxis()->SetTitleOffset(0.6);
  data_site3->GetYaxis()->CenterTitle(true);
  can_3->RedrawAxis();
  TLegend leg3(0.65,0.85-0.20,0.85,0.85);
  leg3.AddEntry(data_site3, " EH3", "lp");
  leg3.AddEntry(acc_site3, " Acc. Bkgd", "f");
  leg3.SetFillColor(kWhite);
  leg3.Draw();
  can_3->Modified();
  c1.cd();

  TString name("SpectraSite");
  // print 3 figures by default
  c1.SaveAs(name + ".eps");
  c1.SaveAs(name + ".pdf");
  c1.SaveAs(name + ".png");

  TString extra(type);
  if (! (extra == "eps" || extra == "pdf" || extra == "png")) {
    c1.SaveAs(name + "." + type);
  }
}
Exemplo n.º 4
0
// ################################################
double NeuralNetwork::LearnGivenData(int MiniBatchSize, int epochs, int TrainingsSize, bool TestOnTrainingsData, bool TestOnTestingData, std::string SavePath){

  TCanvas c1("Accuracy", "Accuracy", 600, 500);
  TCanvas c2("Cost", "Cost", 600, 500);
  TCanvas c3("summary", "summary", 1200, 500);
  c3.SetGrid();
  c3.Divide(2);
  c1.cd();
  TH1F axis("axis", "Accuracy of Neural Network;epochs", epochs, 0.5, epochs+0.5);
  TH1F acc_test ("acc_test" , "Classification accuracy", epochs, 0.5, epochs+0.5);
  TH1F acc_train("acc_train", "Classification accuracy", epochs, 0.5, epochs+0.5);
  TH1F axis2("axis2", "Cost Function of Neural Network;epochs", epochs, 0.5, epochs+0.5);
  TH1F cost_test ("cost_test" , "Cost Function", epochs, 0.5, epochs+0.5);
  TH1F cost_train("cost_train", "Cost Function", epochs, 0.5, epochs+0.5);
  if (TrainingsSize == -1) TrainingsSize = fTrainingsSize;

  for (int k = 0; k < epochs; ++k){
    // if (k % 5 == 0) std::cout << "Epoch nr. " << k << " started!" << std::endl;
    for (int i = 0; i < TrainingsSize / MiniBatchSize; ++i){
      for (int j = 0; j < MiniBatchSize; ++j){
        SetInputVectorInMiniBatch(fTrainingsdata[i*10 + j], fTrainingslabel[i*10 + j]);
      }
      LearnMiniBatches();
    }
    if (TestOnTestingData){
      double correct = 0;
      double cost = 0;
      for (int i = 0; i < fTestingSize; ++i){
        if (Evaluate(fTestingdata[i], fTestinglabel[i]) == true) correct++;
        cost += EvaluateCost(fTestingdata[i], fTestinglabel[i]);
      }
      // std::cout << "Accuracy of test data: " << correct / fTestingSize << " %"<< std::endl;
      acc_test.SetBinContent(k+1, correct / fTestingSize);
      cost_test.SetBinContent(k+1, cost / fTestingSize);
    }
    if (TestOnTrainingsData){
      double correct = 0;
      double cost = 0;
      for (int i = 0; i < TrainingsSize; ++i){
        if (Evaluate(fTrainingsdata[i], fTrainingslabel[i]) == true) correct++;
        cost += EvaluateCost(fTrainingsdata[i], fTrainingslabel[i]);
      }
      // std::cout << "Accuracy of trainings data: " << correct / TrainingsSize << " %" << std::endl;
      acc_train.SetBinContent(k+1, correct / TrainingsSize);
      cost_train.SetBinContent(k+1, cost / fTrainingsSize);
    }
  }
  axis.SetStats(false);
  axis.GetYaxis()->SetNdivisions(524);
  axis.SetAxisRange(0.5, 1.1, "Y");
  axis.Draw("");

  double markersize = 1;
  if (epochs > 100) {
    markersize = 0.5;
    c1.SetLogx();
  }

  acc_test.SetStats(false);
  acc_test.SetMarkerStyle(20);
  acc_test.SetMarkerColor(kOrange-3);
  acc_test.SetMarkerSize(1.);
  acc_test.Draw("p same");
  acc_train.SetStats(false);
  acc_train.SetMarkerStyle(20);
  acc_train.SetMarkerColor(kAzure-2);
  acc_train.SetMarkerSize(1.);
  acc_train.Draw("p same");
  // c1.SetLogx();
  c1.SetGrid();

  // Calculate maxima
  double max_trainingsdata = acc_train.GetMaximum();
  double max_testdata      = acc_test.GetMaximum();

  TLegend leg(0.4,0.1,0.9,0.3);
  leg.SetHeader("Accuracy of");
  leg.AddEntry(&acc_test,  Form("Test data (Maximum: %4.3f)", max_testdata), "p");
  leg.AddEntry(&acc_train, Form("Trainings data (Maximum: %4.3f)", max_trainingsdata), "p");
  leg.Draw("same");

  c1.SaveAs(Form("%saccuracy.pdf", SavePath.c_str()));



  c2.cd();
  axis2.SetStats(false);
  axis2.GetYaxis()->SetNdivisions(524);
  axis2.SetAxisRange(cost_train.GetMinimum()*0.9, cost_test.GetMaximum()*1.1, "Y");
  axis2.Draw("");

  if (epochs > 100) {
    markersize = 0.5;
    c2.SetLogx();
  }

  cost_train.SetStats(false);
  cost_train.SetMarkerStyle(20);
  cost_train.SetMarkerColor(kAzure-2);
  cost_train.SetMarkerSize(1.);
  cost_train.Draw("p same");
  cost_test.SetStats(false);
  cost_test.SetMarkerStyle(20);
  cost_test.SetMarkerColor(kOrange-3);
  cost_test.SetMarkerSize(1.);
  cost_test.Draw("p same");
  // c1.SetLogx();
  c2.SetGrid();

  // Calculate maxima
  double min_trainingsdata = cost_train.GetMinimum();
  double min_testdata      = cost_test.GetMinimum();

  TLegend leg2(0.4,0.7,0.9,0.9);
  leg2.SetHeader("Cost Function");
  leg2.AddEntry(&acc_test,  Form("Test data (Minimum: %4.3f)", min_testdata), "p");
  leg2.AddEntry(&acc_train, Form("Trainings data (Minimum: %4.3f)", min_trainingsdata), "p");
  leg2.Draw("same");

  c2.SaveAs(Form("%scost.pdf", SavePath.c_str()));

  c3.cd(1);
  axis.Draw("");
  acc_test.Draw("p same");
  acc_train.Draw("p same");
  leg.Draw("same");
  c3.cd(2);
  axis2.Draw("");
  cost_test.Draw("p same");
  cost_train.Draw("p same");
  leg2.Draw("same");
  c3.SaveAs(Form("%ssummary.pdf", SavePath.c_str()));



  // std::cout << "Learning completed" << std::endl;
  return max_testdata;
}
Exemplo n.º 5
0
int main(int argc, char* argv[]) {

  TH1::SetDefaultSumw2();

  ProgramOptions options(argc, argv);

  double lumi = options.lumi;

  // input datasets
  Datasets datasets(options.iDir);
  datasets.readFile(options.datasetFile);
 
  // output file
  TFile* ofile = TFile::Open( (options.oDir+std::string("/ZMuMufromW.root")).c_str(), "RECREATE");
  std::string oDir = options.oDir + std::string("/ZmumuFromW");

  if (oDir!="") {
    boost::filesystem::path opath(oDir);
    if (!exists(opath)) {
      std::cout << "Creating output directory : " << oDir << std::endl;
      boost::filesystem::create_directory(opath);
    }
    std::cout << "Writing results to " << oDir << std::endl;
  }

  // cuts
  Cuts cuts;
  unsigned nCutsZMuMu = cuts.nCutsZMuMu();

  TCut puWeight("puWeight");
  TCut trigCorr( "(trigCorrWeight>0) ? trigCorrWeight : 1." );

  // For lepton weights
  TCut muTightWeight    = cuts.muTightWeight(options.leptCorr);
  TCut lVetoWeight  	= cuts.elVetoWeight(options.leptCorr) * cuts.muVetoWeight(options.leptCorr);

  TCut METNo2Muon130("metNo2Muon>130.");
  TCut cutLoDPhi = cuts.cut("dPhiJJ");
  TCut cutHiDPhi("vbfDPhi>2.6");
  TCut cutMe1DPhi("vbfDPhi>1.0 && vbfDPhi<=1.8");
  TCut cutMe2DPhi("vbfDPhi>1.8 && vbfDPhi<=2.6");

  // histograms
  double dphiEdges[5] = { 0., 1.0, 1.8, 2.6, TMath::Pi() };

  // Observed signal MET>130
  TH1D* hZ_DY_C_DPhi 	= new TH1D("hZ_DY_C_DPhi", "", 4, dphiEdges);  // Z+jets MC ctrl region
  TH1D* hZ_BG_ZC_DPhi 	= new TH1D("hZ_BG_ZC_DPhi", "", 4, dphiEdges);  // background MC ctrl region
  TH1D* hZ_Data_ZC_DPhi = new TH1D("hZ_Data_ZC_DPhi", "", 4, dphiEdges);  // Data ctrl region
  // Predicted from Wmunu
  TH1D* hZ_W_C_DPhi 	= new TH1D("hZ_W_C_DPhi", "", 4, dphiEdges);  	// W+jets MC ctrl region
  TH1D* hZ_BG_WC_DPhi 	= new TH1D("hZ_BG_WC_DPhi", "", 4, dphiEdges);  	// background MC ctrl region
  TH1D* hZ_Data_WC_DPhi = new TH1D("hZ_Data_WC_DPhi", "", 4, dphiEdges);  // Data W ctrl region

  TH1D* hZ_W_EffMu_D 	= new TH1D("hZ_W_EffMu_D", "", 1, 0., 1.);	// denominator of MuMu efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  TH1D* hZ_W_EffMu_N 	= new TH1D("hZ_W_EffMu_N", "", 1, 0., 1.);	// numerator of MuMu efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples

  TH1D* hZ_DY_EffMuMu_D = new TH1D("hZ_DY_EffMuMu_D", "", 1, 0., 1.);   // denominator of MuMu efficiency from DY + DY_EWK samples
  TH1D* hZ_DY_EffMuMu_N = new TH1D("hZ_DY_EffMuMu_N", "", 1, 0., 1.);   // numerator of MuMu efficiency from DY + DY_EWK samples 
  
  TH1D* hZ_DY_EffVBFS_D = new TH1D("hZ_DY_EffVBFS_D", "", 1, 0., 1.);   // denominator of VBF(S) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  //TH1D* hZ_DY_EffVBFS_N  = new TH1D("hZ_DY_EffVBFS_N", "", 1, 0., 1.);   // numerator of VBF(S) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples  
  TH1D* hZ_DY_EffVBFS_NLo  = new TH1D("hZ_DY_EffVBFS_NLo", "", 1, 0., 1.);   
  TH1D* hZ_DY_EffVBFS_NHi  = new TH1D("hZ_DY_EffVBFS_NHi", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFS_NMe1 = new TH1D("hZ_DY_EffVBFS_NMe1", "", 1, 0., 1.); 
  TH1D* hZ_DY_EffVBFS_NMe2 = new TH1D("hZ_DY_EffVBFS_NMe2", "", 1, 0., 1.);  

  TH1D* hZ_W_EffVBFC_D 	   = new TH1D("hZ_W_EffVBFC_D", "", 1, 0., 1.);   // denominator of VBF(C) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  //TH1D* hZ_W_EffVBFC_N   = new TH1D("hZ_W_EffVBFC_N", "", 1, 0., 1.);   // numerator of VBF(C) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples 
  TH1D* hZ_W_EffVBFC_NLo   = new TH1D("hZ_W_EffVBFC_NLo", "", 1, 0., 1.);  
  TH1D* hZ_W_EffVBFC_NHi   = new TH1D("hZ_W_EffVBFC_NHi", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFC_NMe1  = new TH1D("hZ_W_EffVBFC_NMe1", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFC_NMe2  = new TH1D("hZ_W_EffVBFC_NMe2", "", 1, 0., 1.);

  // loop over MC datasets
  for (unsigned i=0; i<datasets.size(); ++i) {

    Dataset dataset = datasets.getDataset(i);
    
    TCut cutD = cuts.cutDataset(dataset.name);
    TCut wWeight("");

    // bit of a fudge for mutight weight - data doesn't have the var in it, so need to add an exception (see below)
    // and then restore it for all other MC
    muTightWeight=cuts.muTightWeight(options.leptCorr);

    // check if it's DYJets
    bool isDY = false;
    bool isWJets = false;
    bool isQCD = false;
    bool isEwkW = false;
    if (dataset.name.compare(0,2,"DY")==0) {
      isDY = true;
      std::cout << "Analysing DY->ll MC : " << dataset.name << std::endl;
    }
    else if (dataset.name == "WJets" ||
        dataset.name == "W1Jets" ||
        dataset.name == "W2Jets" ||
        dataset.name == "W3Jets" ||
        dataset.name == "W4Jets" ||
        dataset.name == "EWK_Wp2Jets" ||
        dataset.name == "EWK_Wm2Jets") {

      if (dataset.name == "EWK_Wp2Jets" || dataset.name == "EWK_Wm2Jets") isEwkW = true;
      else isWJets = true;

      if(isWJets) wWeight =  cuts.wWeight();
      std::cout << "Analysing W MC     : " << dataset.name << std::endl;
    }
    else if (dataset.name.compare(0,3,"QCD")==0) {
      isQCD = true;
      std::cout << "Analysing QCD : " << dataset.name << std::endl;
    }
    else if (dataset.isData) {
      muTightWeight="";
      std::cout << "Analysing Data      : " << dataset.name << std::endl;
    }
    else {
      std::cout << "Analysing BG MC     : " << dataset.name << std::endl;
    }

    // get file & tree
    TFile* file = datasets.getTFile(dataset.name);
    TTree* tree = (TTree*) file->Get("invHiggsInfo/InvHiggsInfo");

    // set up cuts
    TCut otherCuts = puWeight * trigCorr * wWeight;

    TCut cutZMuMu_C    	      = otherCuts * muTightWeight * (cutD + cuts.zMuMuVBF() + METNo2Muon130);
    TCut cutWMuNu_C    	      = otherCuts * muTightWeight * (cutD + cuts.wMuVBF() + cuts.cutWMu("MET"));
    // eps_mu from W trigger samples
    TCut cutEfficiencyMu_D    = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen());
    TCut cutEfficiencyMu_N    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu"));

    TCut cutEfficiencyMuMu_D    = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen());
    TCut cutEfficiencyMuMu_N    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco());
    // eps_VBFS from DY-Trig
    TCut cutEfficiencyVBFS_D    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco());
    //TCut cutEfficiencyVBFS_N  = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130);
    TCut cutEfficiencyVBFS_NLoDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutLoDPhi);
    TCut cutEfficiencyVBFS_NHiDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutHiDPhi);
    TCut cutEfficiencyVBFS_NMe1DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutMe1DPhi);
    TCut cutEfficiencyVBFS_NMe2DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutMe2DPhi);

    // eps_VBFC from W
    TCut cutEfficiencyVBFC_D    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu"));
    //TCut cutEfficiencyVBFC_N  = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET")); 
    TCut cutEfficiencyVBFC_NLoDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutLoDPhi);
    TCut cutEfficiencyVBFC_NHiDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutHiDPhi);
    TCut cutEfficiencyVBFC_NMe1DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutMe1DPhi);
    TCut cutEfficiencyVBFC_NMe2DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutMe2DPhi);

    // fill tmp histograms for BG estimation
    //observed
    //TH1D* hZ_ZC_noDPhi  	= new TH1D("hZ_ZC_noDPhi", "", 1, 0., 1.);
    TH1D* hZ_ZC_DPhi  		= new TH1D("hZ_ZC_DPhi", "", 4, dphiEdges);  
    //predicted
    //TH1D* hZ_WC_noDPhi  	= new TH1D("hZ_WC_noDPhi", "", 1, 0., 1.);
    TH1D* hZ_WC_DPhi  		= new TH1D("hZ_WC_DPhi", "", 4, dphiEdges);  // this is for the actual BG estimation
    // fill tmp histograms for efficiency calculation
    TH1D* hZ_EffMuMu_D  	= new TH1D("hZ_EffMuMu_D",   "", 1, 0., 1.);
    TH1D* hZ_EffMuMu_N  	= new TH1D("hZ_EffMuMu_N",   "", 1, 0., 1.);
    TH1D* hZ_EffMu_D  		= new TH1D("hZ_EffMu_D",     "", 1, 0., 1.);
    TH1D* hZ_EffMu_N  		= new TH1D("hZ_EffMu_N",     "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_D  	= new TH1D("hZ_EffVBFS_D",   "", 1, 0., 1.);
    //TH1D* hZ_EffVBFS_N  	= new TH1D("hZ_EffVBFS_N",   "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NLo  	= new TH1D("hZ_EffVBFS_NLo", "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NHi  	= new TH1D("hZ_EffVBFS_NHi", "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NMe1  	= new TH1D("hZ_EffVBFS_NMe1","", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NMe2  	= new TH1D("hZ_EffVBFS_NMe2","", 1, 0., 1.);

    TH1D* hZ_EffVBFC_D  	= new TH1D("hZ_EffVBFC_D",   "", 1, 0., 1.);
    //TH1D* hZ_EffVBFC_N  	= new TH1D("hZ_EffVBFC_N",   "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NLo  	= new TH1D("hZ_EffVBFC_NLo", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NHi  	= new TH1D("hZ_EffVBFC_NHi", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NMe1  	= new TH1D("hZ_EffVBFC_NMe1","", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NMe2  	= new TH1D("hZ_EffVBFC_NMe2","", 1, 0., 1.);

    // W control region DY is BG
    if (isDY) { 
      //tree->Draw("0.5>>hZ_ZC_noDPhi",    cutZMuMu_C);
      tree->Draw("vbfDPhi>>hZ_ZC_DPhi",    cutZMuMu_C);

      //tree->Draw("0.5>>hZ_WC_noDPhi",    cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
      tree->Draw("0.5>>hZ_EffMuMu_D",      cutEfficiencyMuMu_D);
      tree->Draw("0.5>>hZ_EffMuMu_N",      cutEfficiencyMuMu_N);
      tree->Draw("0.5>>hZ_EffVBFS_D",      cutEfficiencyVBFS_D);
      //tree->Draw("0.5>>hZ_EffVBFS_N",    cutEfficiencyVBFS_N);
      tree->Draw("0.5>>hZ_EffVBFS_NLo",    cutEfficiencyVBFS_NLoDPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NHi",    cutEfficiencyVBFS_NHiDPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NMe1",   cutEfficiencyVBFS_NMe1DPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NMe2",   cutEfficiencyVBFS_NMe2DPhi);
    }
    else if(isWJets || isEwkW) {
      //tree->Draw("0.5>>hZ_WC_noDPhi",    cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
      tree->Draw("0.5>>hZ_EffMu_D",        cutEfficiencyMu_D);  
      tree->Draw("0.5>>hZ_EffMu_N",        cutEfficiencyMu_N);    
      tree->Draw("0.5>>hZ_EffVBFC_D",      cutEfficiencyVBFC_D);
      //tree->Draw("0.5>>hZ_EffVBFC_N",    cutEfficiencyVBFC_N);
      tree->Draw("0.5>>hZ_EffVBFC_NLo",    cutEfficiencyVBFC_NLoDPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NHi",    cutEfficiencyVBFC_NHiDPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NMe1",   cutEfficiencyVBFC_NMe1DPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NMe2",   cutEfficiencyVBFC_NMe2DPhi);
    }
    else if(isQCD) {
      //tree->Draw("0.5>>hZ_C_noDPhi",     cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
    }
    else {
      tree->Draw("vbfDPhi>>hZ_ZC_DPhi",    cutZMuMu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
    }

    // weight  to lumi
    double weight = (dataset.isData) ? 1. : lumi * dataset.sigma / dataset.nEvents;

    hZ_ZC_DPhi->Scale(weight);
    hZ_WC_DPhi->Scale(weight);
    hZ_EffVBFS_D->Scale(weight);
    //hZ_EffVBFS_N->Scale(weight);
    hZ_EffVBFS_NLo->Scale(weight);
    hZ_EffVBFS_NHi->Scale(weight);
    hZ_EffVBFS_NMe1->Scale(weight);
    hZ_EffVBFS_NMe2->Scale(weight);
    hZ_EffVBFC_D->Scale(weight);
    //hZ_EffVBFC_N->Scale(weight);
    hZ_EffVBFC_NLo->Scale(weight);
    hZ_EffVBFC_NHi->Scale(weight);
    hZ_EffVBFC_NMe1->Scale(weight);
    hZ_EffVBFC_NMe2->Scale(weight);
    hZ_EffMu_D->Scale(weight);
    hZ_EffMu_N->Scale(weight);
    hZ_EffMuMu_D->Scale(weight);
    hZ_EffMuMu_N->Scale(weight);

    // add to output histograms
    if (dataset.isData) {
      hZ_Data_ZC_DPhi->Add(hZ_ZC_DPhi);
      hZ_Data_WC_DPhi->Add(hZ_WC_DPhi);
    }
    else if (isWJets || isEwkW) {
      //hZ_W_C_noDPhi->Add(hZ_C_noDPhi);
      hZ_W_C_DPhi->Add(hZ_WC_DPhi);
      hZ_W_EffMu_D->Add(hZ_EffMu_D);
      hZ_W_EffMu_N->Add(hZ_EffMu_N);
      hZ_W_EffVBFC_D->Add(hZ_EffVBFC_D);
      //hZ_W_EffVBFC_N->Add(hZ_EffVBFC_N);
      hZ_W_EffVBFC_NLo->Add(hZ_EffVBFC_NLo);
      hZ_W_EffVBFC_NHi->Add(hZ_EffVBFC_NHi);
      hZ_W_EffVBFC_NMe1->Add(hZ_EffVBFC_NMe1);
      hZ_W_EffVBFC_NMe2->Add(hZ_EffVBFC_NMe2);
    }
    else if (isDY) {
      hZ_DY_C_DPhi->Add(hZ_ZC_DPhi);
      hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);

      hZ_DY_EffMuMu_D->Add(hZ_EffMuMu_D);
      hZ_DY_EffMuMu_N->Add(hZ_EffMuMu_N);
      hZ_DY_EffVBFS_D->Add(hZ_EffVBFS_D);
      //hZ_DY_EffVBFS_N->Add(hZ_EffVBFS_N);
      hZ_DY_EffVBFS_NLo->Add(hZ_EffVBFS_NLo);
      hZ_DY_EffVBFS_NHi->Add(hZ_EffVBFS_NHi);
      hZ_DY_EffVBFS_NMe1->Add(hZ_EffVBFS_NMe1);
      hZ_DY_EffVBFS_NMe2->Add(hZ_EffVBFS_NMe2);
    }
    else if (isQCD) {
      //hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);
    }
    else {
      hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);
      hZ_BG_ZC_DPhi->Add(hZ_ZC_DPhi);
    }

    std::cout << "  N_Z ctrl (dphi<1.0) : " << hZ_ZC_DPhi->GetBinContent(1) << " +/- " << hZ_ZC_DPhi->GetBinError(1) << std::endl;
    std::cout << "  N_W ctrl (dphi<1.0) : " << hZ_WC_DPhi->GetBinContent(1) << " +/- " << hZ_WC_DPhi->GetBinError(1) << std::endl;
    
    delete hZ_WC_DPhi;
    delete hZ_ZC_DPhi;
    delete hZ_EffMuMu_D;
    delete hZ_EffMuMu_N;
    delete hZ_EffMu_D;
    delete hZ_EffMu_N;
    delete hZ_EffVBFS_D;
    //delete hZ_EffVBFS_N;
    delete hZ_EffVBFS_NLo;
    delete hZ_EffVBFS_NMe1;
    delete hZ_EffVBFS_NMe2;
    delete hZ_EffVBFS_NHi;
    delete hZ_EffVBFC_D;
    //delete hZ_EffVBFC_N;
    delete hZ_EffVBFC_NLo;
    delete hZ_EffVBFC_NMe1;
    delete hZ_EffVBFC_NMe2;
    delete hZ_EffVBFC_NHi;

    // clean up
    delete tree;
    file->Close();
   
  }

  // numbers - calculate these from MC in this program later!
  //double ratioBF = 5.626;  //  MCFM + NLO
  double ratioBF = 1144./14428.;

  TH1D* hZ_Est_ZC_DPhi = new TH1D("hZ_Est_ZC_DPhi", "", 4, dphiEdges); 

  // bins dPhi
  TH1D* hZ_Est_WC_DPhi = new TH1D("hZ_Est_WC_DPhi", "", 4, dphiEdges); 
  TH1D* hZ_Est_WS_DPhi = new TH1D("hZ_Est_WS_DPhi", "", 4, dphiEdges); 
  TH1D* hZ_Eff_WS_DPhi = new TH1D("hZ_Eff_WS_DPhi", "", 4, dphiEdges);
 
  TH1D* hZ_W_EffMu    = new TH1D("hZ_W_EffMu", "", 1, 0., 1.);     	// epsilon mumu
  hZ_W_EffMu->Add(hZ_W_EffMu_N);
  hZ_W_EffMu->Divide(hZ_W_EffMu_D);
  //double mu_syst = 0.025*hZ_W_EffMu->GetBinContent(1);      //2.5% Muon ID/Iso efficiency uncertainty from EWK-10-002
  //hZ_W_EffMu->SetBinError(1,TMath::Sqrt(hZ_W_EffMu->GetBinError(1)*hZ_W_EffMu->GetBinError(1) + mu_syst*mu_syst));

  TH1D* hZ_DY_EffMuMu = new TH1D("hZ_DY_EffMuMu", "", 1, 0., 1.);       // epsilon mumu
  hZ_DY_EffMuMu->Add(hZ_DY_EffMuMu_N);
  hZ_DY_EffMuMu->Divide(hZ_DY_EffMuMu_D);
  //mu_syst = 0.025*hZ_DY_EffMuMu->GetBinContent(1);      //2.5% Muon ID/Iso efficiency uncertainty from EWK-10-002
  //hZ_DY_EffMuMu->SetBinError(1,TMath::Sqrt(hZ_DY_EffMuMu->GetBinError(1)*hZ_DY_EffMuMu->GetBinError(1) + mu_syst*mu_syst));

  //TH1D* hZ_DY_EffVBFS  = new TH1D("hZ_DY_EffVBFS", "", 1, 0., 1.);  	// epsilon_s_vbf
  TH1D* hZ_DY_EffVBFSLo  = new TH1D("hZ_DY_EffVBFSLo", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSHi  = new TH1D("hZ_DY_EffVBFSHi", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSMe1 = new TH1D("hZ_DY_EffVBFSMe1", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSMe2 = new TH1D("hZ_DY_EffVBFSMe2", "", 1, 0., 1.);

  //TH1D* hZ_W_EffVBFC   = new TH1D("hZ_W_EffVBFC", "", 1, 0., 1.);       // epsilon_c_vbf
  TH1D* hZ_W_EffVBFCLo   = new TH1D("hZ_W_EffVBFCLo", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCHi   = new TH1D("hZ_W_EffVBFCHi", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCMe1  = new TH1D("hZ_W_EffVBFCMe1", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCMe2  = new TH1D("hZ_W_EffVBFCMe2", "", 1, 0., 1.);

  //TH1D* hZ_W_RatioVBF  = new TH1D("hZ_W_RatioVBF", "", 1, 0., 1.);	// epsilon_s_vbf/epsilon_c_vbf
  TH1D* hZ_W_RatioVBFLo  = new TH1D("hZ_W_RatioVBFLo", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFHi  = new TH1D("hZ_W_RatioVBFHi", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFMe1 = new TH1D("hZ_W_RatioVBFMe1", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFMe2 = new TH1D("hZ_W_RatioVBFMe2", "", 1, 0., 1.); 

  //TH1D* hZ_W_TotalEff  = new TH1D("hZ_W_TotalEff", "", 1, 0., 1.); 
  TH1D* hZ_W_TotalEffLo  = new TH1D("hZ_W_TotalEffLo", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffMe1 = new TH1D("hZ_W_TotalEffMe1", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffMe2 = new TH1D("hZ_W_TotalEffMe2", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffHi  = new TH1D("hZ_W_TotalEffHi", "", 1, 0., 1.);
  
  //hZ_DY_EffVBFS->Add(hZ_DY_EffVBFS_N);
  //hZ_DY_EffVBFS->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSLo->Add(hZ_DY_EffVBFS_NLo);
  hZ_DY_EffVBFSLo->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSHi->Add(hZ_DY_EffVBFS_NHi);
  hZ_DY_EffVBFSHi->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSMe1->Add(hZ_DY_EffVBFS_NMe1);
  hZ_DY_EffVBFSMe1->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSMe2->Add(hZ_DY_EffVBFS_NMe2);
  hZ_DY_EffVBFSMe2->Divide(hZ_DY_EffVBFS_D);

  //hZ_W_EffVBFC->Add(hZ_W_EffVBFC_N);
  //hZ_W_EffVBFC->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCLo->Add(hZ_W_EffVBFC_NLo);
  hZ_W_EffVBFCLo->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCMe1->Add(hZ_W_EffVBFC_NMe1);
  hZ_W_EffVBFCMe1->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCMe2->Add(hZ_W_EffVBFC_NMe2);
  hZ_W_EffVBFCMe2->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCHi->Add(hZ_W_EffVBFC_NHi);
  hZ_W_EffVBFCHi->Divide(hZ_W_EffVBFC_D);

  //hZ_W_RatioVBF->Add(hZ_DY_EffVBFS);
  //hZ_W_RatioVBF->Divide(hZ_W_EffVBFC);
  hZ_W_RatioVBFLo->Add(hZ_DY_EffVBFSLo);
  hZ_W_RatioVBFLo->Divide(hZ_W_EffVBFCLo);
  hZ_W_RatioVBFMe1->Add(hZ_DY_EffVBFSMe1);
  hZ_W_RatioVBFMe1->Divide(hZ_W_EffVBFCMe1);
  hZ_W_RatioVBFMe2->Add(hZ_DY_EffVBFSMe2);
  hZ_W_RatioVBFMe2->Divide(hZ_W_EffVBFCMe2);
  hZ_W_RatioVBFHi->Add(hZ_DY_EffVBFSHi);
  hZ_W_RatioVBFHi->Divide(hZ_W_EffVBFCHi);

  hZ_W_TotalEffLo->Add(hZ_W_RatioVBFLo);
  hZ_W_TotalEffLo->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffLo->Divide(hZ_W_EffMu);
  hZ_W_TotalEffMe1->Add(hZ_W_RatioVBFMe1);
  hZ_W_TotalEffMe1->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffMe1->Divide(hZ_W_EffMu);
  hZ_W_TotalEffMe2->Add(hZ_W_RatioVBFMe2);
  hZ_W_TotalEffMe2->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffMe2->Divide(hZ_W_EffMu);
  hZ_W_TotalEffHi->Add(hZ_W_RatioVBFHi);
  hZ_W_TotalEffHi->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffHi->Divide(hZ_W_EffMu);

  //hZ_Eff_S_noDPhi->Add(hZ_W_RatioVBF);  
  //hZ_Eff_S_noDPhi->Divide(hZ_W_EffMu);
  hZ_Eff_WS_DPhi->SetBinContent(1,hZ_W_TotalEffLo->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(1,hZ_W_TotalEffLo->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(2,hZ_W_TotalEffMe1->GetBinContent(1));  
  hZ_Eff_WS_DPhi->SetBinError(2,hZ_W_TotalEffMe1->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(3,hZ_W_TotalEffMe2->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(3,hZ_W_TotalEffMe2->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(4,hZ_W_TotalEffHi->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(4,hZ_W_TotalEffHi->GetBinError(1));

  //for(int ibin = 1; ibin <= hZ_Eff_S_DPhi->GetNbinsX(); ++ibin) {
  //  hZ_Eff_S_DPhi->SetBinContent(ibin,hZ_W_TotalEff->GetBinContent(1));
  //  hZ_Eff_S_DPhi->SetBinError  (ibin,hZ_W_TotalEff->GetBinError(1));
  //}
  // Observed 
  hZ_Est_ZC_DPhi->Add(hZ_Data_ZC_DPhi, hZ_BG_ZC_DPhi, 1., -1.);
  // Predicted
  hZ_Est_WC_DPhi->Add(hZ_Data_WC_DPhi, hZ_BG_WC_DPhi, 1., -1.);
  hZ_Est_WS_DPhi->Add(hZ_Est_WC_DPhi,ratioBF);
  hZ_Est_WS_DPhi->Multiply(hZ_Eff_WS_DPhi);

  // print out

  std::cout << std::endl;
  std::cout << "##################################### Cross-check with W mu ctrl region #####################################" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_mumu by histogram  : " << hZ_DY_EffMuMu->GetBinContent(1) << " +/- " << hZ_DY_EffMuMu->GetBinError(1) << std::endl;
  std::cout << "  eps_mu by histogram    : " << hZ_W_EffMu->GetBinContent(1) << " +/- " << hZ_W_EffMu->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi < 1.0" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSLo->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSLo->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCLo->GetBinContent(1) << " +/- " << hZ_W_EffVBFCLo->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFLo->GetBinContent(1) << " +/- " << hZ_W_RatioVBFLo->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(1) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(1) << " +/- " << hZ_W_C_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(1) << " +/- " << hZ_BG_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(1) << " +/- " << hZ_Data_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Data - BG              : " << hZ_Est_WC_DPhi->GetBinContent(1) << " +/- " << hZ_Est_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(1) << " +/- " << hZ_Est_WS_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(1) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 1.0 dphi < 1.8" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSMe1->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSMe1->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCMe1->GetBinContent(1) << " +/- " << hZ_W_EffVBFCMe1->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFMe1->GetBinContent(1) << " +/- " << hZ_W_RatioVBFMe1->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(2) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(2) << " +/- " << hZ_W_C_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(2) << " +/- " << hZ_BG_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(2) << " +/- " << hZ_Data_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Data - BG     	 : " << hZ_Est_WC_DPhi->GetBinContent(2) << " +/- " << hZ_Est_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(2) << " +/- " << hZ_Est_WS_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(2) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 1.8 dphi < 2.6" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSMe2->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSMe2->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCMe2->GetBinContent(1) << " +/- " << hZ_W_EffVBFCMe2->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFMe2->GetBinContent(1) << " +/- " << hZ_W_RatioVBFMe2->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(3) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(3) << " +/- " << hZ_W_C_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(3) << " +/- " << hZ_BG_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(3) << " +/- " << hZ_Data_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Data - BG       	 : " << hZ_Est_WC_DPhi->GetBinContent(3) << " +/- " << hZ_Est_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(3) << " +/- " << hZ_Est_WS_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Observed Zmumu       	 : " << hZ_Est_ZC_DPhi->GetBinContent(3) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 2.6" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSHi->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSHi->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCHi->GetBinContent(1) << " +/- " << hZ_W_EffVBFCHi->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFHi->GetBinContent(1) << " +/- " << hZ_W_RatioVBFHi->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(4) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(4) << " +/- " << hZ_W_C_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(4) << " +/- " << hZ_BG_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(4) << " +/- " << hZ_Data_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Data - BG region       : " << hZ_Est_WC_DPhi->GetBinContent(4) << " +/- " << hZ_Est_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(4) << " +/- " << hZ_Est_WS_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(4) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "#####################################################################################" << std::endl;
  std::cout << std::endl;

  // draw control plots
  std::string pdfName;

  gStyle->SetOptStat(0);
  gStyle->SetOptFit(111111111);

  double x1[4]  = {0.5, 1.4, 2.2, 2.6 + (TMath::Pi()-2.6)/2};
  double ex1[4] = {0.5, 0.4, 0.4, (TMath::Pi()-2.6)/2};
  double y1[4],ey1[4],y2[4],ey2[4],y3[4],ey3[4];
  double diff[4],ediff[4];
  double frac[4],efrac[4];
  double y_syst[4],e_syst[4];

  for(int i=0; i<4; ++i) {
        y1[i]  = hZ_Est_WS_DPhi->GetBinContent(i+1);     //Prediction
        ey1[i] = hZ_Est_WS_DPhi->GetBinError(i+1);
        y2[i]  = hZ_Est_ZC_DPhi->GetBinContent(i+1);     //Observation
        ey2[i] = hZ_Est_ZC_DPhi->GetBinError(i+1); 
	y3[i]  = hZ_DY_C_DPhi->GetBinContent(i+1);       //MC Prediction
        ey3[i] = hZ_DY_C_DPhi->GetBinError(i+1);

	diff[i]  = y1[i]-y2[i];
	ediff[i] = sqrt(ey1[i]*ey1[i] + ey2[i]*ey2[i]);
        y_syst[i] = 0.;
        e_syst[i] = 0.21;

	if(y1[i] > 0) frac[i]  = (y1[i]-y2[i])/y2[i];
	efrac[i] = (y1[i]/y2[i])*sqrt(pow(ey1[i]/y1[i],2) + pow(ey2[i]/y2[i],2));
  }

  TGraphErrors *graph1 = new TGraphErrors(4,x1,y1,ex1,ey1);
  TGraphErrors *graph2 = new TGraphErrors(4,x1,y2,ex1,ey2);
  TGraphErrors *graph6 = new TGraphErrors(4,x1,y3,ex1,ey3);
  TGraphErrors *graph3 = new TGraphErrors(4,x1,diff,ex1,ediff);
  TGraphErrors *graph4 = new TGraphErrors(4,x1,frac,ex1,efrac);
  TGraphErrors *graph5 = new TGraphErrors(4,x1,y_syst,ex1,e_syst);
  TH1D *h = new TH1D("h", "", 1, 0, TMath::Pi());

  TCanvas canvas;
  canvas.SetCanvasSize(canvas.GetWindowWidth(), 1.2*canvas.GetWindowHeight());

  graph1->SetTitle("");
  graph1->SetMarkerStyle(20);
  graph1->SetMarkerSize(0.9);
  graph1->SetLineColor(kRed);
  graph1->SetMarkerColor(kRed);  
  graph1->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  graph1->GetXaxis()->SetRangeUser(0,TMath::Pi());
  graph1->GetYaxis()->SetTitle("N(Z#rightarrow #mu#mu)");
  graph1->GetYaxis()->SetRangeUser(0,45);
  graph1->Draw("AP");  
  graph2->SetMarkerStyle(20);
  graph2->SetMarkerSize(0.9);
  graph2->SetLineColor(kBlue);
  graph2->SetMarkerColor(kBlue);
  graph2->Draw("P same");
  graph6->SetMarkerStyle(20);
  graph6->SetMarkerSize(0.9);
  graph6->SetLineColor(kViolet);
  graph6->SetMarkerColor(kViolet);
  graph6->Draw("P same");

  TLegend leg(0.12,0.67,0.37,0.88);
  leg.SetBorderSize(0);
  leg.SetFillColor(0);
  leg.AddEntry(graph1,"predicted (data)","P");
  leg.AddEntry(graph2,"observed (data)","P");
  leg.AddEntry(graph6,"predicted (MC)","P");
  leg.Draw();

  pdfName= oDir + std::string("/Zmumu_num.pdf");
  canvas.Print(pdfName.c_str());

  h->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  h->GetYaxis()->SetTitle("Predicted - Observed");
  h->GetYaxis()->SetRangeUser(-20,20);
  h->SetLineColor(kBlue);
  h->Draw();
  graph3->SetMarkerStyle(20);
  graph3->SetMarkerSize(0.9);
  graph3->SetMarkerColor(kGreen-2);
  TF1 *f1 = new TF1("f1","pol0",0,TMath::Pi());
  graph3->Fit("f1","R");
  h->Draw();
  graph3->Draw("SAMEP");

  pdfName= oDir + std::string("/Zmumu_diff.pdf");
  canvas.Print(pdfName.c_str());

  h->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  h->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h->GetYaxis()->SetTitleOffset(1.2);
  h->GetYaxis()->SetRangeUser(-2,2);
  h->SetLineColor(kBlue);
  h->SetLineWidth(2);
  h->Draw();
  graph5->SetLineColor(kGray+2);
  graph5->SetLineWidth(0);
  graph5->SetFillColor(kGray+2);
  graph5->SetFillStyle(3002);
  graph4->SetMarkerStyle(20);
  graph4->SetMarkerSize(1.2);
  graph4->SetMarkerColor(kGreen-2);
  graph4->Fit("f1","R");
  h->Draw();
  graph5->Draw("2 same");
  graph4->Draw("P same");

  TLegend leg2(0.12,0.67,0.40,0.87);
  leg2.SetBorderSize(0);
  leg2.SetFillColor(0);
  leg2.AddEntry(f1,"pol0 fit (0 < #Delta #phi_{jj} < #pi)","l");
  leg2.AddEntry(graph5,"Systematic error","f");
  leg2.Draw();
  pdfName= oDir + std::string("/Zmumu_frac.pdf");
  canvas.Print(pdfName.c_str());

  //store histograms
  ofile->cd();
  hZ_DY_C_DPhi->Write("",TObject::kOverwrite);
  hZ_BG_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_Data_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_W_C_DPhi->Write("",TObject::kOverwrite);
  hZ_BG_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Data_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_WS_DPhi->Write("",TObject::kOverwrite);
  hZ_Eff_WS_DPhi->Write("",TObject::kOverwrite);
  hZ_DY_EffMuMu_D->Write("",TObject::kOverwrite);
  hZ_DY_EffMuMu_N->Write("",TObject::kOverwrite);
  hZ_W_EffMu_D->Write("",TObject::kOverwrite);
  hZ_W_EffMu_N->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_D->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NLo->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NMe1->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NMe2->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NHi->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_D->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NLo->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NMe1->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NMe2->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NHi->Write("",TObject::kOverwrite);
  hZ_W_EffMu->Write("",TObject::kOverwrite);
  //hZ_DY_EffVBFS->Write("",TObject::kOverwrite);
  //hZ_W_EffVBFC->Write("",TObject::kOverwrite);
  //hZ_W_RatioVBF->Write("",TObject::kOverwrite);
  //hZ_W_TotalEff->Write("",TObject::kOverwrite);
 
  ofile->Close();    

}
Exemplo n.º 6
0
void combination() {


    gROOT->SetStyle("Plain");

    gStyle->SetPalette(1);
    gStyle->SetOptStat(1111111);  // Show overflow, underflow + SumOfWeights
    //  gStyle->SetStatStyle(0); // for a completely transparent stat box
    gStyle->SetOptFit(111110);
    gStyle->SetOptFile(1);

    gStyle->SetMarkerStyle(20);
    gStyle->SetMarkerSize(.3);
    gStyle->SetMarkerColor(1);

    gStyle->SetTitleBorderSize(0);  // no border around histogram title (font size can't be calculated anyways ...)

    gROOT->ForceStyle();
    TCanvas *c0 = new TCanvas("c0","--c0--",472,0,800,800);
    gStyle->SetOptStat(0);

    TFile resojet("resojetvspt.root");
    TFile resophot("resophotvspt.root");
    TFile deltapt("deltaptnormvspt.root");
    TFile deltaptjet("deltaptjetnormvspt.root");
    TFile ratiopt("ratioptnormvspt.root");
    deltapt.cd();
    resosum->SetMaximum(30);
    resosum->SetAxisRange(40.,700.);
    resosum->SetMarkerSize(.7);
    resosum->SetTitle("");
    resosum->SetXTitle("p_{T}[GeV/c]");
    resosum->SetYTitle("resolution(p_{T})%");
    resosum->SetTitleOffset(1.,"Y");
    resosum->Draw();
    deltaptjet.cd();
    resosum->SetLineColor(kBlue);
    resosum->SetMarkerColor(kBlue);
    resosum->SetMarkerSize(.7);
    resosum->Draw("same");
    resojet.cd();
    resosum->SetLineColor(kRed);
    resosum->SetMarkerColor(kRed);
    resosum->SetMarkerSize(.7);
    resosum->SetMaximum(30);
    resosum->SetAxisRange(40.,700.);
    resosum->SetTitle("");
    resosum->SetXTitle("p_{T}[GeV/c]");
    resosum->SetYTitle("resolution(p_{T})%");
    resosum->SetTitleOffset(1.,"Y");
    resosum->Draw("same");
    resophot.cd();
    resosum->SetLineColor(kGreen);
    resosum->SetMarkerColor(kGreen);
    resosum->SetMarkerSize(.7);
    resosum->Draw("same");
    deltapt.cd();
    resosum->Draw("same");

    TH1D p1;
    TH1D p2;
    TH1D p3;
    TH1D p4;
    p1.SetMarkerStyle(8);
    p1.SetMarkerSize(1.4);
    p2.SetLineColor(kBlue);
    p2.SetMarkerColor(kBlue);
    p2.SetMarkerStyle(8);
    p2.SetMarkerSize(1.4);
    p3.SetLineColor(kRed);
    p3.SetMarkerColor(kRed);
    p3.SetMarkerStyle(8);
    p3.SetMarkerSize(1.4);
    p4.SetLineColor(kGreen);
    p4.SetMarkerColor(kGreen);
    p4.SetMarkerStyle(8);
    p4.SetMarkerSize(1.4);

    TLegendEntry *legge;
    TLegend *leg;
    leg = new TLegend(0.3,0.65,0.65,0.85);
    leg->SetFillStyle(0);
    leg->SetBorderSize(0.);
    leg->SetTextSize(0.035);
    leg->SetFillColor(0);
    legge = leg->AddEntry(&p1, "jet-gamma", "p");
    legge = leg->AddEntry(&p2, "gamma(MC)-jet(MC)", "p");
    legge = leg->AddEntry(&p3, "jet-jet(MC)", "p");
    legge = leg->AddEntry(&p4, "gamma-gamma(MC)", "p");
    leg->Draw();


    c0->SaveAs("reso_combination.eps");
    c0->SaveAs("reso_combination.gif");

    resojet.cd();
    resosum->Draw();

    c0->SaveAs("resojetonly_combination.eps");
    c0->SaveAs("resojetonly_combination.gif");


    deltapt.cd();
    biassum->SetMaximum(15);
    biassum->SetAxisRange(40.,700.);
    biassum->SetMarkerSize(.7);
    biassum->SetTitle("");
    biassum->SetXTitle("p_{T}[GeV/c]");
    biassum->SetYTitle("peak position(p_{T})%");
    biassum->SetTitleOffset(1.,"Y");
    biassum->Draw();
    deltaptjet.cd();
    biassum->SetLineColor(kBlue);
    biassum->SetMarkerColor(kBlue);
    biassum->SetMarkerSize(.7);
    biassum->Draw("same");
    resojet.cd();
    biassum->SetMaximum(15);
    biassum->SetAxisRange(40.,700.);
    biassum->SetMarkerSize(.7);
    biassum->SetTitle("");
    biassum->SetXTitle("p_{T}[GeV/c]");
    biassum->SetYTitle("peak position(p_{T})%");
    biassum->SetTitleOffset(1.,"Y");
    biassum->SetLineColor(kRed);
    biassum->SetMarkerColor(kRed);
    biassum->SetMarkerSize(.7);
    biassum->Draw("same");
    resophot.cd();
    biassum->SetLineColor(kGreen);
    biassum->SetMarkerColor(kGreen);
    biassum->SetMarkerSize(.7);
    biassum->Draw("same");
    deltapt.cd();
    biassum->Draw("same");

    TLegendEntry * legge2;
    TLegend leg2(0.33,0.15,0.65,0.35);;
    leg2.SetFillStyle(0);
    leg2.SetBorderSize(0.);
    leg2.SetTextSize(0.035);
    leg2.SetFillColor(0);
    legge2 = leg2.AddEntry(&p1, "jet-gamma", "p");
    legge2 = leg2.AddEntry(&p2, "gamma(MC)-jet(MC)", "p");
    legge2 = leg2.AddEntry(&p3, "jet-jet(MC)", "p");
    legge2 = leg2.AddEntry(&p4, "gamma-gamma(MC)", "p");
    leg2.Draw();

    c0->SaveAs("bias_combination.eps");
    //  c0->SaveAs("bias_combination.gif");

    deltapt.cd();
    biassum->Draw();
    c0->SaveAs("biasonly_combination.eps");
    //  c0->SaveAs("biasonly_combination.gif");


    resojet.cd();
    biassum->Draw();
    c0->SaveAs("biasjetonly_combination.eps");
    //   c0->SaveAs("biasjetonly_combination.gif");

    ratiopt.cd();
    biassum->SetMaximum(1.0999);
    biassum->SetMinimum(0.3);
    biassum->SetAxisRange(40.,700.);
    biassum->SetMarkerSize(.7);
    biassum->SetTitle("");
    biassum->SetXTitle("p_{T}(#gamma)[GeV/c]");
    biassum->SetYTitle("#frac{p_{T}(jet)}{p_{T}(#gamma)}");
    biassum->SetTitleOffset(.8,"Y");
    biassum->Draw();
    c0->SaveAs("ratioonly_combination.eps");
    //  c0->SaveAs("ratioonly_combination.gif");


}
Exemplo n.º 7
0
void* Monitoring::MonitorThread(void* arg){
  
  //std::cout<<"d1"<<std::endl;
  monitor_thread_args* args= static_cast<monitor_thread_args*>(arg);  

  std::string outpath=args->outputpath;
  zmq::socket_t Ireceive (*(args->context), ZMQ_PAIR);
  Ireceive.connect("inproc://MonitorThread");
  
  //  std::vector<CardData*> carddata;

  std::map<int,std::vector<TH1F> > PedTime;
  std::map<int,std::vector<TH1F> > PedRMSTime;
  std::vector<TH1F> rates;
  std::vector<TH1F> averagesize;

  std::vector<TH1I> tfreqplots;
  std::map<int,std::vector<std::vector<float > > > pedpars;
  TCanvas c1("c1","c1",600,400);
  
  
  bool running=true;
  bool init=true;    

  std::vector<PMT> PMTInfo;
  /////////////////// Connect to sql ///////////////////////
//std::cout<<"d2"<<std::endl;

  pqxx::connection *C;
  std::stringstream tmp;
  tmp<<"dbname=annie"<<" hostaddr=127.0.0.1"<<" port=5432" ;
  C=new pqxx::connection(tmp.str().c_str());
  if (C->is_open()) {
    // std::cout << "Opened database successfully: " << C->dbname() << std::endl;
  }
  else {
    std::cout << "Can't open database" << std::endl;
    return false;
  }

  tmp.str("");

  pqxx::nontransaction N(*C);

  tmp<<"select gx,gy,gz,vmecard,vmechannel from pmtconnections order by channel; ";


  /* Execute SQL query */
  pqxx::result R( N.exec( tmp.str().c_str() ));

  //pqxx::result::const_iterator c = R.begin();


  ///////// Fill PMT Info//////////////// 
  for ( pqxx::result::const_iterator c = R.begin(); c != R.end(); ++c) {

    PMT tmp;
    tmp.gx= c[0].as<int>();
    tmp.gy= c[1].as<int>();
    tmp.gz= c[2].as<int>();
    tmp.card= c[3].as<int>();
    tmp.channel= c[4].as<int>()-1;
    PMTInfo.push_back(tmp);
  }

//std::cout<<"d3"<<std::endl;

  
  while (running){
    //std::cout<<"d4"<<std::endl;

    
    zmq::message_t comm;
    Ireceive.recv(&comm);
	
    std::istringstream iss(static_cast<char*>(comm.data()));
    std::string arg1="";
    iss>>arg1;

    //std::cout<<"d5"<<std::endl;
    

    if(arg1=="Data"){

      ////////// Setting up plots/////////
      std::vector<TGraph2D*> mg;
      TH2I EventDisplay ("Event Display", "Event Display", 10, -1, 8, 10, -1, 8);
      TH2I RMSDisplay ("RMS Display", "RMS Display", 10, -1, 8, 10, -1, 8);
      std::vector<TH1F> temporalplots;
      std::vector<TH1I> freqplots;
      CardData* carddata;
      int size=0;
      iss>>size;  
      
      //freqplots.clear();
  //std::cout<<"d6"<<std::endl;
  
      for(int i=0;i<size;i++){
	//std::cout<<"d7"<<std::endl;

	long long unsigned int pointer;
	iss>>std::hex>>pointer;
	
	carddata=(reinterpret_cast<CardData *>(pointer));
	
	if(init){ ////make initial freq  plot and ped vector ped time and ped rms////
	  for(int j=0;j<carddata->channels;j++){
	    std::stringstream tmp;
	    tmp<<"Channel "<<(i*4)+j<<" frequency";
	    TH1I tmpfreq(tmp.str().c_str(),tmp.str().c_str(),10,0,9);
	    tfreqplots.push_back(tmpfreq);

	    tmp.str("");
	    tmp<<"Channel "<<(i*4)+j<<" Pedistal";
	    TH1F tmppedtime(tmp.str().c_str(),tmp.str().c_str(),100,0,99);
	    PedTime[carddata->CardID].push_back(tmppedtime);

	    tmp.str("");
	    tmp<<"Channel "<<(i*4)+j<<" Pedistal RMS";
	    TH1F tmppedrmstime(tmp.str().c_str(),tmp.str().c_str(),100,0,99);
	    PedRMSTime[carddata->CardID].push_back(tmppedrmstime);
	    std::vector<float> tmppedpars;
	    tmppedpars.push_back(0);
	    tmppedpars.push_back(0);
	    pedpars[carddata->CardID].push_back(tmppedpars);
	  }
	  if(i==size-1)init=false;
	}

	//std::cout<<"d8"<<std::endl;

	//	std::cout<<"d1"<<std::endl;

	///////Make temporal plot //////////
	for(int j=0;j<carddata->channels;j++){
	  std::stringstream tmp;
	  tmp<<"Channel "<<(i*4)+j<<" temporal";
	  
	  TH1F temporal(tmp.str().c_str(),tmp.str().c_str(),carddata->buffersize,0,carddata->buffersize-1);
	  long sum=0;

	  //////////Make freq plot///////////////
	  tmp.str("");
	  tmp<<"Channel "<<(i*4)+j<<" frequency";
	  TH1I freq(tmp.str().c_str(),tmp.str().c_str(),200,200,399);

	
	  //  std::cout<<"d2"<<std::endl;

	  //std::cout<<"d9"<<std::endl;

	
	  ///// Calculate sum for event dispkay and fill freq plots /////////


	for(int k=0;k<carddata->buffersize;k++){
	  //std::cout<<"d10"<<std::endl;

	  //	  std::cout<<"i="<<i<<" j="<<j<<std::endl;
	  //std::cout<<"d2.5 "<<(i*4)+j<<" feqplot.size = "<<freqplots.size()<<std::endl;
	  if(carddata->Data[(j*carddata->buffersize)+k]>pedpars[carddata->CardID].at(j).at(0)+(pedpars[carddata->CardID].at(j).at(1)*5))sum+=carddata->Data[(j*carddata->buffersize)+k];  
	  freq.Fill(carddata->Data[(j*carddata->buffersize)+k]);

	  //temporal.SetBinContent(k,carddata->Data[(j*carddata->buffersize)+k]);	      
	}

	freqplots.push_back(freq);
	//////// find pedistall fill ped temporals//////////
	freq.Fit("gaus");
	TF1 *gaus = freq.GetFunction("gaus");
        pedpars[carddata->CardID].at(j).at(0)=(gaus->GetParameter(1));
        pedpars[carddata->CardID].at(j).at(1)=(gaus->GetParameter(2));
	gaus->SetLineColor(j+1);
       
	//std::cout<<"d11"<<std::endl;

	for(int bin=99;bin>0;bin--){
	  PedTime[carddata->CardID].at(j).SetBinContent(bin,PedTime[carddata->CardID].at(j).GetBinContent(bin-1));
	  PedRMSTime[carddata->CardID].at(j).SetBinContent(bin,PedRMSTime[carddata->CardID].at(j).GetBinContent(bin-1));
	}
	PedTime[carddata->CardID].at(j).SetBinContent(0, pedpars[carddata->CardID].at(j).at(0));
	PedRMSTime[carddata->CardID].at(j).SetBinContent(0, pedpars[carddata->CardID].at(j).at(1));

	//////// fill temporal plot/////////
	for(int k=0;k<carddata->buffersize/4;k++){
	  //std::cout<<"d12"<<std::endl;

          //std::cout<<"j*4 = "<<j*4<<std::endl;
          //std::cout<<"(i*BufferSize)+(j*4) = "<<(i*BufferSize)+(j*4)<<std::endl;
          //std::cout<<"i*BufferSize)+(j*4)+(BufferSize/2) = "<<(i*BufferSize)+(j*4)+(BufferSize/2)<<std::endl;
          //std::cout<<"(i*BufferSize)+(j*4)+(BufferSize/2)+1 = "<<(i*BufferSize)+(j*4)+(BufferSize/2)+1<<std::endl;
	  int offset=pedpars[carddata->CardID].at(j).at(0);
          double conversion=2.415/pow(2.0, 12.0);
	  temporal.SetBinContent(k*4,(carddata->Data[(j*carddata->buffersize)+(k*2)]-offset)*conversion);
	  temporal.SetBinContent((k*4)+1,(carddata->Data[(j*carddata->buffersize)+(k*2)+1]-offset)*conversion);
	  temporal.SetBinContent((k*4)+2,(carddata->Data[(j*carddata->buffersize)+(k*2)+(carddata->buffersize/2)]-offset)*conversion);
	  temporal.SetBinContent((k*4)+3,(carddata->Data[(j*carddata->buffersize)+(k*2)+(carddata->buffersize/2)+1]-offset)*conversion);

        }

	//std::cout<<"d13"<<std::endl;

	//std::cout<<"d3"<<std::endl;
	
	temporalplots.push_back(temporal);


	////// find x,y,z fill event display /////////
	int x=-10;
	int z=-10;
	int y=-10;
	for(int pmt=0;pmt<PMTInfo.size();pmt++){
	  //std::cout<<"d4"<<std::endl;

	  if(PMTInfo.at(pmt).card==carddata->CardID && PMTInfo.at(pmt).channel==j){
	    x=PMTInfo.at(pmt).gx;
	    z=PMTInfo.at(pmt).gz;
	    y=PMTInfo.at(pmt).gy;
	    //std::cout<<"d15"<<std::endl;
	  
}
	}

	/*
	int x=(((i*4)+j)%8);
	int y=(floor(((i*4)+j)/8.0));
	if(x==0 && y==0){x=-10;y=-10;}
	if(x==7 && y==0){x=-10;y=-10;}
	if(x==0 && y==7){x=-10;y=-10;}
	if(x==7 && y==7){x=-10;y=-10;}
	if (y>7){x=-10;y=-10;}
	std::cout<<"i="<<i<<" j="<<j<<" (i*4)+j)="<<((i*4)+j)<<" x="<<x<<" y="<<y<<" sum="<<sum<<std::endl;
	EventDisplay.SetBinContent(x+1,y+1,sum);
	//EventDisplay.SetBinContent(((i*4)+j),sum);
	*/
	//std::cout<<"d16"<<std::endl;

	if(x!=-10 && z!=-10){
	  //std::cout<<"d17"<<std::endl;

	  //std::cout<<"gx = "<<x<<" , gz="<<z<<std::endl;
	  EventDisplay.SetBinContent(x+2,z+2,sum);
	  RMSDisplay.SetBinContent(x+2,z+2,gaus->GetParameter(2)*100);

	  //// Attempted 2ne event display ///
	  TGraph2D *dt=new TGraph2D(1);
	  dt->SetPoint(0,x,z,z);
	  dt->SetMarkerStyle(20);
	  //dt->GetXaxis()->SetRangeUser(-1,8);
	  // dt->GetYaxis()->SetRangeUser(-1,8);
	  //dt->GetZaxis()->SetRangeUser(-1,8);
	  mg.push_back(dt);
	}	
	
	}
	
	//std::cout<<"d18"<<std::endl;

	

	///////Find max freq for scaling//////////

	int maxplot=0;
	long maxvalue=0;
	
	//std::cout<<"i="<<i<<" (i*4)="<<(i*4)<<" (i*4)+4="<<(i*4)+4<<" size="<<freqplots.size()<<std::endl;
	for(int j=(i*4);j<(i*4)+4;j++){
	  if (freqplots.at(j).GetMaximum()>maxvalue){
	    //std::cout<<"d19"<<std::endl;
	 
   maxvalue=freqplots.at(j).GetMaximum();
	    maxplot=j;
	  }
	}
	

	//std::cout<<"d20"<<std::endl;

	////////Find current time and plot frewuency plot

	time_t t = time(0);   // get time now
	struct tm * now = localtime( & t );
	std::stringstream title;
	title<<"Card "<<carddata->CardID<<" frequency: "<<(now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' <<  now->tm_mday<<','<<now->tm_hour<<':'<<now->tm_min<<':'<<now->tm_sec;
	//std::cout<<"d21"<<std::endl;
	freqplots.at(maxplot).SetTitle(title.str().c_str());
	freqplots.at(maxplot).GetXaxis()->SetTitle("ADC Value");
	freqplots.at(maxplot).GetYaxis()->SetTitle("Frequency");	
	freqplots.at(maxplot).SetLineColor((maxplot%4)+1);
	freqplots.at(maxplot).Draw();
	TLegend leg(0.8,0.4,1.0,0.7);
	//leg.SetHeader("The Legend Title");
	
	//std::cout<<"d22"<<std::endl;
for(int j=(i*4);j<(i*4)+4;j++){
  //std::cout<<"d23"<<std::endl;
	  std::stringstream legend;
	  legend<<"Channel "<<j-(i*4);
	  leg.AddEntry(&freqplots.at(j),legend.str().c_str(),"l");	  
	  freqplots.at(j).SetLineColor((j%4)+1);
	  if(j==maxplot){;}//freqplots.at(i).Draw();                                  
	  else freqplots.at(j).Draw("same");
	  
	}
	leg.Draw();
	//std::cout<<"d24"<<std::endl;
	std::stringstream tmp;
	tmp<<outpath<<carddata->CardID<<"freq.jpg";
	c1.SaveAs(tmp.str().c_str());


	//std::cout<<"d25"<<std::endl;



	///////find max tmporal plot for scaling
	
	///temporal
	maxplot=0;
	maxvalue=0;

	//std::cout<<"i="<<i<<" (i*4)="<<(i*4)<<" (i*4)+4="<<(i*4)+4<<" size="<<freqplots.size()<<std::endl;
        for(int j=(i*4);j<(i*4)+4;j++){
	  //std::cout<<"d26"<<std::endl;
          if (temporalplots.at(j).GetMaximum()>maxvalue){
            maxvalue=temporalplots.at(j).GetMaximum();
            maxplot=j;
          }
        }

	//std::cout<<"d27"<<std::endl;


	//////// Find time and plot temporal plot

	t = time(0);   // get time now
        now = localtime( & t );
	std::stringstream title2;
        title2<<"Card "<<carddata->CardID<<" Temporal: "<<(now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' <<  now->tm_mday<<','<<now->tm_hour<<':'<<now->tm_min<<':'<<now->tm_sec;
	//std::cout<<"d28"<<std::endl;
        temporalplots.at(maxplot).SetTitle(title2.str().c_str());
	temporalplots.at(maxplot).GetXaxis()->SetTitle("Samples");
	temporalplots.at(maxplot).GetYaxis()->SetTitle("Volate (V)");
        temporalplots.at(maxplot).SetLineColor((maxplot%4)+1);
        temporalplots.at(maxplot).Draw();
	TLegend leg2(0.8,0.4,1.0,0.7);
	//std::cout<<"d29"<<std::endl;
	for(int j=(i*4);j<(i*4)+4;j++){
	  //std::cout<<"d30"<<std::endl;
	  std::stringstream legend;
	  legend<<"Channel "<<j-(i*4);	  
	  leg2.AddEntry(&temporalplots.at(j),legend.str().c_str(),"l");
	  temporalplots.at(j).SetLineColor((j%4)+1);
	  if(j==0){;}
	  else temporalplots.at(j).Draw("same");

	}
	//std::cout<<"d31"<<std::endl;
	leg2.Draw();
	//std::cout<<"d6"<<std::endl;

	std::stringstream tmp2;
	tmp2<<outpath<<carddata->CardID<<"temporal.jpg";
	c1.SaveAs(tmp2.str().c_str());
	//std::cout<<"d32"<<std::endl;


	///////plotting PED time and ped rms time //////
        maxplot=0;
        maxvalue=0;

      
        for(int j=0;j<4;j++){
          //std::cout<<"d26"<<std::endl;
          if ( PedTime[carddata->CardID].at(j).GetMaximum()>maxvalue){
            maxvalue=PedTime[carddata->CardID].at(j).GetMaximum();
            maxplot=j;
          }
        }

	t = time(0);   // get time now
        now = localtime( & t );
	title2.str("");
        title2<<"Card "<<carddata->CardID<<" Pedistal Variation: "<<(now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' <<  now->tm_mday<<','<<now->tm_hour<<':'<<now->tm_min<<':'<<now->tm_sec;
        //std::cout<<"d28"<<std::endl;
        PedTime[carddata->CardID].at(maxplot).SetTitle(title2.str().c_str());
	PedTime[carddata->CardID].at(maxplot).GetXaxis()->SetTitle("Samples");
	PedTime[carddata->CardID].at(maxplot).GetYaxis()->SetTitle("ADC Value");
	PedTime[carddata->CardID].at(maxplot).SetLineColor((maxplot%4)+1);
	PedTime[carddata->CardID].at(maxplot).Draw();
        TLegend leg3(0.8,0.4,1.0,0.7);
        //std::cout<<"d29"<<std::endl;
	for(int j=0;j<4;j++){
          //std::cout<<"d30"<<std::endl;
	  std::stringstream legend;
          legend<<"Channel "<<j;
          leg3.AddEntry(&PedTime[carddata->CardID].at(j),legend.str().c_str(),"l");
	  PedTime[carddata->CardID].at(j).SetLineColor((j%4)+1);
          PedTime[carddata->CardID].at(j).Draw("same");

        }
        //std::cout<<"d31"<<std::endl;
        leg3.Draw();
        //std::cout<<"d6"<<std::endl;

	tmp2.str("");
	tmp2<<outpath<<"plots2/"<<carddata->CardID<<"PedTime.jpg";
	c1.SaveAs(tmp2.str().c_str());



	PedRMSTime[carddata->CardID].at(0).Draw();
	for (int channel=1;channel<4;channel++){
	  PedRMSTime[carddata->CardID].at(channel).Draw("same");
	}
	tmp2.str("");
	tmp2<<outpath<<"plots2/"<<carddata->CardID<<"PedRMSTime.jpg";
	c1.SaveAs(tmp2.str().c_str());

	delete carddata;

	
      } /// size i
      
      /*
      //std::cout<<"d4"<<std::endl;

      int maxplot=0;
      long maxvalue=0;
      for(int i=0;i<freqplots.size();i++){
	if (freqplots.at(i).GetMaximum()>maxvalue){
	  maxvalue=freqplots.at(i).GetMaximum();
	  maxplot=i;
	}
      }

      freqplots.at(maxplot).SetLineColor(maxplot+1);
      freqplots.at(maxplot).Draw();
      for(int i=0;i<freqplots.size();i++){

	//	Double_t scale = 1/freqplots.at(i).GetMaximum();
	//	freqplots.at(i).Scale(scale);
	freqplots.at(i).SetLineColor(i+1);
	if(i==maxplot);//freqplots.at(i).Draw();
	else freqplots.at(i).Draw("same");

	//freqplots.at(i).Scale((1.0/scale));
      }
      std::cout<<"d5"<<std::endl;

      std::stringstream tmp;
      tmp<<outpath<<"freq.jpg";
      c1.SaveAs(tmp.str().c_str());
      
      for(int i=0;i<temporalplots.size();i++){
	temporalplots.at(i).SetLineColor(i+1);
	if(i==0)temporalplots.at(i).Draw();
	else temporalplots.at(i).Draw("same");
	
      }	  

      std::cout<<"d6"<<std::endl;

      std::stringstream tmp2;
      tmp2<<outpath<<"temporal.jpg";
      c1.SaveAs(tmp2.str().c_str());
      */
      //std::cout<<"d33"<<std::endl;


      /////////plot event display /////////

      EventDisplay.Draw("COLZ");
      std::stringstream tmp3;
      tmp3<<outpath<<"0EventDisplay.jpg";
      c1.SaveAs(tmp3.str().c_str());
      

      //std::cout<<"d34 ="<<mg.size()<<std::endl;

/////////plot RMS display /////////
      RMSDisplay.Draw("COLZ");
      tmp3.str("");
      tmp3<<outpath<<"0RMSDisplay.jpg";
      c1.SaveAs(tmp3.str().c_str());

      ///plot  atempted 3d event display///////

      tmp3.str("");
      if(mg.size()>0)      mg.at(0)->Draw();
      for(int plots=1;plots<mg.size();plots++){
	mg.at(plots)->Draw("same");
	//std::cout<<"d35"<<std::endl;
      }
      tmp3<<outpath<<"0EventDisplay3D.jpg";
      //c1.SaveAs(tmp3.str().c_str());





    }
    
    
    
    else if(arg1=="Quit"){