Jump to content

Macro for producing Log(Rate) vs. Voltage

From Luter 345 Experiments

plot.C

/****************************************************************************
 * Macro to plot log(Rate) vs. Voltage plots. Takes in an int 1 - 4 for tube
 * number. May need to modify the directory path where your data is.
 * 
 * Usage: in root...
 * 		.x plot.C( 1,2,3, or 4 )
 *
 * Author: Brian Freeman 
 * Date: 11/6/14
 ***************************************************************************/
  
#include <iostream>
#include <fstream>

#include <string>
using namespace std;

//arrays that will be used in the TGraphErrors plot
Double_t x[25];
Double_t y[25];
Double_t dx[25];
Double_t dy[25];
//i is the array index
Int_t i=0;
//constant time used for each plot
Int_t t = 600;

//main method
void plot(int tubeNum){
      
   /*********************
    * Get file contents 
    *********************/
    
     //The file path goes here. My files were named tube[1-4].dat 
     //and located in the data directory
    std::ifstream f( Form("../data/tube%i.dat",tubeNum) );	
		
    //Print the filename that we are plotting
    char tubeName[15]=""; //will be used for graph title
    strcat(tubeName,"tube");
    //following switch statement appends the tube number
    switch(tubeNum){
		
		case 1: strcat(tubeName,"1");;
		break;		
		case 2: strcat(tubeName,"2");;
		break;
		case 3: strcat(tubeName,"3");;
		break;
		case 4: strcat(tubeName,"4");;
		break;
    }
	
    //string to be used for the title
    char title[50] = "Log(Rate) vs. Voltage - ";
    strcat(title,tubeName) ;  

    //loop through the file stopping when there are nomore lines in the file
    for( std::string line; getline( f, line );){
			
	//the line is taken in, and variables assigned and inserted into data arrays
	std::istringstream( line )>>x[i]>>y[i]>>dx[i]>>dy[i]; 
			
	//check zero condition, before taking the log of the y value
	//this is done because log(0) is undefined.
	 if( y[i]!=0 ){
		//divide the numer of events by the constant time value, and then
		//take the log of that number, log10(Rate);
		y[i] = log10( y[i]/t );
	 }

	//increment the index
	i++;
		
    }
    //close file resources
    f.close();
    f.clear();
    
    /**************
    * Plot data
    **************/ 
    
    //create canvas
    TCanvas *c1 = new TCanvas(tubeName,"Log(Rate) vs V",400,400,600,600);
    //set options
    gStyle->SetOptStat(1); 
    gStyle->SetOptFit(1);  
    c1->ToggleEventStatus(); 
	
    //create single graphs   
    
    //using the arrays that I built from the data file
    TGraphErrors *g = new TGraphErrors(1000 , x , y , dx, dy );	
    g = new TGraphErrors(i , x , y , dx, dy);
    g->SetLineColor(2);//red
    g->SetMarkerColor(2);//red
    g->SetMarkerStyle(21);//square
 
    //draw title
    g->SetTitle( title );
	
    //draw graph
    g->Draw("AP"); 
    
    //plot labels
    g->GetYaxis()->SetTitle("Log(Rate) [log(Hz)] ");
    g->GetXaxis()->SetTitle("Voltage [V]"); 

}//end of main