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