FairRoot/PandaRoot
GammaSpectraAnalysis_CableTest.C
Go to the documentation of this file.
1 // root macro to analyze the simulation output
2 //void convertMCPoints()
3 
4 #include "TROOT.h"
5 #include "TSystem.h"
6 #include "TFile.h"
7 #include "TStopwatch.h"
8 #include "TString.h"
9 #include "TH1D.h"
10 #include "TF1.h"
11 #include "TClonesArray.h"
12 #include "TMath.h"
13 #include "TTree.h"
14 #include <fstream>
15 #include <string>
16 #include "TVector3.h"
17 
19 {
20 return E-E/(1+E/0.000511*(1-TMath::Cos(Th)));
21 }
23 {
24 return TMath::ACos((E*E-E*Eprime-Eprime*0.000511)/(E*(E-Eprime)));
25 }
26 
28 {
29  return par[0]+x[0] * par[1]+x[0] * x[0] * par[2]+ par[3] * 1/(TMath::Sqrt(2 * TMath::Pi()) * par[5]) * TMath::Exp(- pow((x[0] -par[4]),2)/2/par[5]/par[5]);
30 }
31 
33 {
34  return par[1]*TMath::Poisson(x[0],par[0]);
35 }
36 int GammaSpectraAnalysis_CableTest(TString Filename)//, Double_t Energy)
37 {
38 
39  // ----- Load libraries ------------------------------------------------
40 //``gSystem->Load("fstream.h");
41  gROOT->Macro("$VMCWORKDIR/gconfig/rootlogon.C");
42 
43  gSystem->Load("libHyp");
44  gSystem->Load("libHypGe");
45 
46  // ----- Timer --------------------------------------------------------
47  TStopwatch timer;
48  timer.Start();
49  // ------------------------------------------------------------------------
50 
51  //Get energy from filename
52 
53  Int_t IndexPreEnergy = Filename.Index("_",1,0,TString::kExact);
54  Int_t IndexPostEnergy = Filename.Index("MeV",3,0,TString::kExact);
55  TString EnergyFromFileName = Filename(IndexPreEnergy+1,IndexPostEnergy-IndexPreEnergy-1);
56  //cout << EnergyFromFileName << endl;
57  Double_t Energy = EnergyFromFileName.Atof()/1000;
58  cout << "Gamma energy: " << Energy << endl;
59  // the sim file you want to analyse
60  //string Filename = "TripleBall40Offset10_1MeV_10000Evts"; //without File Type ending!!!
61  //Double_t Energy = 0.001;
62  if(Filename.EndsWith(".root"))
63  {
64  Filename.ReplaceAll(".root","");
65  cout << "Filename ending chopped!" << endl;
66  }
67  TString CompleteFilename = "$SIMDATADIR/GeantTest/"+Filename+".root";
68  TFile* g = new TFile(CompleteFilename);
69 
70 //Output Files
71  TString Path = getenv("SIMDATADIR");
72  TString outfile= Path+"/GeantTest/Ana/Cable/Ana";
73  outfile += Filename;
74  TString txtfileName = outfile;
75  outfile +=".root";
76  txtfileName += ".txt";
77  TFile* fi = new TFile(outfile,"RECREATE");
78 
79  //cout << "Path" << Path << endl;
80  //TString txtfileName = Path +"/Data_Marcell/"+Filename+"_Spectrum.txt";
81  //cout << txtfileName << endl;
82  ofstream txtfile;
83  txtfile.open(txtfileName);
84  txtfile << "File read:" << CompleteFilename << endl;
85  //photons from hyp electromag. decay
86  TTree *b=(TTree *) g->Get("pndsim") ;
87  TClonesArray* hit_bar=new TClonesArray("PndHypGePoint");
88  b->SetBranchAddress("HypGePoint",&hit_bar);//Branch names
89  TClonesArray* mc_bar=new TClonesArray("PndMCTrack");
90  b->SetBranchAddress("MCTrack",&mc_bar);//Branch names
91 
92 
93 
94 
95  //****photons from hyp elect. decay
96  Double_t HistoUpperThreshold = Energy*1.2;
97  TString Name = "total gam energy deposit, " + Filename;
98  TH1D* gamTde = new TH1D("gamTde",Name.Data(),10000000*HistoUpperThreshold,0.0000,HistoUpperThreshold);
99 
100 
101  bool verbose = false;
102  Int_t MotherId,Motherpdg;
103 
104  TH1D *hNoHits = new TH1D("Number of Hits", "Number of Hits", 10,0,10);
105 
106  TVector3 vecs , pos;
107  int mcpdg = -1,ev;
109 
110  //vector<int> event;
111  int count;
112  set<int> SetOfCrystalHit;
113  set<int>::iterator it;
114 
115  Int_t nEvents = b->GetEntriesFast();
116  cout<< "Number of Simulated Events: "<<nEvents<<endl;
117  txtfile<< "Number of Simulated Events: "<<nEvents<<endl;
118 
119  Double_t Resolution = 2.; //keV
120  for (Int_t k=0; k<nEvents; k++)
121  {
122  //cout << k << endl;
123  Eng=0.;
124  b->GetEntry(k);
125  if (!((k*10)% nEvents))
126  {
127  cout << k << endl;
128  }
129  //if(verbose) cout<<"Event No "<<j<<endl;
130  for (Int_t i=0; i<hit_bar->GetEntriesFast(); i++)
131  {
132  //cout << hit_bar->GetEntriesFast()<<endl;
133  PndHypGePoint *hitgam=(PndHypGePoint*)hit_bar->At(i);
134 
135  PndMCTrack *mcgam = (PndMCTrack*)mc_bar->At(hitgam->GetTrackID());
136  Eng +=gRandom->Gaus(0,0.000001*Resolution/2.3548);
137  Eng =Eng + (hitgam->GetEnergyLoss());
138  SetOfCrystalHit.insert(hitgam->GetDetectorID());
139  //cout <<"DetID" << hitgam->GetDetectorID()<< endl;
140  }//end for i (points in event)
141  //count =0;
142  //cout <<Eng<<endl;
143  //cout << TMath::Abs(Eng-Energy) << "\t" << 3*0.000001*Resolution/2.3548 << endl;
144  if (TMath::Abs(Eng-Energy) < 10*0.000001*Resolution/2.3548) //Peak = 10 * Resolution of 1 crystal --> takes multiple hits into account (faster than fitting and than doing it again, error is small)
145  {
146  //std::cout << "SetOfCrystalHit contains:";
147  //for (it=SetOfCrystalHit.begin(); it!=SetOfCrystalHit.end(); ++it)
148  //{
149  //std::cout << ' ' << *it;
150  //}
151  //std::cout << " and is " << SetOfCrystalHit.size() << " long\n";
152  hNoHits->Fill(SetOfCrystalHit.size());
153  //hNoHits->Fill(hit_bar->GetEntriesFast()); //Fill # of Hits -diagramm with events inside 3 sigma of the peak
154  //cout << hit_bar->GetEntriesFast()<<endl;
155  }
156  if(Eng>0)
157  {
158  gamTde->Fill(Eng); //Fill spectrum
159  }
160 
161  SetOfCrystalHit.clear();
162  }// end for j (events)
163  cout << "event loop finished"<< endl;
164 
165 
166  TCanvas* can3 = new TCanvas("can3","germanium detector",0,0,1000,1000);
167 
168  //gamTde->Draw();
169 
170  //Analysis of spectrum
171 
172  Double_t lowAngle = invCompton(0.001332,0.001040);
173  Double_t highAngle = invCompton(0.001332,0.001096);
174  Int_t npeaks = 1;
175  Int_t PeakToLook = 1;
176  Double_t E = Energy;
177 
178  TSpectrum *s = new TSpectrum(npeaks);
179  s->Search(gamTde,npeaks,"new",0.01);
180 
181  Float_t *xpeaks = s->GetPositionX();
182  Float_t *ypeaks = s->GetPositionY();
183 
184  //Print Peaks
185  for (Int_t i = 0; i < npeaks; ++i)
186  {
187  cout <<"("<< xpeaks[i]<<"," << ypeaks[i] << ") ";
188  }
189  cout << endl;
190 
191  Double_t PeakX = xpeaks[PeakToLook-1]*10000000;
192  Double_t PeakY = ypeaks[PeakToLook-1];
193  Double_t DPeakY = sqrt(PeakY);
194  Double_t SumPeak = 0;
195 
196  for(Int_t i = PeakX-Resolution*10*10/2.3548; i <= PeakX+Resolution*10*10/2.3548; i++) //Peak = 10 * Resolution of 1 crystal --> takes multiple hits into account (faster than fitting and than doing it again, error is small)
197  SumPeak += gamTde->GetBinContent(i);
198  Double_t DSumPeak = sqrt(SumPeak);
199  cout <<"MaxX: "<< PeakX << "; MaxY: " << PeakY << " +- " << DPeakY<< endl;
200  cout << "SumPeak: " << SumPeak << " +- " << DSumPeak << endl;
201  txtfile <<"MaxX: "<< PeakX << "; MaxY: " << PeakY << " +- " << DPeakY<< endl;
202  txtfile << "SumPeak: " << SumPeak << " +- " << DSumPeak << endl;
203 
204  Double_t ComptonEdge = Int_t(PeakX*(1-1/(1+2*PeakX/5110)))+1;
205  cout <<"ComptonEdge @ Bin: " << ComptonEdge << endl;
206  Double_t SumCompton=0, DSumCompton = 0;
207 
208 
209  for (Int_t i =Compton(E,lowAngle)*10000000; i <= Compton(E,highAngle)*10000000; i++)
210  {
211  SumCompton += gamTde->GetBinContent(i);
212  }
213  DSumCompton = sqrt(SumCompton);
214 
215  cout << "lowEnChan: " << Compton(E,lowAngle)*10000000 << endl << "highEnChan: "<< Compton(E,highAngle)*10000000 << endl;
216  txtfile << "lowEnChan: " << Compton(E,lowAngle)*100000000 << endl << "highEnChan: "<< Compton(E,highAngle)*10000000 << endl;
217  cout <<"N. of Compton Events: "<< SumCompton<< " +- " << DSumCompton << endl;
218  txtfile <<"N. of Compton Events: "<< SumCompton<< " +- " << DSumCompton << endl;
219  Double_t PeakToCompton = 0;
220  Double_t DPeakToCompton = 0;
221  if (SumCompton != 0)
222  {
223  PeakToCompton = PeakY/SumCompton*(Compton(E,highAngle)-Compton(E,lowAngle))*10000000;
224  DPeakToCompton = sqrt(pow(DPeakY/SumCompton,2)+ pow(PeakY*DSumCompton/SumCompton/SumCompton,2))*(Compton(E,highAngle)-Compton(E,lowAngle))*10000000;
225  cout << "Peak/Compton (res): "<< PeakToCompton << " +- "<< DPeakToCompton << endl;
226  txtfile << "Peak/Compton (res): "<< PeakToCompton << " +- "<< DPeakToCompton << endl;
227  }
228  else
229  {
230 
231  cout << "SumCompton = 0 -> no PeakToCompton possible!"<<endl;
232  txtfile << "SumCompton = 0 -> no PeakToCompton possible!"<<endl;
233  }
234  Double_t AllEntries = gamTde->GetEntries();
235  cout << "Entries: " << AllEntries << endl;
236  txtfile << "Entries: " << AllEntries << endl;
237 
238 
239  //fitting the histograms
240 
241  TF1 *GausBG = new TF1("GausBG","gausn"/*(0)+pol1(3)" */,(PeakX-100)/10000000,(PeakX+100)/10000000);
242  GausBG->SetParName(0,"Ampl");
243  GausBG->SetParName(1,"x0");
244  GausBG->SetParName(2,"sigma");
245  GausBG->SetParameter(0,PeakY/10000);
246  GausBG->SetParameter(1,PeakX/10000000.);
247  GausBG->SetParameter(2,100./10000000.);
248  GausBG->SetParLimits(0,0.0000001, 1000000);
249  GausBG->SetParLimits(1,(PeakX-10)/10000000.,(PeakX+10)/10000000.);
250  GausBG->SetParLimits(2,1./10000000.,100./10000000.);
251  /*GausBG->SetParameter(0,1);
252  GausBG->SetParameter(1,1);
253  GausBG->SetParameter(2,0);
254  GausBG->FixParameter(2,0);
255  GausBG->FixParameter(1,0);
256  GausBG->FixParameter(0,0);
257  */
258  GausBG->SetNpx(100000);
259  GausBG->SetLineColor(kRed);
260  gamTde->Fit(GausBG,"R");
261 
262  txtfile << "Fitparameter Spectrum (model: gausn):" << endl;
263  for (Int_t i = 0; i < 3; i++)
264  {
265  txtfile << GausBG->GetParName(i) << ":\t" << GausBG->GetParameter(i) << endl;
266  }
267  Double_t FWHM = GausBG->GetParameter(2)*2.3548200;
268  txtfile << "FWHM[keV]:\t" << FWHM*1000000 << endl;
269  cout << "FWHM[keV]:\t" << FWHM*1000000 << endl;
270 
271  TF1 *Poisson = new TF1("Poisson","gausn",0,15);//,2); //maybe gausn? maybe no fit? //gausn --> no ,2);
272  //Poisson->SetParameter(0,4);
273  //Poisson->SetParameter(1,SumPeak);
274  //Poisson->SetParLimits(0,0,12);
275  Poisson->SetParName(0,"Ampl");//Poisson->SetParName(0,"lambda");
276  Poisson->SetParName(1,"x0");//Poisson->SetParName(1,"Ampl");
277  Poisson->SetParName(2,"sigma");
278  //Poisson->SetParameter(1,1000);
279  Poisson->SetNpx(100000);
280  Poisson->SetLineColor(kRed);
281  //hNoHits->Fit(Poisson);
282 
283  //txtfile << "Fitparameter Number of Hits (model: Ampl.*Poisson):" << endl;
284  //for (Int_t i = 0; i < 3; i++) // if gausn fit --> < 2 -> < 3 , if no fit, comment it out!!!
285  //{
286  // txtfile << Poisson->GetParName(i) << ":\t" << Poisson->GetParameter(i) << endl;
287  //}
288 
289  //some make-up for the histograms
290  gamTde->SetXTitle("Energy [GeV]");
291  gamTde->SetYTitle("Counts");
292  gamTde->GetYaxis()->SetTitleOffset(1.35);
293  hNoHits->SetXTitle("Number of Hits");
294  hNoHits->SetYTitle("Counts");
295  hNoHits->GetYaxis()->SetTitleOffset(1.1);
296 
297  cout << "Full-Energy-Peak-Eff. [%]: " << double(int(GausBG->GetParameter(0)*10000000/(nEvents*2)*100*1000))/1000 << endl; // *2 because only 2Pi of solid angle is simulated
298  txtfile << "Full-Energy-Peak-Eff. [%]: " << double(int(GausBG->GetParameter(0)*10000000/(nEvents*2)*100*1000))/1000 << endl; // *2 because only 2Pi of solid angle is simulated
299  // writing to files and closing
300  cout << "Error ofFull-Energy-Peak-Eff. [%]: " << double(int(GausBG->GetParError(0)*10000000/(nEvents*2)*100*1000))/1000 << endl; // *2 because only 2Pi of solid angle is simulated
301  txtfile << "Error of Full-Energy-Peak-Eff. [%]: " << double(int(GausBG->GetParError(0)*10000000/(nEvents*2)*100*1000))/1000 << endl; // *2 because only 2Pi of solid angle is simulated
302  // writing to files and closing
303  gamTde->Write();
304  hNoHits->Write();
305  GausBG->Write();
306  Poisson->Write();
307  fi->Close();
308  txtfile.close();
309 
310 
311 
312  // ----- Finish -------------------------------------------------------
313  timer.Stop();
314  Double_t rtime = timer.RealTime();
315  Double_t ctime = timer.CpuTime();
316  cout << endl << endl;
317  cout << "Macro finished succesfully." << endl;
318  cout << "Output file is " << outfile << endl;
319  cout << "Parameter file is " << txtfileName << endl;
320  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
321  cout << endl;
322  // ------------------------------------------------------------------------
323 
324  return 0;
325 }
TVector3 pos
Int_t Motherpdg
Int_t i
Definition: run_full.C:25
TTree * b
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
#define verbose
TLorentzVector s
Definition: Pnd2DStar.C:50
int ev
TFile * g
Double_t par[3]
Int_t GetTrackID() const
Definition: PndHypGePoint.h:51
Double_t Enth
static T Cos(const T &x)
Definition: PndCAMath.h:43
TClonesArray * hit_bar
TVector3 vecs
static T Abs(const T &x)
Definition: PndCAMath.h:39
Double_t En
TFile * fi
TString CompleteFilename(TString mu, TString Q, TString Psf)
Double_t
Int_t nEvents
Definition: hit_dirc.C:11
TStopwatch timer
Definition: hit_dirc.C:51
Double_t Compton(Double_t E, Double_t Th)
TH1D * gamTde
Double_t PoissonFunc(Double_t *x, Double_t *par)
Double_t Eng
Double_t invCompton(Double_t E, Double_t Eprime)
int mcpdg
Double_t ctime
Definition: hit_dirc.C:114
Double_t x
TClonesArray * mc_bar
int count
Double_t PeakFunc(Double_t *x, Double_t *par)
int GammaSpectraAnalysis_CableTest(TString Filename)
Double_t rtime
Definition: hit_dirc.C:113
TCanvas * can3
Double_t Pi
Int_t GetDetectorID() const
Definition: PndHypGePoint.h:53
Int_t MotherId
Double_t mult
Double_t GetEnergyLoss() const
Definition: PndHypGePoint.h:62
TString outfile