Example #1
0
void mySelector::Terminate()
{
   // The Terminate() function is the last function to be called during
   // a query. It always runs on the client, it can be used to present
   // the results graphically or save the results to file.
  TString option = GetOption();
  TString output = "gr_";
  output += option;
  output += ".root";
  TFile *hfile = new TFile(output,"RECREATE","FONLL CCbar cross section");
  
  hfile->cd();
  const Int_t npoint = eventnumber;
  Float_t x[npoint];
  Float_t y[npoint];
  Float_t ylow[npoint];
  Float_t yup[npoint];
  for(int i=0;i<npoint;i++)
    {
      x[i] = Pt[i];
      y[i] = Central[i];
      ylow[i] = Down[i];
      yup[i] = Up[i];
    }
  //TGraph *grFONLLD0 = new TGraph(npoint,Pt,Central);
  TDirectoryFile *ratioErr = new TDirectoryFile(option,"ratio error of scale pp500 to pp200 ");
  ratioErr->SetName(option);
  ratioErr->Add(hRatio);
  TGraph *grFONLLRatio = new TGraph(npoint,x,y);
  grFONLLRatio->SetName("grFONLLRatio");
  TGraph *grFONLLRatio_u = new TGraph(npoint,x,yup);
  grFONLLRatio_u->SetName("grFONLLRatio_u");
  TGraph *grFONLLRatio_d = new TGraph(npoint,x,ylow);
  grFONLLRatio_d->SetName("grFONLLRatio_d");
  grFONLLRatio->Print();
  ///grFONLLRatio->Write();
  ratioErr->Add(grFONLLRatio);
  grFONLLRatio_u->Print();
  //grFONLLRatio_u->Write();
  ratioErr->Add(grFONLLRatio_u);
  grFONLLRatio_d->Print();
  //grFONLLRatio_d->Write();
  ratioErr->Add(grFONLLRatio_d);
  ratioErr->Write();
  hfile->Print();
  hfile->Close();
  tNow.Set();
  cout<<"----------End of job----------"<<endl;
  tNow.Print();
}
Example #2
0
// ----------------------------------------------------------------------
// create PH vs VCal scans for a grid of phscale and phoffset values
void phOpt(string rootfile = "phOpt.root", string cfgdirectory = "testROC") {
  ConfigParameters *configParameters = ConfigParameters::Singleton();
  
  configParameters->setDirectory(cfgdirectory);
  string cfgFile = configParameters->getDirectory() + string("/configParameters.dat");
  configParameters->readConfigParameterFile(cfgFile);

  
  PixTestParameters *ptp = new PixTestParameters(configParameters->getDirectory() + "/" + configParameters->getTestParameterFileName()); 

  PixSetup *ap = new PixSetup("DEBUG", ptp, configParameters);  

  cout << "pxar: dumping results into " << rootfile << endl;
  TFile *rfile = TFile::Open(rootfile.c_str(), "RECREATE"); 
  
  PixTestFactory *factory = PixTestFactory::instance(); 
  
  PixTest *pt = factory->createTest("DacScan", ap); 
  pt->setDAC("ctrlreg", 4); 
  pt->setParameter("PHmap", "1"); 
  pt->setParameter("DAC", "Vcal"); 
  pt->setParameter("DACLO", "0"); 
  pt->setParameter("DACHI", "255"); 

  int cycle(0);
  TH1D *h1(0); 
  for (unsigned int io = 0; io < 26; ++io) {
    for (unsigned int is = 0; is < 52; ++is) {
      pt->setDAC("phoffset", io*10);
      pt->setDAC("phscale", is*5);
      pt->doTest(); 
      h1 = (TH1D*)rfile->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", cycle)); 
      h1->SetTitle(Form("ph_Vcal_c11_r20_C0_V%d phscale=%d phoffset=%d", cycle, is*5, io*10));
      ++cycle;
    }
  }
  rfile->Print();
  delete pt; 

  rfile->Close();

  ap->killApi();

}
//void MergeMetHists(const int Nfiles=0, 
//							const std::string filenamebase="",
//							const std::string title="", const std::string name="",
//							const float LoX=-1, const float HiX=-1, const int rebin=1,
//							const std::string printfile="", bool debug=false)
void MergeMetHists(const int Nfiles, 
		const std::string filenamebase,
		const std::string title, const std::string name,
		const float LoX=-1, const float HiX=-1, const int rebin=1,
		const std::string printfile="", const int MetSig=4,
		bool debug=false, const bool logy=false)
{
	assert (Nfiles>0 && "Number of files must be > 0");
	assert (name.length()>0 && "Require hist name!");
	assert (filenamebase.length()>0 && "Require base of the file name!");

	std::cout << "MetSig=" << MetSig << std::endl;
	std::cout << "Searching for obj = " << name <<std::endl;

	std::string data_path, bg_path, def_path;
	std::string ue1_path, ue2_path, ue3_path, ue4_path, ue5_path, ue6_path;
	std::string ue7_path, ue8_path, ue9_path;
	std::string jer1_path, jer2_path, jer3_path, jer4_path, jer5_path;
	std::string jer6_path, jer7_path, jer8_path, jer9_path, jer10_path;
	
	data_path="/Ana/MyJetFilter/Hist/Ana_data/";
	bg_path="/Ana/MyJetFilter/Hist/Ana_bckg/";
	def_path="/Ana/MyJetFilter/Hist/Ana_def/";
	ue1_path="/Ana/MyJetFilter/Hist/Ana_ue1/";
	ue2_path="/Ana/MyJetFilter/Hist/Ana_ue2/";
	ue3_path="/Ana/MyJetFilter/Hist/Ana_ue3/";
	ue4_path="/Ana/MyJetFilter/Hist/Ana_ue4/";
	ue5_path="/Ana/MyJetFilter/Hist/Ana_ue5/";
	ue6_path="/Ana/MyJetFilter/Hist/Ana_ue6/";
	ue7_path="/Ana/MyJetFilter/Hist/Ana_ue7/";
	ue8_path="/Ana/MyJetFilter/Hist/Ana_ue8/";
	ue9_path="/Ana/MyJetFilter/Hist/Ana_ue9/";
	jer1_path="/Ana/MyJetFilter/Hist/Ana_jer1/";
	jer2_path="/Ana/MyJetFilter/Hist/Ana_jer2/";
	jer3_path="/Ana/MyJetFilter/Hist/Ana_jer3/";
	jer4_path="/Ana/MyJetFilter/Hist/Ana_jer4/";
	jer5_path="/Ana/MyJetFilter/Hist/Ana_jer5/";
	jer6_path="/Ana/MyJetFilter/Hist/Ana_jer6/";
	jer7_path="/Ana/MyJetFilter/Hist/Ana_jer7/";
	jer8_path="/Ana/MyJetFilter/Hist/Ana_jer8/";
	jer9_path="/Ana/MyJetFilter/Hist/Ana_jer9/";
	jer10_path="/Ana/MyJetFilter/Hist/Ana_jer10/";


/*	data_path="Ana_data";
	bg_path="Ana_bckg";
	def_path="Ana_def";
	ue1_path="Ana_ue1";
	ue2_path="Ana_ue2";
	ue3_path="Ana_ue3";
	ue4_path="Ana_ue4";
	ue5_path="Ana_ue5";
	ue6_path="Ana_ue6";
	ue7_path="Ana_ue7";
	ue8_path="Ana_ue8";
	ue9_path="Ana_ue9";
	jer1_path="Ana_jer1";
	jer2_path="Ana_jer2";
	jer3_path="Ana_jer3";
	jer4_path="Ana_jer4";
	jer5_path="Ana_jer5";
	jer6_path="Ana_jer6";
	jer7_path="Ana_jer7";
	jer8_path="Ana_jer8";
	jer9_path="Ana_jer9";
	jer10_path="Ana_jer10";
*/


	std::vector<std::string> vPaths;
	vPaths.push_back(data_path);
	vPaths.push_back(bg_path);
	vPaths.push_back(def_path);
	vPaths.push_back(ue1_path);
	vPaths.push_back(ue2_path);
	vPaths.push_back(ue3_path);
	vPaths.push_back(ue4_path);
	vPaths.push_back(ue5_path);
	vPaths.push_back(ue6_path);
	vPaths.push_back(ue7_path);
	vPaths.push_back(ue8_path);
	vPaths.push_back(ue9_path);
	vPaths.push_back(jer1_path);
	vPaths.push_back(jer2_path);
	vPaths.push_back(jer3_path);
	vPaths.push_back(jer4_path);
	vPaths.push_back(jer5_path);
	vPaths.push_back(jer6_path);
	vPaths.push_back(jer7_path);
	vPaths.push_back(jer8_path);
	vPaths.push_back(jer9_path);
	vPaths.push_back(jer10_path);

	TFile *f = 0;
	TH1 *hist_data = 0, *hist_bg = 0;

	int iNHists = vPaths.size();

	std::vector<TH1*> vHist;
	for (int n= 0; n < iNHists; ++n)
	{
		TH1 *temp=0;
		vHist.push_back(temp);
	}

	int NfilesOpened = 0;

	//for (int i=1; i<=1; ++i) 
	for (int i=1; i<=Nfiles; ++i) 
	{
		std::stringstream file;
		file << filenamebase << i;
		//file << "myhisto_PhoJetAna_Pyth_phojet22_1Njet15_test_040208.root";

		f = new TFile (file.str().c_str());
		if (f->IsZombie())
		{
			std::cout << "ERROR::File " << file.str() << " did not open! Exiting." << std::endl;
			exit (1);
		} else {
			NfilesOpened++;
			if (debug)
			{
				std::cout << "File Added::";
				f->Print();
			}
		}

		gROOT->ls();
		
		TFolder *fold = (TFolder*) gDirectory->FindObjectAny("Ana");
		assert(fold != NULL && "folder null");
		TFolder *dir = (TFolder*) fold->FindObjectAny("MyJetFilter");
		assert(dir != NULL && "dir null");
		TFolder *dir2 = (TFolder*) dir->FindObjectAny("Hist");
		assert(dir2 != NULL && "Hist null");

		//TH1 *h = dynamic_cast<TH1*> (dir3->FindObjectAny(name.c_str()));
		//assert (h!=NULL && "hist null");
		//h->Draw();
		//return;


		for (unsigned int iPath = 0; iPath < vPaths.size(); ++iPath)
		{
			//std::cout << "iPath = " << iPath << std::endl;
			TFolder *dir3 = (TFolder*) dir2->FindObjectAny(vPaths.at(iPath).c_str());
			assert(dir3 != NULL && "data null");
			//f->cd();
			//gDirectory->pwd();
			//f->cd(vPaths.at(iPath).c_str());
			//gDirectory->pwd();
			//if (iPath<2) f->ls();
			//TH1 *hTemp = dynamic_cast<TH1*> (gDirectory->FindObjectAny(name.c_str()));
			std::stringstream histpath;
			histpath << vPaths.at(iPath) << name;
			//TFolder *ana = (TFolder*) gDirectory->FindObjectAny("Ana");
			//assert(ana !=NULL && "Ana folder not found");
			
			//std::cout << "histpath = " << histpath.str() << std::endl;
			TH1 *hTemp = dynamic_cast<TH1*> (dir3->FindObjectAny(name.c_str()));
			assert(hTemp != NULL && "object not found!");

			if (hTemp->GetEntries())	// this has to be done to avoid crashes when adding hists which some how have 'sum=nan' instead of 'sum=0' when they do not have any entries.
			{
				if (! vHist.at(iPath))
				{
					std::string name = hTemp->GetName() + std::string ("_Clone");
					vHist.at(iPath) = dynamic_cast<TH1*>(hTemp->Clone (name.c_str()));
					assert(vHist.at(iPath) != NULL && "Data hist cast failed");
					vHist.at(iPath)->SetDirectory(0);
				} else
				{
					vHist.at(iPath)->Add(hTemp);
				}
			}
		}

		delete f;

	}

/*	assert(vHist.size() == vPaths.size());
	for (int k=0; k < vHist.size(); ++k)
	{
		vHist.at(k)->Print();
	}
*/
	DoSystematics(vHist);
	
	hist_data = vHist.at(0);
	hist_bg = vHist.at(1);

/*	std::cout << "NORMALIZING BG TO DATA " << std::endl;
	double data_int = hist_data->Integral();
	double bg_int = hist_bg->Integral();
	hist_bg->Scale(data_int/bg_int);
	std::cout << "SCALE = " << data_int/bg_int << std::endl;
*/

	if (debug) 
	{
		hist_data->Print("all");
		hist_bg->Print("all");
	}

	std::cout << "Total file added = " << NfilesOpened << std::endl;
	gStyle->SetOptStat("");

	if (hist_data->GetEntries()) 	hist_data->Rebin(rebin);
	if (hist_bg->GetEntries()) hist_bg->Rebin(rebin);

	TH1 *hist_err_copy = NULL;
	std::string bgname = hist_bg->GetName() + std::string ("err_copy");
	hist_err_copy = dynamic_cast<TH1*>(hist_bg->Clone (bgname.c_str()));
	hist_err_copy->SetDirectory(0);

	TH1 *hist_data_copy = NULL;
	std::string dataname = hist_data->GetName() + std::string ("data_copy");
	hist_data_copy = dynamic_cast<TH1*>(hist_data->Clone (dataname.c_str()));
	hist_data_copy->SetDirectory(0);



	float x_loLim, x_hiLim;
	if (LoX >0) x_loLim = LoX;
	else x_loLim = hist_data->GetBinLowEdge(1);

	if (HiX >0) x_hiLim = HiX;
	else x_hiLim = max(FindUpLimit(hist_data), FindUpLimit(hist_bg)) + hist_data->GetBinWidth(1) * 2;
	if (debug)
	{
		std::cout << "min, max = " << x_loLim << ", " << x_hiLim << std::endl;
	}

	float y_hiLim = max(FindUpLimit(hist_data,"Y"), FindUpLimit(hist_bg,"Y"));
	if (logy) y_hiLim *= 10;
	else y_hiLim += y_hiLim * 0.1;




	gStyle->SetCanvasColor (10);
	gStyle->SetCanvasBorderSize (0);
	gStyle->SetCanvasBorderMode (0);
	gStyle->SetPadColor (10);
	gStyle->SetFillColor (10);
	gStyle->SetTitleFillColor (10);
	gStyle->SetTitleBorderSize (0);
	gStyle->SetStatColor (10);
	gStyle->SetStatBorderSize (1);
	gStyle->SetCanvasDefW(1200);
	gStyle->SetCanvasDefH(600);
	int labelfont = 10 * 4 + 2;		//10 * font ID + precision (2 = scalable)
	int titlefont = 10 * 4 + 2;		//10 * font ID + precision (2 = scalable)
	gStyle->SetLabelFont(labelfont,"X");
	gStyle->SetLabelFont(labelfont,"Y");
	gStyle->SetTitleFont(titlefont,"X");
	gStyle->SetTitleFont(titlefont,"Y");
	gStyle->SetLabelSize(0.04,"X");
	gStyle->SetLabelSize(0.027,"Y");
	//gStyle->SetLabelOffset(0.9);
	gStyle->SetTitleSize(0.03,"Y");
	gStyle->SetTitleOffset(1.8,"Y");
	//TGaxis::SetMaxDigits(3);



	hist_data->UseCurrentStyle();
	hist_bg->UseCurrentStyle();

	TCanvas *c1= new TCanvas;
	c1->Divide(2,1);
	c1->cd(1);
	if (logy)
	{
		if (hist_data->GetEntries() > 0 && hist_bg->GetEntries() > 0) gPad->SetLogy();
	}
	gPad->SetTickx();
	gPad->SetTicky();
	gPad->SetGridx();
	gPad->SetGridy();

	TPaveText *tp = new TPaveText(0.02,0.92,0.98,0.99,"NDC");
	tp->SetLineColor(10);
	tp->SetTextFont(titlefont);


	std::string tt(hist_data->GetTitle());
//	hist_data->SetTitle("");
//	hist_bg->SetTitle("");
	if (title.length()>0)
	{

		//tt += " - ";
		tt += title;
		//tt = title;
		if (debug)
		{
			std::cout << tt << std::endl;
		}
		tp->AddText(tt.c_str());
	}

	std::stringstream ytitle, xtitle;
	ytitle << "Events / " << setprecision(3) << hist_data->GetXaxis()->GetBinWidth(1) << " GeV";
	if (debug)
	{
		std::cout << hist_data->GetBinWidth(1) <<std::endl;
	}
	if (name == "MetAll") xtitle << "#slash{E}_{T} (for all events) (GeV)";
	else if (name == "Met") xtitle << "#slash{E}_{T} (after cuts) (GeV)";
	else if (name == "MetSig") xtitle << "#slash{E}_{T} Significance (for all events)";
	else if (name == "Njet15") xtitle << "Njets^{E_{T}>15GeV} (After #slash{E}_{T}-Sig cut)";
	else if (name == "Njet20") xtitle << "Njets^{E_{T}>20GeV} (After #slash{E}_{T}-Sig cut)";
	else if (name == "Njet25") xtitle << "Njets^{E_{T}>25GeV} (After #slash{E}_{T}-Sig cut)";
	else if (name == "Njet30") xtitle << "Njets^{E_{T}>30GeV} (After #slash{E}_{T}-Sig cut)";
	else if (name == "Njet35") xtitle << "Njets^{E_{T}>35GeV} (After #slash{E}_{T}-Sig cut)";

	if (debug)
	{
		std::cout << "xtitle=" << xtitle.str() << std::endl;
	}

	if (debug)
	{
		hist_data->Print();
		std::cout << "bin#\tLoEdge\tdata\tdata_err\tbg_cont\tbg_err"<<std::endl;
		for (unsigned bin = 0; bin <= (unsigned) hist_data_copy->GetNbinsX() + 1; ++ bin)
		{
			float val = hist_data->GetBinContent (bin);
			float err = hist_data->GetBinError(bin);
			float val2 = hist_bg->GetBinContent (bin);
			float err2 = hist_bg->GetBinError(bin);
			float loEdge = hist_data->GetBinLowEdge(bin);

			if (val>0 || err>0 || val2>0 || err2>0)
				std::cout << bin << "\t" << loEdge <<"\t" << val << "\t" << err << "\t\t" << val2 << "\t" << err2 << std::endl;
		}
	}

	hist_data->GetXaxis()->SetTitle(xtitle.str().c_str());
	if (name.find("Njet") == std::string::npos) hist_data->GetYaxis()->SetTitle(ytitle.str().c_str());
	hist_data->GetXaxis()->CenterTitle(true);
	hist_data->GetYaxis()->CenterTitle(true);
	hist_bg->GetXaxis()->SetTitle(xtitle.str().c_str());
	if (name.find("Njet") == std::string::npos) hist_bg->GetYaxis()->SetTitle(ytitle.str().c_str());
	hist_bg->GetXaxis()->CenterTitle(true);
	hist_bg->GetYaxis()->CenterTitle(true);

	//temp
	x_loLim = 0; x_hiLim = 200;
	hist_data->GetXaxis()->SetRangeUser(x_loLim, x_hiLim);
	hist_bg->GetXaxis()->SetRangeUser(x_loLim, x_hiLim);

	hist_data->SetMinimum(0.1);
	hist_bg->SetMinimum(0.1);
	hist_data->SetMaximum(y_hiLim);
	hist_bg->SetMaximum(y_hiLim);


	hist_data->SetMarkerStyle(8);
	hist_data->SetMarkerSize(1.0);
	hist_data->SetMarkerColor(kBlue);
	hist_data->SetLineColor(kBlue);
	hist_bg->SetMarkerColor(kRed);
	hist_bg->SetLineColor(kRed);
	hist_bg->SetFillColor(kRed);
	hist_data->SetTitleSize(0.04);
	hist_bg->SetTitleSize(0.04);
	TH1* hist_bg_copy = dynamic_cast<TH1*>(hist_bg->Clone ("hist_bg_BlkLine"));
	hist_bg_copy->SetLineColor(kBlack);
	hist_bg_copy->SetLineWidth(2);
	hist_bg_copy->SetFillColor(0);
	//hist_bg_copy->Draw("L");
	//hist_bg->Draw("sameE2");
//	hist_bg->SetFillColor(10);
//	hist_bg->SetLineColor(10);
	hist_bg->Draw("E2");
	hist_bg_copy->Draw("SAME HIST");
	hist_data->Draw("sameP");
	//tp->Draw();

	TLegend *leg = new TLegend (0.4,0.8,0.90,0.90);
	leg->SetTextFont(42);
	leg->SetTextSize(0.025);
	leg->SetBorderSize (1);
	leg->SetFillColor (10);

	//std::stringstream leg_data, leg_bg;
	//leg_data << "Data (E=" << hist_data->GetEntries() << " M=" << hist_data->GetMean()
	//		<< " R=" << hist_data->GetRMS() << ")";
	//leg_bg << "Bkg (E=" << hist_bg->GetEntries() << " M=" << hist_bg->GetMean()
	//		<< " R=" << hist_bg->GetRMS() << ")";

	//leg->AddEntry(hist_data,leg_data.str().c_str());
	//leg->AddEntry(hist_bg,leg_bg.str().c_str());
	//leg->AddEntry(hist_data,"Data (Measured) (DET Jets) ");
	//leg->AddEntry(hist_bg, "MC Prediction (HAD Jets, Norm to Data)");
	leg->AddEntry(hist_data,"Data (Measured)");
	leg->AddEntry(hist_bg, "MC Prediction");
	leg->Draw();

	// now to make the ratio plots
	for (unsigned bin = 0; bin <= (unsigned) hist_data_copy->GetNbinsX() + 1; ++ bin)
	{
		const float val = hist_err_copy->GetBinContent (bin);
		const float scale = val ? 1. / val : 0;
		hist_data_copy->SetBinContent (bin, (hist_data_copy->GetBinContent (bin) - val) * scale);
		hist_data_copy->SetBinError (bin, hist_data_copy->GetBinError (bin) * scale);
	};
	for (unsigned bin = 0; bin <= (unsigned) hist_err_copy->GetNbinsX() + 1; ++ bin)
	{
		float value = hist_err_copy->GetBinContent (bin);
		float error = hist_err_copy->GetBinError (bin);
		hist_err_copy->SetBinError (bin, value ? error / value : 0);
		hist_err_copy->SetBinContent (bin, 0);
	};


	/*
		TH1 *hist_ratio = NULL;
		std::string myname = hist_data->GetName() + std::string ("_copy");
		hist_ratio = dynamic_cast<TH1*>(hist_data->Clone (myname.c_str()));
		hist_ratio->Divide(hist_bg);
	 */


	hist_data_copy->UseCurrentStyle();
	hist_err_copy->UseCurrentStyle();
	//new TCanvas();
	c1->cd(2);
	gPad->SetTickx();
	gPad->SetTicky();
	gPad->SetGridx();
	gPad->SetGridy();
	hist_data_copy->SetTitle("");
	hist_err_copy->SetTitle("");
	//hist_data_copy->SetTitle(tt.c_str());
	//hist_err_copy->SetTitle(tt.c_str());
	hist_data_copy->GetXaxis()->SetTitle(xtitle.str().c_str());
	hist_err_copy->GetXaxis()->SetTitle(xtitle.str().c_str());
	hist_data_copy->GetXaxis()->SetRangeUser(x_loLim, x_hiLim);
	hist_err_copy->GetXaxis()->SetRangeUser(x_loLim, x_hiLim);
	float fRatioHist_ymax = 1.0;
	float fRatioHist_ymin = -1.0;
	hist_data_copy->SetMinimum(fRatioHist_ymin);
	hist_data_copy->SetMaximum(fRatioHist_ymax);
	hist_err_copy->SetMinimum(fRatioHist_ymin);
	hist_err_copy->SetMaximum(fRatioHist_ymax);
	std::stringstream ratio_ytitle;
	ratio_ytitle << "(Data Measured - MC Prediction) / MC Prediction";
	hist_data_copy->GetYaxis()->SetTitle(ratio_ytitle.str().c_str());
	hist_err_copy->GetYaxis()->SetTitle(ratio_ytitle.str().c_str());
	//hist_data_copy->SetTitle(ratio_ytitle.str().c_str());
	//hist_err_copy->SetTitle(ratio_ytitle.str().c_str());

	hist_data_copy->GetXaxis()->CenterTitle(true);
	hist_data_copy->GetYaxis()->CenterTitle(true);
	hist_err_copy->GetXaxis()->CenterTitle(true);
	hist_err_copy->GetYaxis()->CenterTitle(true);
	//	hist_data->GetYaxis()->SetRangeUser(;
	////	hist_bg->SetMinimum(0.1);


	hist_data_copy->SetLineColor(kBlue);
	hist_data_copy->SetMarkerColor(kBlue);
	hist_data_copy->SetMarkerStyle (8);
	hist_data_copy->SetMarkerSize(1.0);
	hist_err_copy->SetFillColor(kRed);
	hist_err_copy->SetFillStyle(3002);
	hist_data_copy->Draw("P");	
	hist_err_copy->Draw("same E2");	
	//tp->Draw();


	c1->cd();
	if (printfile.length()>0)
	{
		c1->Print(printfile.c_str());
	}

	DebugSystError(hist_data,hist_bg, hist_data_copy, hist_err_copy);
	

}
int main(int argc, char ** argv) {
	TStopwatch comulativeWatch;
	comulativeWatch.Start(true);
	readInputs(argc,argv);
	cout<<"Currrent Subversion Revision: "<<SVN_REV<<endl;
	cout << "starting main loop.." << endl;
	RunListOK = ReadRunList();
	if(!RunListOK)exit(-1);
	TSystem* sys = gSystem;
	std::string currentDir = sys->pwd();
	for (unsigned int i = 0; i < RunParameters.size(); i++) {
		cout << RunParameters[i].getRunNumber();
		if (i+1 < RunParameters.size()) cout << ", ";
	}
	cout << " will be analysed.." << endl;

	if (!RunListOK) return 0;

	/**Start with Analyising, read RunParameteres of the Run and start analysis with that parameters
	 */
	for (unsigned int i = 0; i < RunParameters.size(); i++) {
		TStopwatch runWatch;
		runWatch.Start(true);
		RunParameters[i].Print();
		bool DO_ALIGNMENT = RunParameters[i].doAlignment();
		bool DO_ALIGNMENTANALYSIS = RunParameters[i].doAlignmentAnalysis();
		bool DO_TRANSPARENT_ANALYSIS = RunParameters[i].doTransparentAnalysis();

		time_t rawtime;
		tm *timestamp;
		time (&rawtime);
		timestamp = gmtime(&rawtime);

		ostringstream logfilename;
		logfilename << "analyse_log_" << RunParameters[i].getRunNumber() << "_" << timestamp->tm_year << "-" << timestamp->tm_mon << "-" << timestamp->tm_mday << "." << timestamp->tm_hour << "." << timestamp->tm_min << "." << timestamp->tm_sec << ".log";
		//
		//		FILE *log;
		//		log = freopen(logfilename.str().c_str(), "w", stdout);


		TSettings *settings = 0;
		cout<<"settings"<<endl;
		settings = new TSettings((TRunInfo*)&RunParameters[i]);

		TResults *currentResults =new TResults(settings);
		currentResults->Print();


		TRawEventSaver *eventSaver;
		eventSaver = new TRawEventSaver(settings);
		eventSaver->saveEvents(RunParameters[i].getEvents());
		delete eventSaver;

		//Calculate Pedestal
		sys->cd(currentDir.c_str());
		TPedestalCalculation* pedestalCalculation;
		pedestalCalculation = new TPedestalCalculation(settings);
		pedestalCalculation->calculateSlidingPedestals(RunParameters[i].getEvents());
		delete pedestalCalculation;

		if(RunParameters[i].doPedestalAnalysis()){
			sys->cd(currentDir.c_str());
			TAnalysisOfPedestal *analysisOfPedestal;
			analysisOfPedestal = new TAnalysisOfPedestal(settings);
			analysisOfPedestal->setResults(currentResults);
			analysisOfPedestal->doAnalysis(RunParameters[i].getEvents());
			delete analysisOfPedestal;
		}

		THTMLGenerator *htmlGen = new THTMLGenerator(settings);
		htmlGen->setFileGeneratingPath(settings->getAbsoluteOuputPath(true));
		htmlGen->setMainPath("./");
		htmlGen->setSubdirPath("");
		htmlGen->setFileName("overview.html");
		htmlGen->addSection("Pedestal","<a href=\"./pedestalAnalysis/pedestal.html\">PEDESTAL</a>");
		htmlGen->addSection("Clustering","<a href=\"./clustering/clustering.html\">CLUSTERING</a>");
		htmlGen->addSection("Selection","<a href=\"./selections/selection.html\">SELECTION</a>");
		htmlGen->addSection("Alignment","<a href=\"./alignment/alignment.html\">ALIGNMENT</a>");
		htmlGen->addSection("Landau","<a href=\"./selectionAnalysis/landaus.html\">LANDAU-DISTRIBUTIONS</a>");
		htmlGen->generateHTMLFile();
		delete htmlGen;

		sys->cd(currentDir.c_str());
		TClustering* clustering;
		clustering=new TClustering(settings);//int seedDetSigma=10,int hitDetSigma=7,int seedDiaSigma=5, int hitDiaSigma=3);
		clustering->ClusterEvents(RunParameters[i].getEvents());
		delete clustering;

		if(RunParameters[i].doClusterAnalysis()){
			sys->cd(currentDir.c_str());
			TAnalysisOfClustering* analysisClustering;
			analysisClustering = new TAnalysisOfClustering(settings);
			analysisClustering->setResults(currentResults);
			analysisClustering->doAnalysis(RunParameters[i].getEvents());
			delete analysisClustering;
		}

		sys->cd(currentDir.c_str());
		TSelectionClass* selectionClass = new TSelectionClass(settings);
		selectionClass->SetResults(currentResults);
		selectionClass->MakeSelection(RunParameters[i].getEvents());
		delete selectionClass;

		if(RunParameters[i].doSelectionAnalysis()){
			sys->cd(currentDir.c_str());
			TAnalysisOfSelection *analysisSelection=new TAnalysisOfSelection(settings);
			analysisSelection->doAnalysis(RunParameters[i].getEvents());
			delete analysisSelection;
		}

		if (DO_ALIGNMENT){
			sys->cd(currentDir.c_str());
			TAlignment *alignment = new TAlignment(settings);
			//			alignment->setSettings(settings);
			//alignment->PrintEvents(1511,1501);
			alignment->Align(RunParameters[i].getEvents());
			delete alignment;
		}

		if(DO_ALIGNMENTANALYSIS){
			sys->cd(currentDir.c_str());
			TAnalysisOfAlignment *anaAlignment;
			anaAlignment=new TAnalysisOfAlignment(settings);
			anaAlignment->doAnalysis(RunParameters[i].getEvents());
			delete anaAlignment;
		}

		if (DO_TRANSPARENT_ANALYSIS) {
			TTransparentAnalysis *transpAna;
			transpAna = new TTransparentAnalysis(settings);
			transpAna->setResults(currentResults);
			transpAna->analyze(RunParameters[i].getEvents(),RunParameters.at(i).getStartEvent());
			delete transpAna;
		}

		if (settings && settings->doTransparentAlignmnet()){
			sys->cd(currentDir.c_str());
			TAlignment *alignment = new TAlignment(settings,TSettings::transparentMode);
			alignment->createTransparentEventVectors(RunParameters[i].getEvents());
			//			alignment->setSettings(settings);
			//alignment->PrintEvents(1511,1501);
			alignment->Align(RunParameters[i].getEvents(),0,TAlignment::diaAlignment);
			delete alignment;

			TTransparentAnalysis *transpAna;
			transpAna = new TTransparentAnalysis(settings,TSettings::transparentMode);
			transpAna->setResults(currentResults);
			transpAna->analyze(RunParameters[i].getEvents(),RunParameters.at(i).getStartEvent());
			delete transpAna;
		}
		cout<<"PRINT RESULTS"<<endl;
		currentResults->Print();
		cout<<"SAVE RESULTS"<<endl;
//		currentResults->saveResults(settings->getResultsRootFilePath());
		TFile* file = new TFile (settings->getResultsRootFilePath().c_str(),"RECREATE");
		file->Print();
		file->ls();
		file->SetName("fileName");
		file->cd();
		cout<<"KEYS: "<<file->GetNkeys()<<endl;
		file->GetListOfKeys()->Print();
		cout<<"Write"<<endl;

		currentResults->Write("test");
		cout<<"CLOSE FILE"<<endl;
		file->Close();
//		if (currentResults){
//			cout<<"DELETE RESULTS"<<endl;
//			delete currentResults;
//			cout<<"#"<<endl;;
//		}
		cout<<"saved results..."<<endl;


		runWatch.Stop();
		cout<<"needed Time for Run "<<RunParameters[i].getRunNumber()<<":"<<endl;
		runWatch.Print();
   
        cout<<"needed Time for Run "<<RunParameters[i].getRunNumber()<<":\n\t"<<flush;
        runWatch.Print();
        if (settings!=NULL){
            cout<<"delete Settings..."<<endl;
            delete settings;
            cout<<"DONE_SETTINGS"<<endl;
                                                           
        }
        cout<<"DONE with Analysis of Run "<< RunParameters[i].getRunNumber();
        cout<<": "<<i+1<<"/"<<RunParameters.size()<<endl;
   }
   cout<<"DONE with Analysis of all "<<RunParameters.size()<<" Runs from RunList.ini"<<endl;
   cout<<"total time for all analysis:\n\t"<<flush;

	cout<<"time for all analysis:"<<endl;
	comulativeWatch.Print();
	cout<<"DONE_ALL"<<endl;

	return 0;
}