#include "TH1.h" #include "TPad.h" #include "TCanvas.h" #include "TAxis.h" #include "TF1.h" #include "TPolyMarker.h" #include "TRandom.h" #include "TFile.h" #include "TGraph.h" #include "TGraphErrors.h" #include "TExec.h" #include "TPaveText.h" #include #include "time.h" #include #include #include #include "TFile.h" //Make the fit function Double_t fitfunction(Double_t *x, Double_t *par) { return par[0]*(exp(0.5*(pow(((x[0]-par[1])/par[2]),2.0))))+par[3]; } //run number for Clover_Energy_Spectra_Run_#.root file //int clover_runNum = 28; int clover_runNum = 300; //define global things TH1D *h1; TFile *fin; TDirectory *dir; TCanvas *c1; vector Peak_Locations; vector Peak_Heights; int x_up; int x_down; //Counters int counter; int marker_counter=0; int fit_counter=0; //Makers TPolyMarker *pm[100]; //Fit Parameters double Sigma[100]; double Sigma_Error[100]; double Mean[100]; double Mean_Error[100]; double Height[100]; double Height_Error[100]; double Area[100]; double Area_Error[100]; double Resolution[100]; double Resolution_Error[100]; double Efficiency[100]; double Efficiency_Error[100]; double Slope; double Slope_Error; double Intercept; double Intercept_Error; //Turn on and off testing bool Program_Test=false; bool MCA_Data=false; //Safeguard against saving under stupidity or without results bool Dont_Save=true; //Time stuff; struct tm source_date; struct tm run_date; //Source Stuff int N_Components; double Half_Lives[100]; //in days int N_Lines; double Energy[100]; //in keV double Energy_Unc[100]; //in keV double CPS[100]; //CPS of the source double CPS_Unc[100]; //in % int Source_Comp[100]; double CPS_Decay_Corr[100]; // Decay corrected source intensities (CPS) double CPS_Error_Decay_Corr[100]; //Decay corrected source intensity errors (CPS) double Decay_Corr[100]; //factor to multiply by for decay corr double Counts_Expected[100]; double Counts_Expected_Error[100]; //Run Stuff double Run_Length; //in minutes double Live_Time; //percentage //Fit Functions TF1 *f1[100]; TF1 *f2[100]; //Executive Function void Position_Selector() { TObject *selected = gPad->GetSelected(); if(!selected) return; if (!selected->InheritsFrom(TAxis::Class())) { gPad->SetUniqueID(0); return; } gPad->GetCanvas()->FeedbackMode(kTRUE); int event = gPad->GetEvent(); int eventx = gPad->GetEventX(); //cout << "event = " << event <<", eventx = " << eventx <PixeltoX(eventx)-25; kPixeltoX(eventx)+25; k++) { for(int k= gPad->PixeltoX(eventx)-100; kPixeltoX(eventx)+100; k++) { Int_t bin = h1->GetXaxis()->FindBin(k); //Double_t yp = h1->GetBinContent(bin); if(h1->GetBinContent(bin) > py) { py = h1->GetBinContent(k); px = k; } } py = 1.03*py; pm[marker_counter] = new TPolyMarker(1,&px,&py); pm[marker_counter]->SetMarkerStyle(20); pm[marker_counter]->SetMarkerColor(2); pm[marker_counter]->Draw(); gPad->Update(); // Peak_Locations.push_back(gPad->PixeltoX(eventx)); Peak_Locations.push_back(px); Peak_Heights.push_back(py); cout<< marker_counter <<" "<SetParameter(0,Peak_Heights[n]); f1[n]->SetParameter(1,Peak_Locations[n]); f1[n]->SetParameter(2,120); //5.0 //100 f1[n]->SetParameter(3,Peak_Heights[n]*0.05); f1[n]->SetParameter(4,0.0); // f1[n]->SetParameter(5,1.0); // cout << " INITIAL FIT " << n << endl; h1->Fit(Form("Fit_%d",n),"SELRMNQ"); // getc(stdin); int fit_low = 210;//20 int fit_high = 220;//20 // if(Energy[n]==42.8) { // fit_high= 5; // } // if(Energy[n]==591.8) { // fit_low = 20; // } // if(Energy[n]==996.3) { // fit_high=9; // } // if(Energy[n]==1004.7) { // fit_low=10; // } // if(Energy[n]==123.1) { // fit_high=50; // fit_low=50; // } // if(Energy[n]==723.3) { // fit_high=25; // fit_low=25; // } f2[n] = new TF1(Form("Fit2_%d",n),"gaus(0)+pol1(3)",(Peak_Locations[n]-1.7*fit_low),(Peak_Locations[n]+1.7*fit_high)); f2[n]->SetParameter(0,f1[n]->GetParameter(0)); f2[n]->SetParameter(1,f1[n]->GetParameter(1)); // f2[n]->SetParLimits(1,(Peak_Locations[n]-0.5*fit_low),(Peak_Locations[n]+0.5*fit_high)); f2[n]->SetParameter(2,f1[n]->GetParameter(2)); f2[n]->SetParameter(3,f1[n]->GetParameter(3)); f2[n]->SetParameter(4,f1[n]->GetParameter(4)); f2[n]->SetParameter(5,f1[n]->GetParameter(5)); h1->Fit(Form("Fit2_%d",n),"RMQ+"); f2[n]->SetLineColor(2); f2[n]->Draw("Same"); fit_counter++; gPad->Modified(); gPad->Update(); //Get the Parameters of the fit Sigma[n] = fabs(f2[n]->GetParameter(2)); Mean[n] = f2[n]->GetParameter(1); Height[n] = f2[n]->GetParameter(0); //Get the associated Errors Sigma_Error[n] = f2[n]->GetParError(2); Mean_Error[n] = f2[n]->GetParError(1); Height_Error[n] = f2[n]->GetParError(0); //Calculate the Area Area[n] = pow((2.0*3.14152),0.5)*Height[n]*Sigma[n]; Area_Error[n] = pow((2.0*3.14152),0.5)*pow(((pow(Sigma_Error[n],2.0)*pow(Height[n],2.0))+(pow(Height_Error[n],2.0)*pow(Sigma[n],2.0))),0.5); //Calculate % Resolution Resolution[n] = 100*2.35482*Sigma[n]/Mean[n]; Resolution_Error[n] = 100*2.35482*pow(((pow(Sigma_Error[n],2.0)*pow((1.0/Mean[n]),2.0))+(pow(Mean_Error[n],2.0)*pow(-1.0*Sigma[n]/(pow(Mean[n],2.0)),2.0))),0.5); //Calculate the % Efficiency Efficiency[n] = 100.0*Area[n]/Counts_Expected[n]; Efficiency_Error[n] = 100.0*pow(((pow(Area_Error[n],2.0)*pow((1.0/Counts_Expected[n]),2.0))+(pow(Counts_Expected_Error[n],2.0)*pow(-1.0*Area[n]/(pow(Counts_Expected[n],2.0)),2.0))),0.5); cout<<"Peak: "<GetXaxis()->SetRangeUser(Peak_Locations[0]-200, Peak_Locations[Peak_Locations.size()-1]+200); gPad->Modified(); gPad->Update(); //Make energy calibrations TGraph *gr1 = new TGraphErrors(N_Lines, Mean, Energy, Mean_Error, Energy_Unc); gr1->SetName("En_Cal"); gr1->SetTitle("Energy vs Channel"); gr1->GetXaxis()->SetTitle("Channels (Arb. Units)"); gr1->GetYaxis()->SetTitle("Energy (keV)"); gr1->SetMarkerStyle(20); //Make efficiency calibrations TGraph *gr2 = new TGraphErrors(N_Lines, Energy, Efficiency, Energy_Unc, Efficiency_Error); gr2->SetName("Eff_Cal"); gr2->SetTitle("Efficiency vs Energy"); gr2->GetXaxis()->SetTitle("Energy (keV)"); gr2->GetYaxis()->SetTitle("Efficiency (%)"); gr2->SetMarkerStyle(20); //Make resolutions plot TGraph *gr3 = new TGraphErrors(N_Lines, Energy, Resolution, Energy_Unc, Resolution_Error); gr3->SetName("Res"); gr3->SetTitle("Resolution vs Energy"); gr3->GetXaxis()->SetTitle("Energy (keV)"); gr3->GetYaxis()->SetTitle("Resolution (%)"); gr3->SetMarkerStyle(20); TCanvas *c2 = new TCanvas("Results","Results",640,480); c2->Divide(3,1); c2->cd(1); gr1->Draw("AP"); //TF1 *en_fit = new TF1("en_fit","pol1(0)",(Peak_Locations[3]),(Peak_Locations[Peak_Locations.size()-1]+200)); TF1 *en_fit = new TF1("en_fit","pol1(0)",(Peak_Locations[0]-200),(Peak_Locations[Peak_Locations.size()-1]+200)); en_fit->SetParameter(1,0.3); en_fit->SetParameter(0,0.2); gr1->Fit("en_fit","RMQ"); gr1->Fit("en_fit","RMQ"); gr1->Fit("en_fit","RMQ"); Slope = en_fit->GetParameter(1); Intercept = en_fit->GetParameter(0); Slope_Error = en_fit->GetParError(1); Intercept_Error = en_fit->GetParError(0); cout<<"Energy Calibration"<cd(1); TPaveText *pten = new TPaveText(200,1400,3500,1600); pten->SetFillColor(0); pten->SetTextAlign(12); pten->AddText(Form("Slope: %3.5f +/- %3.5f",Slope, Slope_Error)); pten->AddText(Form("Intercept: %3.5f +/- %3.5f",Intercept, Intercept_Error)); pten->Draw("same"); c2->Modified(); c2->Update(); c2->cd(2); gr2->Draw("AP"); TPaveText *pteff = new TPaveText(400,2.5,1600,6.5); pteff->SetFillColor(0); pteff->SetTextAlign(32); pteff->AddText("Energy (keV) Efficiency (%)"); // for(i=3; iAddText(Form("%4.1f %3.4f +/- %3.4f",Energy[i],Efficiency[i],Efficiency_Error[i])); } pteff->Draw("same"); c2->Modified(); c2->Update(); c2->cd(3); gr3->Draw("AP"); TPaveText *ptres = new TPaveText(400,1,1600,5); ptres->SetFillColor(0); ptres->SetTextAlign(32); ptres->AddText("Energy (keV) Resolution (%)"); // for(i=3; iAddText(Form("%4.1f %3.4f +/- %3.4f",Energy[i],Resolution[i],Resolution_Error[i])); } ptres->Draw("same"); c2->Modified(); c2->Update(); c2->Modified(); c2->Update(); } //if you press "v" you can view the peaks selected if(event == 24 && eventx == 118) { //Bubble Sort for(int m=0; mGetXaxis()->SetRangeUser(Peak_Locations[n]-200, Peak_Locations[n]+200); gPad->Modified(); gPad->Update(); cout<<"Peak Number: "<>text; if(text=="n") continue; if(text=="q") { h1->GetXaxis()->SetRangeUser(Peak_Locations[0]-200, Peak_Locations[Peak_Locations.size()-1]+200); gPad->Modified(); gPad->Update(); return; } } else { h1->GetXaxis()->SetRangeUser(Peak_Locations[0]-200, Peak_Locations[Peak_Locations.size()-1]+200); gPad->Modified(); gPad->Update(); return; } } } //pressing "r" removes the last peak if(event == 24 && eventx == 114) { if(Peak_Locations.size() > 0) { cout<<"Removing Last Point"<GetListOfPrimitives(); li->Remove(pm[marker_counter]); // pm[marker_counter]->Delete(); gPad->Modified(); gPad->Update(); } else { cout<<"Cant remove points because there is nothing selected!"<>counter; cout<<"Detector Number: "<Fill(j,gRandom->Uniform(1.0,5.0)); */ /* } */ /* for(int j=0; j<2000; j++) { */ /* h1->Fill(gRandom->Gaus(1500.0, 5.0)); */ /* } */ /* for(int j=0; j<1000; j++) { */ /* h1->Fill(gRandom->Gaus(5500.0,5.0)); */ /* } */ /* for(int j=0; j<1500; j++) { */ /* h1->Fill(gRandom->Gaus(10000.0, 5.0)); */ /* } */ /* for(int j=0; j<100000; j++) { */ /* h1->Fill(gRandom->Exp(2000.0)); */ /* } */ /* } */ /* else { */ /* if(MCA_Data) { */ /* h1 = new TH1D(Form("Clover_Spectrum_%d",counter), Form("Clover_Spectrum_%d",counter), 8192, 0, 8192); */ /* ifstream mca_data; */ /* mca_data.open("MCA_Clover_9_Ch1.txt"); */ /* int temp; */ /* for(int i=0; i<8192; i++) { */ /* mca_data>>temp; */ /* h1->Fill(i,temp); */ /* } */ /* } */ /* else{ */ //Get the histogram from the file // TFile *fin = new TFile(Form("Clover_Energy_Spectra_Run_%d.root", clover_runNum)); // h1 = (TH1D*)fin->Get(Form("Clover_Spectrum_%d",counter)); // TFile *fin = new TFile(Form("DSSDFB_HiGain_Run_300.root")); TFile *fin = new TFile( Form("Test-for-Chain-003.root") ); // cout<<"file opened"< folder(0); cout <<"enetr"<GetDirectory("SSSD"); cout<<"*****"<cd(); TDirectory* dirSSSD = dir->GetDirectory("HiERaw/"); dirSSSD->cd(); TH1D *h1; TString name = Form("SSSD_Hi_%2d_ERaw",counter); dirSSSD->GetObject(name.Data(),h1); cout<<"******* H! "<Draw(); // TH1D* h1 = (TH1D*)fin->FindObject(Form("SSSD/HiERaw/SSSD_Hi_%2d_ERaw",counter)); // h1= (TH1D*)fin->Get(Form("SSSD/HiERaw/SSSD_Hi_%2d_ERaw",counter)); // h1 = (TH1D*)fin->Get(Form("DSSD_HiGain_Front%d",counter)); // h1 = (TH1D*)fin->Get(Form("DSSD_HiGain_Back%d",counter)); // h1 = (TH1D*)fin->Get(Form("Clover_Spectrum_%d",counter)); // cout<>N_Components; for(int n = 0; n>Half_Lives[n]; } //Read in the date of the source Year Month Day Hour source_data>>source_date.tm_year>>source_date.tm_mon>>source_date.tm_mday>>source_date.tm_hour>>source_date.tm_min; source_date.tm_sec =0; //Read in the decay energies and intensities Energy (kev) Unc (kev) Intensity (CPS) Unc (%) source_data>>N_Lines; for(int n = 0; n>Energy[n]>>Energy_Unc[n]>>CPS[n]>>CPS_Unc[n]>>Source_Comp[n]; } source_data.close(); cout<<"Source found! "<>Run_Length; //in minutes run_data>>Live_Time; //percentage //Read in the date of the run Year Month Day Hour run_data>>run_date.tm_year>>run_date.tm_mon>>run_date.tm_mday>>run_date.tm_hour>>run_date.tm_min; run_date.tm_sec =0; cout.precision(10); //calucate the time difference (in seconds) between the source date and the run date double time_diff = difftime(mktime(&run_date),mktime(&source_date)); cout<<"It has been "<cd(); // h1->Draw("hist"); // cout<<"Draw Hist "<Draw("hist"); h1->GetXaxis()->SetTitle("Energy (Channels)"); h1->GetYaxis()->SetTitle("Counts / Channel"); h1->Rebin(2); gPad->AddExec(Form("Exec_%d",counter),"Position_Selector()"); }