FairRoot/PandaRoot
GammaSpectraAnalysis_NoH_Split.C
Go to the documentation of this file.
1 // created on 23.09.13 by Marcell Steinen steinen@kph.uni-mainz.de
2 // root macro to create an energy spectrum of parts of simulations with a high number of events (memory leak somewhere :/); to be used with "script_Gamma_Ana_List_split.sh" on himster
3 // further analysis is done via bash script "script_CombineAndAnalyzeGamma.sh" which combines the split up root output via "hadd" and calls root macro "AnalyseCombinedGammaFiles.C" to analyse the spectrum
4 
5 
6 
8 {
9 return Energy-Energy/(1+Energy/0.000511*(1-TMath::Cos(Th)));
10 }
12 {
13 return TMath::ACos((Energy*Energy-Energy*Eprime-Eprime*0.000511)/(Energy*(Energy-Eprime)));
14 }
15 
17 {
18  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]);
19 }
20 
22 {
23  return par[1]*TMath::Poisson(x[0],par[0]);
24 }
25 int GammaSpectraAnalysis_NoH_Split(TString Filename = "TripleBall40Offset10STT_8MeV_1000EvtswithSecTar.root",Int_t NoOfJobs = 10, Int_t JobNumber = 2)//, Double_t Energy)
26 {
27 
28 
29  // ----- Load libraries ------------------------------------------------
30 //``gSystem->Load("fstream.h");
31  //gROOT->Macro("$VMCWORKDIR/gconfig/rootlogon.C");
32 
33 
34  gSystem->Load("libHypGe");
35  gSystem->Load("set"); // needed to use a std::set
36  // ----- Timer --------------------------------------------------------
37  TStopwatch timer;
38  timer.Start();
39  // ------------------------------------------------------------------------
40 
41  //Get energy from filename
42 
43  Int_t IndexPreEnergy = Filename.Index("_",1,0,0);
44  Int_t IndexPostEnergy = Filename.Index("MeV",3,0,0);
45  TString EnergyFromFileName = Filename(IndexPreEnergy+1,IndexPostEnergy-IndexPreEnergy-1);
46  //cout << EnergyFromFileName << endl;
47  Double_t Energy = EnergyFromFileName.Atof()/1000;
48 
49  // the sim file you want to analyse
50  //string Filename = "TripleBall40Offset10_1MeV_10000Evts"; //without File Type ending!!!
51  //Double_t Energy = 0.001;
52  if(Filename.EndsWith(".root",1))
53  {
54  Filename.ReplaceAll(".root","");
55  cout << "Filename ending chopped!" << endl;
56  }
57 
58 
59  TString CompleteFilename = "$SIMDATADIR/Gamma/"+Filename+".root";
60  TFile* g = new TFile(CompleteFilename);
61 
62 //Output Files
63  TString SIMDATADIR = getenv("SIMDATADIR");
64  TString OutPath = SIMDATADIR+"/Gamma/Ana/" + Filename;
65  TString MkDir = ".!mkdir -p " + OutPath;
66  gROOT->ProcessLine(MkDir);
67  cout << MkDir << endl;
68  TString outfile= OutPath + "/Ana";
69  outfile += Filename;
70  outfile += "_";
71  outfile += JobNumber;
72  TString txtfileName = outfile;
73  outfile +=".root";
74  txtfileName += ".txt";
75  TFile* fi = new TFile(outfile,"RECREATE");
76 
77 
78  //photons from hyp electromag. decay
79  TTree *b=(TTree *) g->Get("pndsim") ;
80  TClonesArray* hit_bar=new TClonesArray("PndHypGePoint");
81  b->SetBranchAddress("HypGePoint",&hit_bar);//Branch names
82  TClonesArray* mc_bar=new TClonesArray("PndMCTrack");
83  b->SetBranchAddress("MCTrack",&mc_bar);//Branch names
84 
85 
86 
87 
88  //****photons from hyp elect. decay
89  Double_t HistoUpperThreshold = Energy*1.2;
90  string Name = "total gam energy deposit, " + Filename;
91  Int_t ChannelResolution = 10000000; // GeV/ChannelResolution = energy per channel , with 10000000 100 eV/channel
92  Double_t Resolution = 2.; //keV
93  TH1D* gamTde = new TH1D("gamTde",Name.c_str(),ChannelResolution*HistoUpperThreshold,0.0000,HistoUpperThreshold);
94  TH1D* ParameterBuffer = new TH1D("ParameterBuffer", "ParameterBuffer",4,0,3);
95  ParameterBuffer->SetBinContent(1,Energy);
96  ParameterBuffer->SetBinContent(2,b->GetEntriesFast());
97  ParameterBuffer->SetBinContent(3,ChannelResolution);
98  ParameterBuffer->SetBinContent(4,Resolution);
99 
100 
101  ParameterBuffer->Write();
102 
103  bool verbose = false;
104  Int_t MotherId,Motherpdg;
105 
106  TH1D *hNoHits = new TH1D("Number of Hits", "Number of Hits", 10,0,10);
107 
108  TVector3 vecs,pos;
109  int mcpdg = -1,ev;
111 
112  int count;
113  std::set<int> SetOfCrystalHit; //seems to be some error with set's -> taken out for now
114  std::set<int>::iterator it;
115 
116  Int_t nEvents = b->GetEntriesFast();
117  cout<< "Number of Simulated Events: "<<nEvents<<endl;
118 
119 
120 
121  PndHypGePoint *hitgam;// = new PndHypGePoint();
122  PndMCTrack *mcgam;
123  Int_t StartEvent = nEvents/NoOfJobs*(JobNumber-1);
124  Int_t EndEvent = nEvents/NoOfJobs*(JobNumber);
125  cout << "Analysing job number " << JobNumber <<"; from Event " << StartEvent << " to " << EndEvent-1 << endl;
126 
127  //for (Int_t k=0; k<nEvents; k++)
128  for (Int_t k=StartEvent; k<EndEvent; k++)
129  {
130  //cout << k << endl;
131  Eng=0.;
132  b->GetEntry(k);
133  //cout << k<< endl;
134  if (!((k*10)% (EndEvent-StartEvent)))
135  {
136  cout << k << endl;
137  }
138  //if(verbose) cout<<"Event No "<<j<<endl;
139  for (Int_t i=0; i<hit_bar->GetEntriesFast(); i++)
140  {
141  //cout << hit_bar->GetEntriesFast()<<endl;
142  hitgam = new PndHypGePoint();
143  hitgam=(PndHypGePoint*)hit_bar->At(i);
144  //cout << hitgam << endl;
145  //mcgam = (PndMCTrack*)mc_bar->At(hitgam->GetTrackID());
146  Eng += gRandom->Gaus(0,0.000001*Resolution/2.3548);
147  Eng += hitgam->GetEnergyLoss();
148  SetOfCrystalHit.insert(hitgam->GetDetectorID());
149  //cout <<"DetID" << hitgam->GetDetectorID()<< endl;
150  //delete hitgam;
151  //delete mcgam;
152 
153  }//end for i (points in event)
154  //count =0;
155  //cout <<Eng<<endl;
156  //cout << TMath::Abs(Eng-Energy) << "\t" << 3*0.000001*Resolution/2.3548 << endl;
157  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)
158  {
159  std::cout << "SetOfCrystalHit contains Crystal No";
160  for (it=SetOfCrystalHit.begin(); it!=SetOfCrystalHit.end(); ++it)
161  {
162  std::cout << ' ' << *it;
163  }
164  std::cout << " and is " << SetOfCrystalHit.size() << " long\n";
165  hNoHits->Fill(SetOfCrystalHit.size());
166  //hNoHits->Fill(hit_bar->GetEntriesFast()); //Fill # of Hits -diagramm with events inside 3 sigma of the peak
167  //cout << hit_bar->GetEntriesFast()<<endl;
168  }
169  if(Eng>0)
170  {
171  gamTde->Fill(Eng); //Fill spectrum
172  }
173  //cout << "Size" << SetOfCrystalHit.size()<< endl;
174  SetOfCrystalHit.clear();
175  //cout << "Size" << SetOfCrystalHit.size()<< endl;
176  }// end for j (events)
177 
178 
179 
180 
181 
182  //some make-up for the histograms
183  gamTde->SetXTitle("Energy [GeV]");
184  gamTde->SetYTitle("Counts");
185  gamTde->GetYaxis()->SetTitleOffset(1.35);
186  hNoHits->SetXTitle("Number of Hits");
187  hNoHits->SetYTitle("Counts");
188  hNoHits->GetYaxis()->SetTitleOffset(1.1);
189 
190 
191  gamTde->Write();
192  hNoHits->Write();
193 
194  fi->Close();
195 
196  // ----- Finish -------------------------------------------------------
197  timer.Stop();
198  Double_t rtime = timer.RealTime();
199  Double_t ctime = timer.CpuTime();
200  cout << endl << endl;
201  cout << "Macro finished succesfully." << endl;
202  cout << "Output file is " << outfile << endl;
203  cout << "Parameter file is " << txtfileName << endl;
204  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
205  cout << endl;
206  // ------------------------------------------------------------------------
207 
208  return 0;
209 }
TVector3 pos
Int_t Motherpdg
Int_t i
Definition: run_full.C:25
TTree * b
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
#define verbose
int ev
int GammaSpectraAnalysis_NoH_Split(TString Filename="TripleBall40Offset10STT_8MeV_1000EvtswithSecTar.root", Int_t NoOfJobs=10, Int_t JobNumber=2)
TFile * g
Double_t par[3]
Double_t Enth
static T Cos(const T &x)
Definition: PndCAMath.h:43
Double_t invCompton(Double_t Energy, Double_t Eprime)
TClonesArray * hit_bar
TVector3 vecs
static T Abs(const T &x)
Definition: PndCAMath.h:39
Double_t En
Double_t PeakFunc(Double_t *x, Double_t *par)
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 PoissonFunc(Double_t *x, Double_t *par)
TH1D * gamTde
Double_t Compton(Double_t Energy, Double_t Th)
Double_t Eng
int mcpdg
Double_t ctime
Definition: hit_dirc.C:114
Double_t x
TClonesArray * mc_bar
int count
Double_t rtime
Definition: hit_dirc.C:113
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