Example #1
0
/**
 * \brief Plots the data
 * Plots the data in data_x and data_y
 * \param bool autox , bool autoy actually not needed ;)
 * \return bool on success
 */
bool Plotter::plot(bool autox , bool autoy){
  for(size_t j = 0 ; j< graph.size(); j++){
    try{
      TGraph *gr = graph[j].get();
      for(size_t i = 0; i < data_x.size(); i++){
	gr->SetPoint(i,data_x[i], data_y[i]);
      }
      
      if(!opt.autox){
	
	gr->GetHistogram()->GetXaxis()->SetRangeUser(opt.startx, opt.stopx);
      }      
      if(!opt.autoy){
	gr->GetHistogram()->GetYaxis()->SetRangeUser(opt.starty, opt.stopy);
      }
      gr->SetMarkerSize(0.2);
      gr->GetHistogram()->SetXTitle(opt.xlabel.c_str());
      gr->GetHistogram()->SetYTitle(opt.ylabel.c_str());
      gr->SetTitle(opt.plot_name.c_str());
      gr->Draw("A*");
    }catch(std::runtime_error &e){
      std::cerr << e.what() << std::endl;
      return false;
    }
  }
  return true;
}
void laserCalibration(
		char* filename = "frascatirun", //input file
		int filenum = 1081, 					//file number
		int channel = 3, 						//trace channel
		int flagChannel = 5, 					//laser flag channel
		Double_t entriesN = 10,					//number of entries for prcessing
		int sleep = 10,							//sleep time between 2 processed entries, helpful for viewing traces
		bool gui = true							//enable or disable trace visualization
		)
{
	caen_5742 caen;
	Int_t nbins = 1024;
	Double_t entries = entriesN;
	Int_t bin;

	TCanvas *c1 = new TCanvas("c1","frascatirun",900,700);
	c1->Divide(1,2);
	c1->cd(1);

	TGraph* g = new TGraph();
	TH1F* lmPeaks = new TH1F("lm","Peaks Ratio", 1000, 0, 5000);
    TH1F* d = new TH1F("d","",nbins,0,nbins);
    TH1F* back = new TH1F("Back","",nbins,0,nbins);

    // input file
    char fname[100]=0;
    sprintf(fname,"%s_0%i.root",filename,filenum);

    TFile* infile = new TFile(fname);
	TTree *t = (TTree*) infile->Get("t");
	t->SetBranchAddress("caen_5742", &caen.system_clock);
	t->Print();

	if(entriesN<=0)
		entries = t->GetEntries();

	//out file
    char foutname[100]=0;
    int lm=0;
    if (channel ==3)lm=1;
    if (channel ==4)lm=2;
    sprintf(foutname,"./calibration/LM%i_out_0%i.root",lm,filenum);
	outfile = new TFile(foutname,"RECREATE");
	outTree = new TTree("LM","frascatirun output");
	calibTree = new TTree("LM_cal","frascatirun output");
	outTree->Branch("LM_PX1",&fPositionX1,"PX1/D");
	outTree->Branch("LM_PX2",&fPositionX2,"PX2/D");
	outTree->Branch("LM_PY1",&fPositionY1,"PY1/D");
	outTree->Branch("LM_PY2",&fPositionY2,"PY2/D");
	//outTree->Branch("baseline",baseline,"baseline[1024]/F");
	outTree->Branch("time",&timeline,"time/D");
	outTree->Branch("LM_P2_Integral",&integralP2,"IP2/D");
	calibTree->Branch("LM_P2_Integral_mean",&integralP2_mean,"IP2_mean/D");
	calibTree->Branch("LM_P2_Integral_mean_error",&integralP2_mean_error,"IP2_mean_error/D");
	calibTree->Branch("LM_P2_Integral_sigma",&integralP2_sigma,"IP2_sigma/D");
	calibTree->Branch("LM_P2_Integral_sigma_error",&integralP2_sigma_error,"IP2_sigma_error/D");

    /**************************************
     * read entries
     **************************************
    */
	for (int j = 0; j < entries; ++j){
		gSystem->Sleep (sleep);

		t->GetEntry(j);

		//TRIGGER SELECTION
		if(caen.trace[flagChannel][400]>1000 && caen.trace[flagChannel][800]<3000){
			timeline = caen.system_clock;

			/**************************************
		     * Peaks estimation
		     **************************************
		    */
			for (int i = 0; i < nbins; ++i){
				g->SetPoint(i, i, caen.trace[channel][i]);
			}

			Double_t y_max = TMath::MaxElement(g->GetN(),g->GetY());
			Float_t * source = new Float_t[nbins];
			Float_t * dest = new Float_t[nbins];

			for (int i = 0; i < nbins; ++i){
				source[i]=y_max-caen.trace[channel][i];
				g->SetPoint(i, i, source[i]);
			}

			//Use TSpectrum to find the peak candidates
			TSpectrum *s = new TSpectrum();
		   	Int_t nfound = s->SearchHighRes(source, dest, nbins, 3, 2, kTRUE, 2, kFALSE, 5);

		    /**************************************
		     * Background estimation
		     **************************************
		    */
		    Int_t  	ssize = nbins;
		    Int_t  	numberIterations = 20;
		    Int_t  	direction = s->kBackIncreasingWindow;
		    Int_t  	filterOrder = s->kBackOrder2;
		    bool  	smoothing = kFALSE;
		    Int_t  	smoothWindow = s->kBackSmoothing3;
		    bool  	compton = kFALSE;
		    for (int i = 0; i < nbins; ++i) baseline[i] = source[i];
		    s->Background(baseline, ssize, numberIterations, direction, filterOrder, smoothing, smoothWindow, compton);

		    /**************************************
		     * Peaks and integral estimation
		     **************************************
		    */
		    Double_t px[2], py[2];
		    for (int i = 0; i < nbins; ++i) dest[i] = source[i]-baseline[i];
		    if(nfound==2){
			   bin = s->GetPositionX()[0];
			   fPositionX1 = bin;
			   fPositionY1 = dest[bin];
			   px[0] = bin;
			   py[0] = dest[bin];
			   bin = s->GetPositionX()[1];
			   fPositionX2 = bin;
			   fPositionY2 = dest[bin];
			   px[1] = bin;
			   py[1] = dest[bin];
		    }
			int posxa=6;
		    int posxb=9;
		    switch (filenum){
		    	case 1081:
		    		posxa=6;
		    		posxb=9;
		    		break;

		    	case 1082:
		    		posxa=5;
		    		posxb=7;
		    		break;

		    	case 1083:
		    		posxa=5;
		    		posxb=8;
		    		break;

		    	case 1084:
		    		posxa=5;
		    		posxb=7;
		    		break;

		    	case 1085:
		    		posxa=5;
		    		posxb=7;
		    		break;

		    	case 1086:
		    		posxa=5;
		    		posxb=5;
		    		break;

		    	case 1087:
		    		posxa=4;
		    		posxb=4;
		    		break;

		    	case 1088:
		    		posxa=3;
		    		posxb=4;
		    		break;

		    	case 1089:
		    		posxa=3;
		    		posxb=3;
		    		break;

		    	default:
		    		posxa=6;
		    		posxb=9;
}

		    integralP2 = g->Integral (fPositionX2-posxa,fPositionX2+posxb);

		    /**************************************
		     * print and update the canvas
		     **************************************
		    */
		    if(gui==true){
				TH1F* gh = g->GetHistogram();
				gh->FillN(nbins,g->GetX(),g->GetY());
				g->Draw();

				TPolyMarker* pm = (TPolyMarker*)gh->GetListOfFunctions()->FindObject("TPolyMarker");
				if (pm) {
				   gh->GetListOfFunctions()->Remove(pm);
				   delete pm;
				}
				pm = new TPolyMarker(nfound, px, py);

				gh->GetListOfFunctions()->Add(pm);
				pm->SetMarkerStyle(23);
				pm->SetMarkerColor(kBlue);
				pm->SetMarkerSize(1.3);
				for (i = 0; i < nbins; i++) d->SetBinContent(i,dest[i]);
				d->SetLineColor(kRed);
				d->Draw("SAME");

				for (i = 0; i < nbins; i++) back->SetBinContent(i,baseline[i]);
			    back->SetLineColor(kGreen);
			    back->Draw("SAME");
				c1->Update();
		    }

		    /**************************************
		     * Fill tree and peaks data Histogram
		     **************************************
		    */
			if(nfound==2)
			{
				lmPeaks->Fill(integralP2);
				outTree->Fill();
			}
	        //printf("time= %d, posx1= %d, posy1= %d\n",time, fPositionX1, fPositionY1);
			//printf("time= %d, posx2= %d, posy2= %d\n",time, fPositionX2, fPositionY2);
			//for (int i=0;i<nbins;i++) printf("time = %d\n",baseline[i]);
		}
	}

	/**************************************
     * switch to the bottom pan and Draw Histogram
     **************************************
    */
	c1->cd(2);
	//lmPeaks->SetAxisRange(TMath::MinElement(entries,binmin),TMath::MaxElement(entries,binmax)+100);
	//lmPeaks->SetAxisRange(0,3000);
	lmPeaks->Fit("gaus");
	integralP2_mean = lmPeaks->GetFunction("gaus")->GetParameter(1);
	integralP2_sigma = lmPeaks->GetFunction("gaus")->GetParameter(2);
	integralP2_mean_error = lmPeaks->GetFunction("gaus")->GetParError(1);
	integralP2_sigma_error = lmPeaks->GetFunction("gaus")->GetParError(2);
	//printf("mean = %f\n",integralP2_mean);
	//printf("sigma = %f\n",integralP2_sigma);
	calibTree->Fill();
	lmPeaks->Draw();
	c1->Update();

	outfile->cd();
	gROOT->GetList()->Write();
	outTree->Write();
	calibTree->Write();
	outfile->Close();
}
Example #3
0
ApplicationWindow::ApplicationWindow()
    : Q3MainWindow( 0, "example application main window", Qt::WDestructiveClose )
{
   // create a printer
   printer = new QPrinter;
   // create user interface actions

   Q3Action *fileNewAction = new Q3Action( "New", "&New", Qt::CTRL+Qt::Key_N, this, "new" );

   connect( fileNewAction, SIGNAL( activated() ) , this, SLOT( newDoc() ) );

   Q3Action *fileOpenAction = new Q3Action( "Open File", QPixmap( fileopen ), "&Open", Qt::CTRL+Qt::Key_O, this, "open" );
   connect( fileOpenAction, SIGNAL( activated() ) , this, SLOT( load() ) );
   Q3MimeSourceFactory::defaultFactory()->setPixmap( "fileopen", QPixmap( fileopen ) );
   fileOpenAction->setWhatsThis( fileOpenText );

   Q3Action *fileSaveAction = new Q3Action( "Save File", QPixmap( filesave ), "&Save", Qt::CTRL+Qt::Key_S, this, "save" );
   connect( fileSaveAction, SIGNAL( activated() ) , this, SLOT( save() ) );
   fileSaveAction->setWhatsThis( fileSaveText );

   Q3Action *fileSaveAsAction = new Q3Action( "Save File As", "Save &as", 0,  this, "save as" );
   connect( fileSaveAsAction, SIGNAL( activated() ) , this, SLOT( saveAs() ) );
   fileSaveAsAction->setWhatsThis( fileSaveText );

   Q3Action *filePrintAction = new Q3Action( "Print File", QPixmap( fileprint ), "&Print", Qt::CTRL+Qt::Key_P, this, "print" );
   connect( filePrintAction, SIGNAL( activated() ) , this, SLOT( print() ) );
   filePrintAction->setWhatsThis( filePrintText );

   Q3Action *fileCloseAction = new Q3Action( "Close", "&Close", Qt::CTRL+Qt::Key_W, this, "close" );
   connect( fileCloseAction, SIGNAL( activated() ) , this, SLOT( close() ) );

   Q3Action *fileQuitAction = new Q3Action( "Quit", "&Quit", Qt::CTRL+Qt::Key_Q, this, "quit" );
   connect( fileQuitAction, SIGNAL( activated() ) , qApp, SLOT( quit() ) );

   // create button for histo handling
   Q3Action *Update_histo = new Q3Action("Update Histo",QPixmap("qtbuttonsupdate.xpm"),"&Update", Qt::CTRL+Qt::Key_0, this, "update");
   connect( Update_histo, SIGNAL( activated() ) , this, SLOT( execute() ) );
   Q3MimeSourceFactory::defaultFactory()->setPixmap( "update", QPixmap("qtbuttonsupdate.xpm" ) );
   Update_histo->setWhatsThis( updateHisto );

   Q3Action *clear_histo = new Q3Action("Clear Histo",QPixmap("qtbuttonsclear.xpm"),"&Clear", Qt::CTRL+Qt::Key_0, this, "clear");   connect( clear_histo, SIGNAL( activated() ) , this, SLOT( clear_histo() ) );
   Q3MimeSourceFactory::defaultFactory()->setPixmap( "clear", QPixmap("qtbuttonsclear.xpm" ) );
   clear_histo->setWhatsThis( clearHisto );

   // populate a tool bar with some actions

   Q3ToolBar* fileTools = new Q3ToolBar( this, "file operations" );
   fileTools->setLabel( tr( "File Operations" ) );
   fileOpenAction->addTo( fileTools );
   fileSaveAction->addTo( fileTools );
   filePrintAction->addTo( fileTools );
   Update_histo->addTo ( fileTools );
   clear_histo->addTo ( fileTools );
   (void)Q3WhatsThis::whatsThisButton( fileTools );
   // popuplate a menu with all actions

   Q3PopupMenu * file = new Q3PopupMenu( this );
   menuBar()->insertItem( "&File", file );
   fileNewAction->addTo( file );
   fileOpenAction->addTo( file );
   fileSaveAction->addTo( file );
   fileSaveAsAction->addTo( file );
   file->insertSeparator();
   filePrintAction->addTo( file );
   file->insertSeparator();
   fileCloseAction->addTo( file );
   fileQuitAction->addTo( file );

   // add a help menu

   Q3PopupMenu * help = new Q3PopupMenu( this );
   menuBar()->insertSeparator();
   menuBar()->insertItem( "&Help", help );
   help->insertItem( "&About", this, SLOT(about()), Qt::Key_F1 );
   help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
   help->insertSeparator();
   help->insertItem( "What's &This", this, SLOT(whatsThis()), Qt::SHIFT+Qt::Key_F1 );

   // create and define the ROOT Canvas central widget
   tab = new QTabWidget(this);
   tab->show();
   setCentralWidget( tab );

   Q3MainWindow *win1 = new Q3MainWindow( 0, "tab1 main window", Qt::WDestructiveClose );
   Q3MainWindow *win2 = new Q3MainWindow( 0, "tab2 main window", Qt::WDestructiveClose );
   aCanvas = new TQRootCanvas(this, win1,"Qt&Root");
   aCanvas2 = new TQRootCanvas(this, win2,"Qt&Root");

   win1->setCentralWidget(aCanvas);
   win2->setCentralWidget(aCanvas2);


   tab->addTab(win1,"page1");
   tab->addTab(win2,"page2");

   win1->show();
   win2->show();

   // with no QTabWidget
   //    aCanvas = new TQRootCanvas(this,"Qt&Root");
   //    setCentralWidget( aCanvas );
   resize( 450, 500 );

   // put here some ROOT Specifics ...
   if (aCanvas->GetCanvas()) {

      aCanvas->GetCanvas()->Resize();
      aCanvas->GetCanvas()->SetFillColor(40);
      aCanvas->GetCanvas()->cd();

      pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95,21);
      pad1->Draw();
      pad1->cd();
      pad1->SetGridx();
      pad1->SetGridy();
      pad1->GetFrame()->SetFillColor(42);
      pad1->GetFrame()->SetBorderMode(-1);
      pad1->GetFrame()->SetBorderSize(5);

      histo= new TH1F("hppx","Gaussian distribution",100,-4,4);
      histo->SetFillColor(0);
      histo->Draw();

      aCanvas->GetCanvas()->cd();
      pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45,21);
      pad2->Draw();
      pad2->cd();

      form1 = new TFormula("form1","abs(sin(x)/x)");
      sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
      sqroot->SetParameters(10,4,1,20);
      sqroot->SetLineColor(4);
      sqroot->SetLineWidth(6);
      sqroot->Draw();
   } // ! aCAnvas

   if (aCanvas2) {

      TCanvas *c1 = aCanvas2->GetCanvas();
      c1->Resize();
      c1->SetFillColor(42);
      c1->GetFrame()->SetFillColor(21);
      c1->GetFrame()->SetBorderSize(12);
      c1->cd();

      //graph example
      const Int_t n = 20;
      Double_t x[n], y[n];
      for (Int_t i=0;i<n;i++) {
         x[i] = i*0.1;
         y[i] = 10*TMath::Sin(x[i]+0.2);
         //  printf(" i %i %f %f \n",i,x[i],y[i]);
      }
      TGraph* gr = new TGraph(n,x,y);
      gr->SetLineColor(2);
      gr->SetLineWidth(4);
      gr->SetMarkerColor(4);
      gr->SetMarkerStyle(21);
      gr->SetTitle("a simple graph");
      gr->Draw("ACP");

      gr->GetHistogram()->SetXTitle("X title");
      gr->GetHistogram()->SetYTitle("Y title");
      c1->Modified();
      c1->Update();
    } //!aCanvas2
}
Example #4
0
void Fitter::Plot_sWeights()
{
	vector<TGraph*> vec_pGraph;
	vector<vector<double>* > vec_pVec_values;
	Interval yRange(0., 0.);
	
	for(auto& pVec_pdfs : {&sigPdfs, &bkgPdfs})
	for(auto& pdf : *pVec_pdfs)
	{
		RooAbsReal& yield = pdf.GetYld();	
		int nEvents = pData->numEntries();
		vector<double>* px = new vector<double>;
		vector<double>* py = new vector<double>;
		TGraph* pGraph = NULL;
		vector<tuple<double, double> > points(nEvents, make_tuple(0., 0.));
				
		for(int i=0; i<nEvents; ++i) //nSWeights
		{
			double x = pData->get(i)->getRealValue(pFitVar->GetName());
			double y = pSPlot->GetSWeight(i, yield.GetName());
			
			points[i] = make_tuple(x, y);
			
			if(y > yRange.maximum) yRange.maximum = y;
			if(y < yRange.minimum) yRange.minimum = y;
		}
		
		sort(points.begin(), points.end(), [](const tuple<double, double>& p, const tuple<double, double>& q){ return get<0>(p) < get<0>(q); });
		for(auto& p : points)
		{
			px->push_back(get<0>(p));
			py->push_back(get<1>(p));
		}
		
		pGraph = new TGraph(nEvents, &px->at(0), &py->at(0));		
		
		vec_pGraph.push_back(pGraph);
		vec_pVec_values.push_back(px);
		vec_pVec_values.push_back(py);
	}
	
	yRange = GetNiceLimits(yRange);
	
	
	//TFile* pFile = new TFile(outputFilename.c_str(), "UPDATE");
	//TDirectory* pSPlotDir = pFile->GetDirectory("sWeightPlots");
	TCanvas* pCanvas = new TCanvas("sWeight_plot", "sWeight_plot", 800, 600);
	TLegend* pLegend = new TLegend(0.65, 0.65, 0.99, 0.93);
	
	//if(!pSPlotDir) pSPlotDir = pFile->mkdir("sWeightPlots");
	
	pLegend->SetTextSize(0.035);
	for(size_t i=0; i<vec_pGraph.size(); ++i)
	{
		TGraph* pGraph = vec_pGraph[i];
		RooAbsReal& yield = (i<sigPdfs.size()?sigPdfs[i]:bkgPdfs[i-sigPdfs.size()]).GetYld();
		pGraph->SetLineWidth(2);
		//pGraph->GetHistogram()->SetMarkerColorAlpha(kWhite, 1.);
		pGraph->GetHistogram()->SetFillColor(10);
		pGraph->SetFillColor(10);
		pGraph->GetHistogram()->SetTitle(("sWeight vs " + plotAxisLabel).c_str());
		pGraph->SetLineColor(i+1);
		pGraph->GetXaxis()->SetTitle(plotAxisLabel.c_str());
		pGraph->GetHistogram()->SetMinimum(yRange.minimum);
		pGraph->GetHistogram()->SetMaximum(yRange.maximum);
		pGraph->GetHistogram()->GetYaxis()->SetTitle("sWeight");
		pGraph->Draw(i?"same":"");
		pLegend->AddEntry(pGraph, (yield.GetName()+string("_sw")).c_str());
		pLegend->Draw("L");
	}
	
	ChangeDirectory();
	pCanvas->Write();
	
	delete pLegend;
	delete pCanvas;
	//delete pFile;
	for(auto& pVec : vec_pVec_values) delete pVec;
	for(auto& pGraph : vec_pGraph) delete pGraph;
}
  PadBoundaries PlotBundle::calculatePadBoundaries() const {
    PadBoundaries union_pad_boundaries;
    bool nothing_drawn(true);

    std::vector<DrawableDataObjectDrawOptionPair<TH1*> >::const_iterator hist_it;

    for (hist_it = histograms.begin(); hist_it != histograms.end(); hist_it++) {
      TH1* hist = hist_it->data_object;
      std::string draw_option(hist_it->draw_option);
      if (hist) {
        hist->Draw(draw_option.c_str());
        if (plot_axis.x_axis_range.active)
          hist->GetXaxis()->SetRangeUser(plot_axis.x_axis_range.low,
              plot_axis.x_axis_range.high);
        PadBoundaries current_pad_boundaries(*hist);

        if (nothing_drawn) {
          union_pad_boundaries = current_pad_boundaries;
          nothing_drawn = false;
        } else {
          union_pad_boundaries = union_pad_boundaries.getUnionBoundaries(
              current_pad_boundaries);
        }
      }
    }

    std::vector<DrawableDataObjectDrawOptionPair<TGraph*> >::const_iterator graph_it;

    for (graph_it = graphs.begin(); graph_it != graphs.end(); graph_it++) {
      TGraph* graph = graph_it->data_object;
      std::string draw_option("A");
      draw_option.append(graph_it->draw_option);
      if (graph) {
        graph->Draw(draw_option.c_str());
        if (plot_axis.x_axis_range.active)
          graph->GetXaxis()->SetRangeUser(plot_axis.x_axis_range.low,
              plot_axis.x_axis_range.high);
        PadBoundaries current_pad_boundaries(*graph->GetHistogram());
        if (nothing_drawn) {
          union_pad_boundaries = current_pad_boundaries;
          nothing_drawn = false;
        } else {
          union_pad_boundaries = union_pad_boundaries.getUnionBoundaries(
              current_pad_boundaries);
        }
      }
    }

    std::vector<DrawableDataObjectDrawOptionPair<TGraph2D*> >::const_iterator graph2d_it;

    for (graph2d_it = graphs2d.begin(); graph2d_it != graphs2d.end();
        graph2d_it++) {
      TGraph2D* graph = graph2d_it->data_object;
      std::string draw_option("A");
      draw_option.append(graph2d_it->draw_option);
      if (graph) {
        graph->Draw(draw_option.c_str());
        PadBoundaries current_pad_boundaries(*(TH1*) graph->GetHistogram());
        current_pad_boundaries.is_2d = true;
        current_pad_boundaries.z_min = graph->GetZmin();
        current_pad_boundaries.z_max = graph->GetZmax();
        if (nothing_drawn) {
          union_pad_boundaries = current_pad_boundaries;
          nothing_drawn = false;
        } else {
          union_pad_boundaries = union_pad_boundaries.getUnionBoundaries(
              current_pad_boundaries);
        }
      }
    }

    return union_pad_boundaries;
  }
Example #6
0
void setTimeDisp(TGraph& g, const Int_t ndiv=510) {
   if ( (g.GetHistogram()!=0) &&
        (g.GetHistogram()->GetXaxis()!=0) ) {
      setTimeDisp(*(g.GetHistogram()),ndiv);
   }
}