00001 #include "TF1.h"
00002 #include "TSpectrum.h"
00003 #include "TH1.h"
00004 #include "TMath.h"
00005 #include "TCanvas.h"
00006 #include "TAxis.h"
00007 #include "TLegend.h"
00008 #include "TRegexp.h"
00009 #include "TLine.h"
00010 #include "TGraphErrors.h"
00011 #include "analysis/utilities.C"
00012 #include <iostream>
00013 #include <string>
00014 #include <algorithm>
00015 #include <numeric>
00016 #include <vector>
00017
00018 using namespace std;
00019 double epeak = 511.;
00020
00021 double Fit511Photopeak(TH1* h, double* error=0)
00022 {
00023 TSpectrum spec(1);
00024 spec.Search(h);
00025 h->GetXaxis()->SetTitle("Energy [photoelectrons]");
00026 h->Draw("e");
00027 TH1* bg = spec.Background(h);
00028 TH1* sig = (TH1*)(h->Clone());
00029 sig->SetLineColor(kGreen);
00030
00031 sig->Add(bg,-1);
00032 sig->Draw("same e");
00033 sig->Fit("gaus","m","same e");
00034 TF1* gaus = sig->GetFunction("gaus");
00035 if(gaus)
00036 gaus->SetLineColor(kGreen);
00037
00038 bg->SetLineColor(kRed);
00039 bg->Draw("same e");
00040
00041 TLine* line = new TLine(gaus->GetParameter(1),0,
00042 gaus->GetParameter(1),h->GetMaximum());
00043 line->SetLineColor(kBlue);
00044 line->SetLineWidth(2);
00045 line->Draw();
00046
00047
00048 double yield = spec.GetPositionX()[0]/epeak;
00049 double err = 0;
00050
00051 cout<<"Results from TSpectrum: \n\t"
00052 <<"Peak = "<<spec.GetPositionX()[0]<<" p.e.; Light Yield = "
00053 <<yield<<" p.e./keV"<<endl;
00054 if(gaus){
00055 yield = gaus->GetParameter(1)/epeak;
00056 err = gaus->GetParError(1)/epeak;
00057 cout<<"Results from BG Subtracted Gaus Fit: \n\t"
00058 <<"Peak = "<<gaus->GetParameter(1)<<" p.e.; Light Yield = "
00059 <<yield<<" +/- "<<err<<" p.e./keV"<<endl;
00060 err = max(err, TMath::Abs(yield-spec.GetPositionX()[0]/epeak));
00061
00062 }
00063 TLegend* leg = new TLegend(.6,.6,.9,.9);
00064 leg->AddEntry(h,"Raw Spectrum","lpe");
00065 leg->AddEntry(bg,"Background","lpe");
00066 leg->AddEntry(sig,"Signal","lpe");
00067 char title[20];
00068 sprintf(title,"Yield = %.2f pe/keV",yield);
00069 leg->AddEntry(line, title ,"l");
00070 leg->Draw();
00071
00072 if(error) *error = err;
00073 return yield;
00074 }
00075
00076 double Na22LightYield(const char* filename, double* error=0)
00077 {
00078 TTree* Events = GetEventsTree(filename);
00079 if(!Events)
00080 return 0;
00081 gROOT->ProcessLine(".x analysis/Aliases.C");
00082
00083 TString title = TString("Na22 Spectrum, ") +
00084 TString(filename)(TRegexp("Run......"));
00085 TH1F* hist = new TH1F("Na22Spec",title,200,1000,2500);
00086 Events->Draw("sumspec >> Na22Spec","min > 0","e");
00087 return Fit511Photopeak(hist, error);
00088
00089 }
00090
00091 TGraphErrors* PlotLightYieldGraph()
00092 {
00093 string filename;
00094 vector<double> x,y,ex,ey;
00095 while(1){
00096 cout<<"\n\nEnter next file to process; <enter> to finish."<<endl;
00097 getline(cin, filename);
00098 if(filename=="")
00099 break;
00100
00101
00102 TTree* Events = GetEventsTree(filename.c_str());
00103 if(!Events)
00104 continue;
00105 gROOT->ProcessLine(".x analysis/Aliases.C");
00106 double start = Events->GetMinimum("timestamp");
00107 double end = Events->GetMaximum("timestamp");
00108 double error = 0;
00109 TString title = TString("Na22 Spectrum, ") +
00110 TString(filename)(TRegexp("Run......"));
00111 TH1F* hist = new TH1F("Na22Spec",title,200,1000,2500);
00112 Events->Draw("sumspec >> Na22Spec","min > 0","e");
00113 double yield = Fit511Photopeak(hist,&error);
00114
00115 x.push_back((start+end)/2.);
00116 ex.push_back((end-start)/2.);
00117 y.push_back(yield);
00118 ey.push_back(error);
00119 }
00120 if(x.size() == 0){
00121 cerr<<"No valid points found!"<<endl;
00122 return 0;
00123 }
00124 TGraphErrors* graph = new TGraphErrors(x.size(),&x[0],&y[0],&ex[0],&ey[0]);
00125 graph->Draw("ape");
00126 TAxis* xax = graph->GetXaxis();
00127 xax->SetTitle("Run Time");
00128 xax->SetTimeDisplay(1);
00129 xax->SetTimeFormat("%Y/%m/%d");
00130 xax->SetTimeOffset(1,"gmt");
00131 TAxis* yax = graph->GetYaxis();
00132 yax->SetTitle("511 keV Light Yield [pe/keV]");
00133 graph->Draw("ape");
00134 return graph;
00135 }