Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conversionRate.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file conversionRate.C
1 
2 TChain* handleFile(string name, string extension, string treename, unsigned int filecount){
3  TChain *all = new TChain(treename.c_str());
4  string temp;
5  for (int i = 0; i < filecount; ++i)
6  {
7 
8  ostringstream s;
9  s<<i;
10  temp = name+string(s.str())+extension;
11  all->Add(temp.c_str());
12  }
13  return all;
14 }
15 
22 TEfficiency* makepTRes(TFile* out_file,TChain* ttree=NULL,TTree* allTree=NULL){
23  //check if the spectra are in the file
24  out_file->ReOpen("READ");
25  if(out_file->Get("converted_photon_truth_pT")&&out_file->Get("all_photon_truth_pT"))
26  return new TEfficiency(*(TH1F*)out_file->Get("converted_photon_truth_pT"),*(TH1F*)out_file->Get("all_photon_truth_pT"));
27  //if they are not in the file check if the trees are NULL
28  else if(!ttree||!allTree){
29  return NULL;
30  }
31  out_file->ReOpen("UPDATE");
32  float pT;
33  float tpT;
34  float track_pT;
35  ttree->SetBranchAddress("photon_pT",&pT);
36  ttree->SetBranchAddress("tphoton_pT",&tpT);
37 
38  vector<float> *allpT=NULL;
39  allTree->SetBranchAddress("photon_pT",&allpT);
40 
41  TH1F *pTeffPlot = new TH1F("#frac{#it{p}^{T}}{#it{p}_{#it{truth}}^{T}}","",40,-2,2);
42  TH2F *pTefffuncPlot = new TH2F("pT_resolution_to_truthpt","",40,1,35,40,-1.5,1.5);
43  TH1F *converted_pTspec = new TH1F("converted_photon_truth_pT","",20,5,25);
44  TH1F *all_pTspec = new TH1F("all_photon_truth_pT","",20,5,25);
45  //TH1F *trackpTDist = new TH1F("truthpt","",40,0,35);
46  pTeffPlot->Sumw2();
47  converted_pTspec->Sumw2();
48  all_pTspec->Sumw2();
49  pTefffuncPlot->Sumw2();
50  //TODO need to turn off other branches
51  //make the pT spectra fo converted
52  for (int event = 0; event < ttree->GetEntries(); ++event)
53  {
54  ttree->GetEvent(event);
55  if(pT>0)pTeffPlot->Fill(pT/tpT);
56  converted_pTspec->Fill(tpT);
57  if(pT>0)pTefffuncPlot->Fill(tpT,pT/tpT);
58  //trackpTDist->Fill(track_pT);
59  }
60  //make the pT spectra for unconverted
61  for (int event = 0; event < allTree->GetEntries(); ++event)
62  {
63  allTree->GetEvent(event);
64  for(auto i : *allpT){
65  all_pTspec->Fill(i);
66  }
67  }
68  //format and save the data
69  TEfficiency* uni_rate = new TEfficiency(*converted_pTspec,*all_pTspec);
70  uni_rate->SetName("uni_rate");
71  uni_rate->Write();
72  pTeffPlot->Scale(1./ttree->GetEntries(),"width");
73  pTefffuncPlot->Scale(1./ttree->GetEntries());
74  TProfile* resProfile = pTefffuncPlot->ProfileX("func_prof",5,30);
75  resProfile->Write();
76  //trackpTDist->Scale(1./ttree->GetEntries(),"width");
77  out_file->Write();
78  ttree->ResetBranchAddresses();
79  return uni_rate;
80 }
81 
82 unsigned totalMB(string path, string extension, string hardname, string softname){
83  string name = path+hardname+extension;
84  TFile *hardFile=new TFile(name.c_str(),"READ");
85  name=path+softname+extension;
86  TFile *softFile=new TFile(name.c_str(),"READ");
87  TTree* noPhoton = (TTree*) softFile->Get("nophotonTree");
88  unsigned r=noPhoton->GetEntries()*2000000;
89  noPhoton = (TTree*) hardFile->Get("nophotonTree");
90  r+=noPhoton->GetEntries()*2000000;
91  return r;
92 }
93 
97 TH1F* calculateRate(TEfficiency* rate,TFile* file,unsigned nMB){
98  //get the combined pythiaspec from the file then clone it to rate
99  TH1F* conversion_rate = (TH1F*)((TH1F*) file->Get("combinedpythia"))->Clone("rate");
100  //make a histogram for the uniform rate
101  TH1* uni_rate = (TH1F*)rate->GetPassedHistogram()->Clone("uni_rate");
102  uni_rate->Divide(rate->GetTotalHistogram());
103  conversion_rate->Multiply(uni_rate);
104  conversion_rate->Scale(1./nMB);
105  file->ReOpen("UPDATE");
106  conversion_rate->Write();
107  return conversion_rate;
108 }
109 
110 void derivitvePlot(TH1F* finalrate){
111  TH1F *dplot = new TH1F("derivative","",finalrate->GetNbinsX(),5,25);
112  for (int i = 1; i < finalrate->GetNbinsX(); ++i)
113  {
114  double error;
115  dplot->SetBinContent(i,finalrate->IntegralAndError(i,finalrate->GetNbinsX(),error));
116  dplot->SetBinError(i,error);
117  }
118  dplot->Write();
119 }
120 
122  TFile *out_file = new TFile("effplots.root","UPDATE");
123  TEfficiency* uni_rate=makepTRes(out_file);
124  string treeExtension = ".root";
125  if(!uni_rate){
126  string treePath = "/sphenix/user/vassalli/gammasample/truthconversiononlineanalysis";
127  unsigned int nFiles=200;
128  TChain *ttree = handleFile(treePath,treeExtension,"cutTreeSignal",nFiles);
129  TChain *observations = handleFile(treePath,treeExtension,"observTree",nFiles);
130  cout<<"Got tree: "<<ttree->GetName()<<" and "<<ttree->GetEntries()<<" entries"<<endl;
131  cout<<"Got tree: "<<observations->GetName()<<" and "<<observations->GetEntries()<<" entries"<<endl;
132  uni_rate=makepTRes(out_file,ttree,observations);
133  }
134  out_file->ReOpen("READ");
135  string treePath="/sphenix/user/vassalli/minBiasPythia/";
136  string softname = "softana";
137  string hardname = "hard4ana";
138  auto conversion_rate=calculateRate(uni_rate,out_file,totalMB(treePath,treeExtension,softname,hardname));
139  derivitvePlot(conversion_rate);
140 }