]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG4/JetTasks/AliAnalysisHelperJetTasks.cxx
added merging function for different pt hard bins
[u/mrichter/AliRoot.git] / PWG4 / JetTasks / AliAnalysisHelperJetTasks.cxx
1
2 #include "TROOT.h"
3 #include "TList.h"
4 #include "TH1F.h"
5 #include "TProfile.h"
6 #include "THnSparse.h"
7 #include "TFile.h"
8 #include "AliMCEvent.h"
9 #include "AliAODJet.h"
10 #include "AliStack.h"
11 #include "AliGenEventHeader.h"
12 #include "AliGenCocktailEventHeader.h"
13 #include "AliGenPythiaEventHeader.h"
14 #include <fstream>
15 #include <iostream>
16 #include "AliAnalysisHelperJetTasks.h"
17
18
19 ClassImp(AliAnalysisHelperJetTasks)
20
21
22
23  
24 AliGenPythiaEventHeader*  AliAnalysisHelperJetTasks::GetPythiaEventHeader(AliMCEvent *mcEvent){
25   
26   AliGenEventHeader* genHeader = mcEvent->GenEventHeader();
27   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
28   if(!pythiaGenHeader){
29     // cocktail ??
30     AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
31     
32     if (!genCocktailHeader) {
33       Printf("%s %d: Unknown header type (not Pythia or Cocktail)",(char*)__FILE__,__LINE__);
34       return 0;
35     }
36     TList* headerList = genCocktailHeader->GetHeaders();
37     for (Int_t i=0; i<headerList->GetEntries(); i++) {
38       pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
39       if (pythiaGenHeader)
40         break;
41     }
42     if(!pythiaGenHeader){
43       Printf("%s %d: PythiaHeader not found!",(char*)__FILE__,__LINE__);
44       return 0;
45     }
46   }
47   return pythiaGenHeader;
48
49 }
50
51
52 void AliAnalysisHelperJetTasks::PrintStack(AliMCEvent *mcEvent,Int_t iFirst,Int_t iLast,Int_t iMaxPrint){
53
54   AliStack *stack = mcEvent->Stack();
55   if(!stack){
56     Printf("%s%d No Stack available",(char*)__FILE__,__LINE__);
57     return;
58   }
59
60   static Int_t iCount = 0;
61   if(iCount>iMaxPrint)return;
62   Int_t nStack = stack->GetNtrack();
63   if(iLast == 0)iLast = nStack;
64   else if(iLast > nStack)iLast = nStack;
65
66
67   Printf("####################################################################");
68   for(Int_t np = iFirst;np<iLast;++np){
69     TParticle *p = stack->Particle(np);
70     Printf("Nr.%d --- Status %d ---- Mother1 %d Mother2 %d Daughter1 %d Daughter2 %d ",
71            np,p->GetStatusCode(),p->GetMother(0),p->GetMother(1),p->GetDaughter(0),p->GetDaughter(1));
72     Printf("Eta %3.3f Phi %3.3f ",p->Eta(),p->Phi()); 
73     p->Print();    
74     Printf("---------------------------------------");
75   }
76   iCount++;
77 }
78
79
80
81
82 void AliAnalysisHelperJetTasks::GetClosestJets(AliAODJet *genJets,const Int_t &nGenJets,
83                                                AliAODJet *recJets,const Int_t &nRecJets,
84                                                Int_t *iGenIndex,Int_t *iRecIndex,
85                                                Int_t iDebug,Float_t maxDist){
86
87   //
88   // Relate the two input jet Arrays
89   //
90
91   //
92   // The association has to be unique
93   // So check in two directions
94   // find the closest rec to a gen
95   // and check if there is no other rec which is closer
96   // Caveat: Close low energy/split jets may disturb this correlation
97
98   // Idea: search in two directions generated e.g (a--e) and rec (1--3)
99   // Fill a matrix with Flags (1 for closest rec jet, 2 for closest rec jet
100   // in the end we have something like this
101   //    1   2   3
102   // ------------
103   // a| 3   2   0
104   // b| 0   1   0
105   // c| 0   0   3
106   // d| 0   0   1
107   // e| 0   0   1
108   // Topology
109   //   1     2
110   //     a         b        
111   //
112   //  d      c
113   //        3     e
114   // Only entries with "3" match from both sides
115   
116   const int kMode = 3;
117
118   
119
120   if(nRecJets==0||nGenJets==0)return;
121
122   for(int i = 0;i < nGenJets;++i)iGenIndex[i] = -1;
123   for(int j = 0;j < nRecJets;++j)iRecIndex[j] = -1;
124
125   UShort_t *iFlag = new UShort_t[nGenJets*nRecJets];
126   for(int i = 0;i < nGenJets;++i){
127     for(int j = 0;j < nRecJets;++j){
128       iFlag[i*nGenJets+j] = 0;
129     }
130   }
131
132
133
134   // find the closest distance to the generated
135   for(int ig = 0;ig<nGenJets;++ig){
136     Float_t dist = maxDist;
137     if(iDebug>1)Printf("Gen (%d) p_T %3.3f eta %3.3f ph %3.3f ",ig,genJets[ig].Pt(),genJets[ig].Eta(),genJets[ig].Phi());
138     for(int ir = 0;ir<nRecJets;++ir){
139       Double_t dR = genJets[ig].DeltaR(&recJets[ir]);
140       if(iDebug>1)Printf("Rec (%d) p_T %3.3f eta %3.3f ph %3.3f ",ir,recJets[ir].Pt(),recJets[ir].Eta(),recJets[ir].Phi());
141       if(iDebug>1)Printf("Distance (%d)--(%d) %3.3f ",ig,ir,dR);
142       if(dR<dist){
143         iRecIndex[ig] = ir;
144         dist = dR;
145       } 
146     }
147     if(iRecIndex[ig]>=0)iFlag[ig*nGenJets+iRecIndex[ig]]+=1;
148     // reset...
149     iRecIndex[ig] = -1;
150   }
151   // other way around
152   for(int ir = 0;ir<nRecJets;++ir){
153     Float_t dist = maxDist;
154     for(int ig = 0;ig<nGenJets;++ig){
155       Double_t dR = genJets[ig].DeltaR(&recJets[ir]);
156       if(dR<dist){
157         iGenIndex[ir] = ig;
158         dist = dR;
159       } 
160     }
161     if(iGenIndex[ir]>=0)iFlag[iGenIndex[ir]*nGenJets+ir]+=2;
162     // reset...
163     iGenIndex[ir] = -1;
164   }
165
166   // check for "true" correlations
167
168   if(iDebug>1)Printf(">>>>>> Matrix");
169
170   for(int ig = 0;ig<nGenJets;++ig){
171     for(int ir = 0;ir<nRecJets;++ir){
172       // Print
173       if(iDebug>1)printf("Flag[%d][%d] %d ",ig,ir,iFlag[ig*nGenJets+ir]);
174
175       if(kMode==3){
176         // we have a uniqie correlation
177         if(iFlag[ig*nGenJets+ir]==3){
178           iGenIndex[ir] = ig;
179           iRecIndex[ig] = ir;
180         }
181       }
182       else{
183         // we just take the correlation from on side
184         if((iFlag[ig*nGenJets+ir]&2)==2){
185           iGenIndex[ir] = ig;
186         }
187         if((iFlag[ig*nGenJets+ir]&1)==1){
188           iRecIndex[ig] = ir;
189         }
190       }
191     }
192     if(iDebug>1)printf("\n");
193   }
194
195   delete [] iFlag;
196
197 }
198
199
200
201 void  AliAnalysisHelperJetTasks::MergeOutput(char* cFiles, char* cList){
202
203   // This is used to merge the analysis-output from different 
204   // data samples/pt_hard bins
205   // in case the eventweigth was set to xsection/ntrials already, this
206   // is not needed. Both methods only work in case we do not mix different 
207   // pt_hard bins, and do not have overlapping bins
208
209   const Int_t nMaxBins = 12;
210   // LHC08q jetjet100: Mean = 1.42483e-03, RMS = 6.642e-05
211   // LHC08r jetjet50: Mean = 2.44068e-02, RMS = 1.144e-03
212   // LHC08v jetjet15-50: Mean = 2.168291 , RMS = 7.119e-02
213   // const Float_t xsection[nBins] = {2.168291,2.44068e-02};
214
215   Float_t xsection[nMaxBins];
216   Float_t nTrials[nMaxBins];
217   Float_t sf[nMaxBins];
218   TList *lIn[nMaxBins];
219   TFile *fIn[nMaxBins];
220
221   ifstream in1;
222   in1.open(cFiles);
223
224   char cFile[120];
225   Int_t ibTotal = 0;
226   while(in1>>cFile){
227     fIn[ibTotal] = TFile::Open(cFile);
228     lIn[ibTotal] = (TList*)fIn[ibTotal]->Get(cList);
229     if(!lIn[ibTotal]){
230       Printf("%s:%d No list %s found, exiting...",__FILE__,__LINE__,cList);
231       fIn[ibTotal]->ls();
232       return;
233     }
234     TH1* hTrials = (TH1F*)lIn[ibTotal]->FindObject("fh1Trials");
235     if(!hTrials){
236       Printf("%s:%d fh1PtHard_Trials not found in list, exiting...",__FILE__,__LINE__);
237       return;
238     }
239     TProfile* hXsec = (TProfile*)lIn[ibTotal]->FindObject("fh1Xsec");
240     if(!hXsec){
241       Printf("%s:%d fh1Xsec  not found in list, exiting...",__FILE__,__LINE__);
242       return;
243     }
244     xsection[ibTotal] = hXsec->GetBinContent(1);
245     nTrials[ibTotal] = hTrials->Integral();
246     sf[ibTotal] = xsection[ibTotal]/ nTrials[ibTotal];
247     ibTotal++;
248   }
249
250   if(ibTotal==0){
251     Printf("%s:%d No files found for mergin, exiting",__FILE__,__LINE__);
252     return;
253   }
254
255   TFile *fOut = new TFile("allpt.root","RECREATE");
256   TList *lOut = new TList();
257   lOut->SetName(lIn[0]->GetName());
258   // for the start scale all...
259   for(int ie = 0; ie < lIn[0]->GetEntries();++ie){
260     TH1 *h1Add = 0;
261     THnSparse *hnAdd = 0;
262     for(int ib = 0;ib < ibTotal;++ib){
263       // dynamic cast does not work with cint
264       TObject *h = lIn[ib]->At(ie);
265       if(h->InheritsFrom("TH1")){
266         TH1 *h1 = (TH1*)h;
267         if(ib==0){
268           h1Add = (TH1*)h1->Clone(h1->GetName());
269           h1Add->Scale(sf[ib]);
270         }
271         else{
272           h1Add->Add(h1,sf[ib]);
273         }
274       }
275       else if(h->InheritsFrom("THnSparse")){
276         THnSparse *hn = (THnSparse*)h;
277         if(ib==0){
278           hnAdd = (THnSparse*)hn->Clone(hn->GetName());
279           hnAdd->Scale(sf[ib]);
280         }
281         else{
282           hnAdd->Add(hn,sf[ib]);
283         }
284       }
285       
286
287     }// ib
288     if(h1Add)lOut->Add(h1Add);
289     else if(hnAdd)lOut->Add(hnAdd);
290   }
291   fOut->cd();
292   lOut->Write(lOut->GetName(),TObject::kSingleKey);
293   fOut->Close();
294 }