Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
check2.cc
Go to the documentation of this file.
1 //
2 // fill histograms from trackanal.root tree
3 // the histograms are created in setupHits.C according to
4 // the number of Thrown tracks of the first event expecting
5 // all events to be the same Thrown topology.
6 //
7 // include files */
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <stdio.h>
12 #include <stdlib.h>
13 using namespace std;
14 
15 // root include files
16 #include "TFile.h"
17 #include "TH1.h"
18 #include "TH1F.h"
19 #include "TH2F.h"
20 #include "TH3F.h"
21 #include "TF1.h"
22 #include "TMath.h"
23 #include "TTree.h"
24 #include "TGraph.h"
25 #include "TCanvas.h"
26 #include "TLatex.h"
27 
28 #include <string.h>
29 
30 
31 TH1F *hist[99];
32 TH2F *hist2d[10];
33 void setupHists(TH1F** , TH2F** , Int_t , Int_t *, Int_t *);
34 void moreHists(TH1F** , TH2F** , Int_t , Int_t *, Int_t *, Float_t *);
35 
36 int main(int argc, char **argv) {
37 
38  Int_t DEBUG = 0;
39  char InFile[128] = "trackanal.root";
40  TFile *INF = NULL;
41  INF = new TFile(InFile);
42  INF->ls();
43 
44  Int_t EventNum;
45  Int_t NTrThrown;
46  Int_t ThrownPType[200]; // particle type of thrown tracks
47  Float_t ThrownPp[200]; // particle momentum of thrown tracks
48  Float_t ThrownQ[200]; // electric charge of thrown particle
49  Int_t NTrCand;
50  Float_t TrCandP[200]; // momentum of track candidate
51  Float_t TrCandQ[200]; // charge of track candidate
52  Float_t TrCandN[200]; // number of hits(Ndof) of track candidate
53  Float_t TrCandM[200]; // number of hits with match in TruthPoints for track candidates
54  Int_t NTrCandHits;
55  Int_t NTrFit;
56  Int_t NTrHits;
57  Int_t MaxT, MaxC, MaxF;
58  Int_t trlistPtype[250]; // particle type of track candidate with best FOM
59  Int_t trlistcand[250]; // track candidate number
60  Float_t trlistPp[250]; // particle momentum of track candidate with best FOM
61  Float_t trlistFOM[250]; //track fit FOM
62  Float_t trlistchisq[250]; //chisq
63  Float_t nh[900] ; // for each track candidate the chamber hits of each thrown particle track
64  Float_t ptypes[900]; // for each track candidate the chamer hits for each particle type
65 
66  TTree *TrackTree = (TTree*)INF->Get("TrackTree");
67  TrackTree->Print();
68 
69  TrackTree->SetBranchAddress("EventNum", &EventNum);
70  TrackTree->SetBranchAddress("MaxT", &MaxT);
71  TrackTree->SetBranchAddress("MaxC", &MaxC);
72  TrackTree->SetBranchAddress("MaxF", &MaxF);
73 
74  TrackTree->SetBranchAddress("NTrThrown", &NTrThrown);
75  TrackTree->SetBranchAddress("ThrownPType", ThrownPType);
76  TrackTree->SetBranchAddress("ThrownPp", ThrownPp);
77  TrackTree->SetBranchAddress("ThrownQ", ThrownQ);
78 
79  TrackTree->SetBranchAddress("NTrCand", &NTrCand);
80  TrackTree->SetBranchAddress("TrCandQ", TrCandQ);
81  TrackTree->SetBranchAddress("TrCandP", TrCandP);
82  TrackTree->SetBranchAddress("TrCandN", TrCandN);
83  TrackTree->SetBranchAddress("TrCandM", TrCandM);
84 
85  TrackTree->SetBranchAddress("NTrFit", &NTrFit);
86  TrackTree->SetBranchAddress("trlistPtype", trlistPtype);
87  TrackTree->SetBranchAddress("trlistFOM", trlistFOM);
88  TrackTree->SetBranchAddress("trlistchisq", trlistchisq);
89  TrackTree->SetBranchAddress("trlistPp", trlistPp);
90  TrackTree->SetBranchAddress("trlistcand", trlistcand);
91 
92  TrackTree->SetBranchAddress("NTrCandHits", &NTrCandHits);
93  TrackTree->SetBranchAddress("nh", nh);
94  TrackTree->SetBranchAddress("ptypes", ptypes);
95 
96  Int_t Offset[5] = {0, 0, 0, 0, 0};
97  for (int j=0;j<MaxF;j++){
98  for (int i=0;i<MaxF;i++){
99  ptypes[j*MaxF+i] = 0.0 ;
100  nh[j*MaxF+i] = 0.0;
101  }
102  }
103 
104  Long64_t nentries = TrackTree->GetEntries();
105  cout<<"Number of Entries: "<<nentries<<endl;
106 
107 
108  Float_t PMax[50];
109  for (Int_t j = 0;j<50;j++){
110  PMax[j] = 0;
111  }
112 
113 
114  for (Long64_t i=0; i<nentries;i++) {
115  //for (Long64_t i=0; i<5;i++) {
116 
117  // TrackTree->GetEntry(i++);
118  TrackTree->GetEntry(i);
119 
120  // take the first event to know how many tracks are thrown
121  // and generate the histograms accordingly.cd ../../
122  if (i==0) {
123  setupHists(hist, hist2d, NTrThrown, Offset, ThrownPType);
124  }
125 
126  for (Int_t k = 0;k<NTrThrown;k++){
127  if (ThrownPp[k]>PMax[k])
128  PMax[k] = ThrownPp[k];
129  }
130 
131 
132  hist[0]->Fill((Float_t)NTrThrown);
133  hist[2]->Fill((Float_t)NTrCand);
134 
135  Int_t SZ = MaxF;
136  Int_t idx = 0;
137  Int_t MostHits[MaxC];
138  Int_t TotHits[MaxC];
139  memset(MostHits,0,4*MaxC);
140  memset(TotHits,0,4*MaxC);
141 
142  Int_t MultCount[NTrThrown];
143  Float_t NQ = 0;
144  for (Int_t j=0;j<NTrThrown;j++){
145  MultCount[j] = 0;
146  if (ThrownQ[j]!=0){
147  NQ += 1.;
148  }
149  }
150  hist[1]->Fill(NQ);
151 
152 
153  for (Int_t j=0;j<NTrCand;j++){
154  Int_t MaxHits = 0;
155  Int_t idx = -1;
156  Int_t SumHits = 0;
157  Int_t MaxHits1 = 0;
158  Int_t ptype = 0;
159 
160  // ratio of hits with a mach in TruthPoint to all hits
161  hist[Offset[4]-1]->Fill(TrCandM[j]/TrCandN[j]);
162 
163  for (Int_t k=0;k<NTrThrown+1;k++){
164  //cout<<j<<" and "<<k<<endl;
165  SumHits += nh[j*SZ+k];
166  if (((Int_t)nh[j*SZ+k])>MaxHits){ // original thrown track with most hits
167  MaxHits = nh[j*SZ+k];
168  idx = k;
169  }
170  }
171  for (Int_t k=0;k<SZ;k++){
172  if (((Int_t)ptypes[j*SZ+k])>MaxHits1){ // particle type with most hits
173  MaxHits1 = ptypes[j*SZ+k];
174  ptype = k;
175  }
176  }
177  if (idx>-1){
178  MostHits[j] = nh[j*SZ+idx];
179  }
180 
181  TotHits[j] = SumHits;
182  hist[3]->Fill((Float_t)SumHits);
183  if (idx==0){
184  hist[4]->Fill(SumHits);
185  }
186 
187  if (idx>0){
188  Float_t rat = (Float_t)MostHits[j]/(Float_t) TotHits[j]*100.;
189  hist[Offset[2]+idx-1]->Fill(rat);
190  hist[4+idx]->Fill(SumHits);
191  MultCount[idx-1]++;
192  }
193 
194  // loop over track fits that match the candidate
195  Float_t chi2=9e99;
196  Int_t index = -1;
197  Int_t index1 = -1;
198  Int_t index2 = -1;
199  Float_t FOM = 0;
200  for (Int_t k=0;k<NTrFit;k++){
201  if (trlistcand[k]==j+1){
202  if (chi2>trlistchisq[k]){
203  chi2 = trlistchisq[k];
204  index1 = k;
205  }
206  if (FOM<trlistFOM[k]){
207  FOM = trlistFOM[k];
208  index2 = k;
209  }
210  }
211  }
212 
213  if (index2>-1){
214  index = index2;
215  } else {
216  index = index1;
217  }
218 
219  if (index<0) // all fits for this candidate failed.
220  continue;
221 
222  // for the thrown tracks it is always track 1=pi+ track2=pi- and track3=proton
223  Int_t test1 = 0;
224  for (Int_t kk=0;kk<NTrThrown;kk++){
225  test1 += ((idx==kk+1) && (ptype==ThrownPType[kk]));
226  }
227 
228  if ( test1 && idx>0) {
229  Int_t trptype = trlistPtype[index];
230  Float_t prat = ((Float_t)trptype)/((Float_t)ptype);
231  Float_t ptrack = trlistPp[index];
232  Float_t ptrue = ThrownPp[idx-1];
233  hist[Offset[0]+idx-1]->Fill(ptrack/ptrue);
234  hist[Offset[1]+idx-1]->Fill(prat);
235  hist2d[0+idx-1]->Fill(ptrack/ptrue, (Float_t)nh[j*SZ+idx]);
236  hist2d[Offset[5]+idx]->Fill(prat, (Float_t)nh[j*SZ+idx]);
237  } else if (idx==0) { //most hits for this track candidate come from secondary
238  Int_t trptype = trlistPtype[index];
239  Float_t prat = ((Float_t)trptype)/((Float_t)ptype);
240  hist[Offset[2]-1]->Fill(prat);
241  hist2d[Offset[5]]->Fill(prat, (Float_t)nh[j*SZ+idx]);
242  }
243 
244  if (DEBUG){
245  cout<<j<<" "<<idx<<" ";
246  if (idx>-1) {
247  cout<<nh[j*SZ+idx]/SumHits*100.<<" "<<SumHits<<endl;
248  } else {
249  cout<<" ?/SumHits "<<SumHits<<endl;
250  }
251  }
252  }
253 
254  if (DEBUG==2){
255  for (Int_t j=0;j<NTrFit;j++){
256  cout<< i<<" "<< j << " " << trlistcand[j]<< " " <<trlistFOM[j] << " " <<
257  trlistchisq[j]<<" "<<trlistPp[j]<<" "<<trlistPtype[j]<<endl;
258  }
259  }
260 
261  //if (i==20)
262  // break;
263 
264  for (Int_t k=0; k<NTrThrown; k++){
265  hist[Offset[3]+k]->Fill((Float_t)MultCount[k]);
266  }
267 
268  }
269 
270  //cout<<"End looping over data"<<endl;
271  // now loop once more the make nice momentum distributions
272  // but first create the histograms.
273 
274  moreHists(hist, hist2d, NTrThrown,
275  Offset, ThrownPType, PMax);
276 
277  for (Long64_t i=0; i<nentries;i++) {
278  //for (Long64_t i=0; i<5;i++) {
279 
280  // TrackTree->GetEntry(i++);
281  TrackTree->GetEntry(i);
282 
283  for (Int_t k=0;k<NTrThrown;k++){
284  hist[Offset[4]+k]->Fill(ThrownPp[k]);
285  }
286 
287  }
288 
289 
290 
291  char fnam[128];
292  sprintf(fnam,"histograms_trackanal.root");
293  TFile *fout = new TFile(fnam,"RECREATE");
294 
295  TList * hist_list= new TList();
296  Int_t Nhist = Offset[4] + NTrThrown;
297  cout<<"Number of 1D histos: "<<Nhist<<endl;
298  for (Int_t j=0;j<Nhist;j++){
299  hist_list->Add(hist[j]);
300  }
301 
302  TList * hist2d_list= new TList();
303  Int_t Nhist2d = Offset[5] + NTrThrown;
304  cout<<"Number of 2D histos: "<<Nhist2d<<endl;
305  for (Int_t j=0;j<Nhist2d;j++){
306  hist2d_list->Add(hist2d[j]);
307  }
308 
309  hist_list->Write("hist_list", TObject::kSingleKey);
310  hist2d_list->Write("hist2d_list", TObject::kSingleKey);
311  fout->Close();
312 
313 
314  return 0;
315 }
316 // -------------- END OF MAIN -------------------------------
317 
318 //
319 // ------------------------------------------------------------
320 //
321 
322 void setupHists(TH1F** hist, TH2F** hist2d, Int_t NTrThrown,
323  Int_t *Offset, Int_t *ThrownPType){
324 
325  Int_t k = 0;
326 
327  hist[0] = new TH1F("hist0","All Tracks Thrown", 16, -0.5, 15.5);
328  hist[1] = new TH1F("hist1","Charged Tracks Thrown", 16, -0.5, 15.5);
329  hist[2] = new TH1F("hist2","Track Candidates", 16, -0.5, 15.5);
330  hist[3] = new TH1F("hist3","Tracking Hits of Candidates ", 41, -0.5, 40.5);
331  hist[0]->GetXaxis()->SetTitle("Number of track per event");
332  hist[1]->GetXaxis()->SetTitle("Number of track per event");
333  hist[2]->GetXaxis()->SetTitle("Number of track per event");
334  hist[3]->GetXaxis()->SetTitle("Number of hits for track candidates");
335 
336  for (Int_t i=0;i<NTrThrown;i++) {
337  char str1[128];
338  char str2[128];
339  sprintf(str1,"hist%d",i+5);
340  sprintf(str2,"Thrown Track %d, Ptype=%d : Cand Hits ",i+1,ThrownPType[i]);
341  hist[i+5] = new TH1F(str1, str2, 41, -0.5, 40.5);
342  hist[i+5]->GetXaxis()->SetTitle("Number of hits per track");
343  }
344  char str1[128];
345  char str2[128];
346  sprintf(str1,"hist%d",4+NTrThrown);
347  sprintf(str2,"Track X Cand Hits ");
348 
349  hist[4] = new TH1F("hist4", str2, 41, -0.5, 40.5);
350 
351 
352  Offset[0] = NTrThrown + 4 + 1;
353  for (Int_t i=0;i<NTrThrown;i++) {
354  char str1[128];
355  char str2[128];
356  sprintf(str1,"hist%d",i+Offset[0]);
357  sprintf(str2,"Track %d Candidate p_det/p_true",i+1);
358  hist[i+Offset[0]] = new TH1F(str1, str2,160, -2., 2.);
359  hist[i+Offset[0]]->GetXaxis()->SetTitle("p_det/p_true");
360  k = i;
361  }
362 
363  Offset[1] = Offset[0] + NTrThrown;
364 
365  for (Int_t i=0;i<NTrThrown;i++) {
366  char str1[128];
367  char str2[128];
368  sprintf(str1,"hist%d",i+Offset[1]);
369  sprintf(str2,"PID_Tracking/True_PID Thrown Track %d",i+1);
370  hist[i+Offset[1]] = new TH1F(str1, str2, 50, -0.5, 2.5);
371  hist[i+Offset[1]]->GetXaxis()->SetTitle("pid_det/pid_gen");
372  k = i;
373  }
374 
375  sprintf(str1,"hist%d",k+1+Offset[1]);
376  hist[k+1+Offset[1]] = new TH1F(str1, "PID_Tracking/True_PID secondary Track X", 50, -0.5, 2.5);
377  hist[k+1+Offset[1]]->GetXaxis()->SetTitle("pid_det/pid_gen");
378 
379  Offset[2] = Offset[1] + NTrThrown + 1;
380 
381  for (Int_t i=0;i<NTrThrown;i++) {
382  char str1[128];
383  char str2[128];
384  sprintf(str1,"hist%d",i+Offset[2]);
385  sprintf(str2,"Thrown Track %d PType=%d : NHits/TotHits",i+1,ThrownPType[i]);
386  hist[i+Offset[2]] = new TH1F(str1, str2, 20, 40., 100.1);
387  hist[i+Offset[2]]->GetXaxis()->SetTitle("% of hits from thrown particle");
388  k = i;
389  }
390 
391  Offset[3] = Offset[2] + NTrThrown;
392 
393  for (Int_t i=0;i<NTrThrown;i++) {
394  char str1[128];
395  char str2[128];
396  sprintf(str1,"hist%d",i+Offset[3]);
397  sprintf(str2,"Thrown Track %d PType=%d : Candidates",i+1,ThrownPType[i] );
398  hist[i+Offset[3]] = new TH1F(str1, str2, 6, -0.5, 5.5);
399  hist[i+Offset[3]]->GetXaxis()->SetTitle("Candidate Multiplicity");
400  k = i;
401  }
402 
403  Offset[4] = Offset[3] + NTrThrown;
404 
405  sprintf(str1,"hist%d",Offset[4]);
406  hist[Offset[4]] = new TH1F(str1, "Matched_Hits/Hits", 100, 0.5, 1.5);
407  hist[Offset[4]]->GetXaxis()->SetTitle("Ratio Associated_Hits/All_Hits");
408  Offset[4]+=1;
409 
410  // now setup the 2 dimensional histograms
411  for (Int_t i=0;i<NTrThrown;i++) {
412  char str1[128];
413  char str2[128];
414  sprintf(str1,"hist2d%d",i);
415  sprintf(str2,"Thrown track %d NHits vs. #Delta p/p", i+1);
416  hist2d[i] = new TH2F(str1, str2, 160, -2.,2.,41, -0.5,40.5);
417  hist2d[i]->GetXaxis()->SetTitle("(p_det/p_gen)");
418  hist2d[i]->GetYaxis()->SetTitle("Number of Hits");
419  k = i;
420  }
421  Offset[5] = NTrThrown;
422 
423  for (Int_t i=0;i<NTrThrown;i++) {
424  char str1[128];
425  char str2[128];
426  sprintf(str1,"hist2d%d",i+Offset[5]+1);
427  sprintf(str2,"Thrown track %d NHits vs.ID/TRUE_ID", i+1);
428  hist2d[i+Offset[5]+1] = new TH2F(str1, str2, 50, -.5,2.5, 41, -0.5,40.5);
429  hist2d[i+Offset[5]+1]->GetXaxis()->SetTitle("ID_det/ID_true");
430  hist2d[i+Offset[5]+1]->GetYaxis()->SetTitle("Number of Hits");
431  k = i;
432  }
433  sprintf(str1,"hist2d%d",Offset[5]);
434  hist2d[Offset[5]] = new TH2F(str1,"TrX NHits vs. ID/TRUE_ID X",50, -.5,2.5, 41, -0.5,40.5);
435  hist2d[Offset[5]]->GetXaxis()->SetTitle("ID_det/ID_true");
436  hist2d[Offset[5]]->GetYaxis()->SetTitle("Number of Hits");
437 
438 
439 }
440 //
441 //
442 void moreHists(TH1F** hist, TH2F** hist2d, Int_t NTrThrown,
443  Int_t *Offset, Int_t *ThrownPType, Float_t *PMax){
444 
445 
446  char str1[128];
447  char str2[128];
448  for (Int_t i=0;i<NTrThrown;i++) {
449  sprintf(str1,"hist%d",i+Offset[4]);
450  sprintf(str2,"Thrown Track %d PType=%d",i+1,ThrownPType[i]);
451  hist[i+Offset[4]] = new TH1F(str1, str2, 50, 0.0, PMax[i]*1.1);
452  hist[i+Offset[4]]->GetXaxis()->SetTitle("Track Momentum [GeV/c]");
453  }
454 
455 }
TH1F * hist2d[Idx+1]
Definition: readhist.C:16
void moreHists(TH1F **, TH2F **, Int_t, Int_t *, Int_t *, Float_t *)
Definition: check2.cc:442
sprintf(text,"Post KinFit Cut")
#define MaxHits
static char index(char c)
Definition: base64.cpp:115
void setupHists(TH1F **, TH2F **, Int_t, Int_t *, Int_t *)
Definition: check2.cc:322
TList * hist2d_list
Definition: readhist.C:7
TList * hist_list
Definition: readhist.C:6
char fnam[128]
Definition: readhist.C:3
#define DEBUG
Definition: tw_corr.C:41
int main(int argc, char *argv[])
Definition: gendoc.cc:6
TH1F * hist[Idx+1]
Definition: readhist.C:10