]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG4/JetTasks/AliAnalysisHelperJetTasks.cxx
Add method to merge only certain directories from on output file
[u/mrichter/AliRoot.git] / PWG4 / JetTasks / AliAnalysisHelperJetTasks.cxx
1
2 #include "TROOT.h"
3 #include "TDirectory.h"
4 #include "TKey.h"
5 #include "TList.h"
6 #include "TSystem.h"
7 #include "TH1F.h"
8 #include "TProfile.h"
9 #include "THnSparse.h"
10 #include "TSeqCollection.h"
11 #include "TMethodCall.h"
12 #include "TFile.h"
13 #include "TString.h"
14 #include "AliMCEvent.h"
15 #include "AliLog.h"
16 #include "AliESDEvent.h"
17 #include "AliAODJet.h"
18 #include "AliAODEvent.h"
19 #include "AliStack.h"
20 #include "AliGenEventHeader.h"
21 #include "AliGenCocktailEventHeader.h"
22 #include <AliGenDPMjetEventHeader.h>
23 #include "AliGenPythiaEventHeader.h"
24 #include <fstream>
25 #include <iostream>
26 #include "AliAnalysisHelperJetTasks.h"
27 #include "TMatrixDSym.h"
28 #include "TMatrixDSymEigen.h"
29 #include "TVector.h"
30
31 ClassImp(AliAnalysisHelperJetTasks)
32
33 Int_t AliAnalysisHelperJetTasks::fgLastProcessType = -1;
34
35  
36 AliGenPythiaEventHeader*  AliAnalysisHelperJetTasks::GetPythiaEventHeader(AliMCEvent *mcEvent){
37   
38   if(!mcEvent)return 0;
39   AliGenEventHeader* genHeader = mcEvent->GenEventHeader();
40   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
41   if(!pythiaGenHeader){
42     // cocktail ??
43     AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
44     
45     if (!genCocktailHeader) {
46       AliWarningGeneral(Form(" %s:%d",(char*)__FILE__,__LINE__),"Unknown header type (not Pythia or Cocktail)");
47       //      AliWarning(Form("%s %d: Unknown header type (not Pythia or Cocktail)",(char*)__FILE__,__LINE__));
48       return 0;
49     }
50     TList* headerList = genCocktailHeader->GetHeaders();
51     for (Int_t i=0; i<headerList->GetEntries(); i++) {
52       pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
53       if (pythiaGenHeader)
54         break;
55     }
56     if(!pythiaGenHeader){
57       AliWarningGeneral(Form(" %s:%d",(char*)__FILE__,__LINE__),"Pythia event header not found");
58       return 0;
59     }
60   }
61   return pythiaGenHeader;
62
63 }
64
65
66 void AliAnalysisHelperJetTasks::PrintStack(AliMCEvent *mcEvent,Int_t iFirst,Int_t iLast,Int_t iMaxPrint){
67
68   AliStack *stack = mcEvent->Stack();
69   if(!stack){
70     Printf("%s%d No Stack available",(char*)__FILE__,__LINE__);
71     return;
72   }
73
74   static Int_t iCount = 0;
75   if(iCount>iMaxPrint)return;
76   Int_t nStack = stack->GetNtrack();
77   if(iLast == 0)iLast = nStack;
78   else if(iLast > nStack)iLast = nStack;
79
80
81   Printf("####################################################################");
82   for(Int_t np = iFirst;np<iLast;++np){
83     TParticle *p = stack->Particle(np);
84     Printf("Nr.%d --- Status %d ---- Mother1 %d Mother2 %d Daughter1 %d Daughter2 %d ",
85            np,p->GetStatusCode(),p->GetMother(0),p->GetMother(1),p->GetDaughter(0),p->GetDaughter(1));
86     Printf("Eta %3.3f Phi %3.3f ",p->Eta(),p->Phi()); 
87     p->Print();    
88     Printf("---------------------------------------");
89   }
90   iCount++;
91 }
92
93
94
95
96 void AliAnalysisHelperJetTasks::GetClosestJets(AliAODJet *genJets,const Int_t &kGenJets,
97                                                AliAODJet *recJets,const Int_t &kRecJets,
98                                                Int_t *iGenIndex,Int_t *iRecIndex,
99                                                Int_t iDebug,Float_t maxDist){
100
101   //
102   // Relate the two input jet Arrays
103   //
104
105   //
106   // The association has to be unique
107   // So check in two directions
108   // find the closest rec to a gen
109   // and check if there is no other rec which is closer
110   // Caveat: Close low energy/split jets may disturb this correlation
111
112
113   // Idea: search in two directions generated e.g (a--e) and rec (1--3)
114   // Fill a matrix with Flags (1 for closest rec jet, 2 for closest rec jet
115   // in the end we have something like this
116   //    1   2   3
117   // ------------
118   // a| 3   2   0
119   // b| 0   1   0
120   // c| 0   0   3
121   // d| 0   0   1
122   // e| 0   0   1
123   // Topology
124   //   1     2
125   //     a         b        
126   //
127   //  d      c
128   //        3     e
129   // Only entries with "3" match from both sides
130
131   // In case we have more jets than kmaxjets only the 
132   // first kmaxjets are searched
133   // all other are -1
134   // use kMaxJets for a test not to fragemnt the memory...
135
136   for(int i = 0;i < kGenJets;++i)iGenIndex[i] = -1;
137   for(int j = 0;j < kRecJets;++j)iRecIndex[j] = -1;
138
139
140   
141   const int kMode = 3;
142
143   const Int_t nGenJets = TMath::Min(kMaxJets,kGenJets);
144   const Int_t nRecJets = TMath::Min(kMaxJets,kRecJets);
145
146   if(nRecJets==0||nGenJets==0)return;
147
148   // UShort_t *iFlag = new UShort_t[nGenJets*nRecJets];
149   UShort_t iFlag[kMaxJets*kMaxJets];
150   for(int i = 0;i < nGenJets;++i){
151     for(int j = 0;j < nRecJets;++j){
152       iFlag[i*nGenJets+j] = 0;
153     }
154   }
155
156
157
158   // find the closest distance to the generated
159   for(int ig = 0;ig<nGenJets;++ig){
160     Float_t dist = maxDist;
161     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());
162     for(int ir = 0;ir<nRecJets;++ir){
163       Double_t dR = genJets[ig].DeltaR(&recJets[ir]);
164       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());
165       if(iDebug>1)Printf("Distance (%d)--(%d) %3.3f ",ig,ir,dR);
166       if(dR<dist){
167         iRecIndex[ig] = ir;
168         dist = dR;
169       } 
170     }
171     if(iRecIndex[ig]>=0)iFlag[ig*nGenJets+iRecIndex[ig]]+=1;
172     // reset...
173     iRecIndex[ig] = -1;
174   }
175   // other way around
176   for(int ir = 0;ir<nRecJets;++ir){
177     Float_t dist = maxDist;
178     for(int ig = 0;ig<nGenJets;++ig){
179       Double_t dR = genJets[ig].DeltaR(&recJets[ir]);
180       if(dR<dist){
181         iGenIndex[ir] = ig;
182         dist = dR;
183       } 
184     }
185     if(iGenIndex[ir]>=0)iFlag[iGenIndex[ir]*nGenJets+ir]+=2;
186     // reset...
187     iGenIndex[ir] = -1;
188   }
189
190   // check for "true" correlations
191
192   if(iDebug>1)Printf(">>>>>> Matrix");
193
194   for(int ig = 0;ig<nGenJets;++ig){
195     for(int ir = 0;ir<nRecJets;++ir){
196       // Print
197       if(iDebug>1)printf("Flag[%d][%d] %d ",ig,ir,iFlag[ig*nGenJets+ir]);
198
199       if(kMode==3){
200         // we have a uniqie correlation
201         if(iFlag[ig*nGenJets+ir]==3){
202           iGenIndex[ir] = ig;
203           iRecIndex[ig] = ir;
204         }
205       }
206       else{
207         // we just take the correlation from on side
208         if((iFlag[ig*nGenJets+ir]&2)==2){
209           iGenIndex[ir] = ig;
210         }
211         if((iFlag[ig*nGenJets+ir]&1)==1){
212           iRecIndex[ig] = ir;
213         }
214       }
215     }
216     if(iDebug>1)printf("\n");
217   }
218 }
219
220
221 void  AliAnalysisHelperJetTasks::MergeOutputDirs(const char* cFiles,const char* cPattern,char *cOutFile){
222   // Routine to merge only directories containing the pattern
223   //
224   TString outFile(cOutFile);
225   if(outFile.Length()==0)outFile = Form("%s.root",cPattern);
226   ifstream in1;
227   in1.open(cFiles);
228   // open all files
229   TList fileList;
230   char cFile[240];
231   if(in1>>cFile){// only open the first file
232     fileList.Add(TFile::Open(cFile));
233   }
234
235   TFile *fOut = 0;
236   if(fileList.GetEntries()){// open the first file
237     TFile* fIn = dynamic_cast<TFile*>(fileList.At(0));
238     if(fIn){
239       Printf("Input File not Found");
240     }
241     // fetch the keys for the directories
242     TList *ldKeys = fIn->GetListOfKeys();
243     for(int id = 0;id < ldKeys->GetEntries();id++){
244       // check if it is a directory
245       TKey *dKey = (TKey*)ldKeys->At(id);
246       TDirectory *dir = dynamic_cast<TDirectory*>(dKey->ReadObj());
247       if(dir){
248         TString dName(dir->GetName());
249         if(dName.Contains(cPattern)){
250           // open new file if first match
251           if(!fOut){
252             fOut = new TFile(outFile.Data(),"RECREATE");
253           }
254           // merge all the stuff that is in this directory
255           TList *llKeys = dir->GetListOfKeys();
256           TList *tmplist;
257           TMethodCall callEnv;
258           for(int il = 0;il < llKeys->GetEntries();il++){
259             TKey *lKey = (TKey*)llKeys->At(id);
260             TObject *object = dynamic_cast<TObject*>(lKey->ReadObj());
261             //  from TSeqCollections::Merge
262             if(!object)continue;
263             // If current object is not mergeable just skip it
264             if (!object->IsA()) {
265               continue;
266             }
267             callEnv.InitWithPrototype(object->IsA(), "Merge", "TCollection*");
268             if (!callEnv.IsValid()) {
269               continue;
270             }
271             // Current object mergeable - get corresponding objects in input lists
272             tmplist = new TList();
273             for(int i = 1; i < fileList.GetEntries();i++){
274               TDirectory *fInTmp = dynamic_cast<TDirectory*>(fileList.At(i)); 
275               if(!fInTmp){
276                 Printf("File %d not found",i);
277                 continue;
278               }
279               TDirectory *dTmp = (TDirectory*)fInTmp->Get(dName.Data());
280               if(!dTmp){
281                 Printf("Directory %s not found",dName.Data());
282                 continue;
283               }
284               TObject* oTmp = (TObject*)dTmp->Get(object->GetName());
285               if(!oTmp){
286                 Printf("Object %s not found",object->GetName());
287                 continue;
288               }
289               tmplist->Add(oTmp);
290             }
291             callEnv.SetParam((Long_t) tmplist);
292             callEnv.Execute(object);
293             delete tmplist;tmplist = 0;
294             fOut->cd();
295             object->Write();
296           }
297         }
298       }
299     }
300     if(fOut){
301       fOut->Close();
302     }
303   }
304 }
305
306 void  AliAnalysisHelperJetTasks::MergeOutput(char* cFiles, char* cDir, char *cList,char *cOutFile,Bool_t bUpdate){
307
308   // This is used to merge the analysis-output from different 
309   // data samples/pt_hard bins
310   // in case the eventweigth was set to xsection/ntrials already, this
311   // is not needed. Both methods only work in case we do not mix different 
312   // pt_hard bins, and do not have overlapping bins
313
314   const Int_t nMaxBins = 12;
315   // LHC08q jetjet100: Mean = 1.42483e-03, RMS = 6.642e-05
316   // LHC08r jetjet50: Mean = 2.44068e-02, RMS = 1.144e-03
317   // LHC08v jetjet15-50: Mean = 2.168291 , RMS = 7.119e-02
318   // const Float_t xsection[nBins] = {2.168291,2.44068e-02};
319
320   Float_t xsection[nMaxBins];
321   Float_t nTrials[nMaxBins];
322   Float_t sf[nMaxBins];
323   TList *lIn[nMaxBins];
324   TDirectory *dIn[nMaxBins];
325   TFile *fIn[nMaxBins];
326
327   ifstream in1;
328   in1.open(cFiles);
329
330   char cFile[120];
331   Int_t ibTotal = 0;
332   while(in1>>cFile){
333     fIn[ibTotal] = TFile::Open(cFile);
334     if(strlen(cDir)==0){
335       dIn[ibTotal] = gDirectory;
336     }
337     else{
338       dIn[ibTotal] = (TDirectory*)fIn[ibTotal]->Get(cDir);
339     }
340     if(!dIn[ibTotal]){
341       Printf("%s:%d No directory %s found, exiting...",__FILE__,__LINE__,cDir);
342       fIn[ibTotal]->ls();
343       return;
344     }
345
346     lIn[ibTotal] = (TList*)dIn[ibTotal]->Get(cList);
347     Printf("Merging file %s %s",cFile, cDir);
348     if(!lIn[ibTotal]){
349       Printf("%s:%d No list %s found, exiting...",__FILE__,__LINE__,cList);
350       fIn[ibTotal]->ls();
351       return;
352     }
353     TH1* hTrials = (TH1F*)lIn[ibTotal]->FindObject("fh1Trials");
354     if(!hTrials){
355       Printf("%s:%d fh1PtHard_Trials not found in list, exiting...",__FILE__,__LINE__);
356       return;
357     }
358     TProfile* hXsec = (TProfile*)lIn[ibTotal]->FindObject("fh1Xsec");
359     if(!hXsec){
360       Printf("%s:%d fh1Xsec  not found in list, exiting...",__FILE__,__LINE__);
361       return;
362     }
363     xsection[ibTotal] = hXsec->GetBinContent(1);
364     nTrials[ibTotal] = hTrials->Integral();
365     sf[ibTotal] = xsection[ibTotal]/ nTrials[ibTotal];
366     ibTotal++;
367   }
368
369   if(ibTotal==0){
370     Printf("%s:%d No files found for mergin, exiting",__FILE__,__LINE__);
371     return;
372   }
373
374   TFile *fOut = 0;
375   if(bUpdate)fOut = new TFile(cOutFile,"UPDATE");
376   else fOut = new TFile(cOutFile,"RECREATE");
377   TDirectory *dOut = fOut->mkdir(dIn[0]->GetName());
378   dOut->cd();
379   TList *lOut = new TList();
380   lOut->SetName(lIn[0]->GetName());
381
382   // for the start scale all...
383   for(int ie = 0; ie < lIn[0]->GetEntries();++ie){
384     TH1 *h1Add = 0;
385     THnSparse *hnAdd = 0;
386     for(int ib = 0;ib < ibTotal;++ib){
387       // dynamic cast does not work with cint
388       TObject *h = lIn[ib]->At(ie);
389       if(h->InheritsFrom("TH1")){
390         TH1 *h1 = (TH1*)h;
391         if(ib==0){
392           h1Add = (TH1*)h1->Clone(h1->GetName());
393           h1Add->Scale(sf[ib]);
394         }
395         else{
396           h1Add->Add(h1,sf[ib]);
397         }
398       }
399       else if(h->InheritsFrom("THnSparse")){
400         THnSparse *hn = (THnSparse*)h;
401         if(ib==0){
402           hnAdd = (THnSparse*)hn->Clone(hn->GetName());
403           hnAdd->Scale(sf[ib]);
404         }
405         else{
406           hnAdd->Add(hn,sf[ib]);
407         }
408       }
409       
410
411     }// ib
412     if(h1Add)lOut->Add(h1Add);
413     else if(hnAdd)lOut->Add(hnAdd);
414   }
415   dOut->cd();
416   lOut->Write(lOut->GetName(),TObject::kSingleKey);
417   fOut->Close();
418 }
419
420 Bool_t AliAnalysisHelperJetTasks::PythiaInfoFromFile(const char* currFile,Float_t &fXsec,Float_t &fTrials){
421   //
422   // get the cross section and the trails either from pyxsec.root or from pysec_hists.root
423   // This is to called in Notify and should provide the path to the AOD/ESD file
424
425   TString file(currFile);  
426   fXsec = 0;
427   fTrials = 1;
428
429   if(file.Contains("root_archive.zip#")){
430     Ssiz_t pos1 = file.Index("root_archive",12,TString::kExact);
431     Ssiz_t pos = file.Index("#",1,pos1,TString::kExact);
432     file.Replace(pos+1,20,"");
433   }
434   else {
435     // not an archive take the basename....
436     file.ReplaceAll(gSystem->BaseName(file.Data()),"");
437   }
438   Printf("%s",file.Data());
439   
440  
441    
442
443   TFile *fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec.root")); // problem that we cannot really test the existance of a file in a archive so we have to lvie with open error message from root
444   if(!fxsec){
445     // next trial fetch the histgram file
446     fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec_hists.root"));
447     if(!fxsec){
448         // not a severe condition but inciate that we have no information
449       return kFALSE;
450     }
451     else{
452       // find the tlist we want to be independtent of the name so use the Tkey
453       TKey* key = (TKey*)fxsec->GetListOfKeys()->At(0); 
454       if(!key){
455         fxsec->Close();
456         return kFALSE;
457       }
458       TList *list = dynamic_cast<TList*>(key->ReadObj());
459       if(!list){
460         fxsec->Close();
461         return kFALSE;
462       }
463       fXsec = ((TProfile*)list->FindObject("h1Xsec"))->GetBinContent(1);
464       fTrials  = ((TH1F*)list->FindObject("h1Trials"))->GetBinContent(1);
465       fxsec->Close();
466     }
467   } // no tree pyxsec.root
468   else {
469     TTree *xtree = (TTree*)fxsec->Get("Xsection");
470     if(!xtree){
471       fxsec->Close();
472       return kFALSE;
473     }
474     UInt_t   ntrials  = 0;
475     Double_t  xsection  = 0;
476     xtree->SetBranchAddress("xsection",&xsection);
477     xtree->SetBranchAddress("ntrials",&ntrials);
478     xtree->GetEntry(0);
479     fTrials = ntrials;
480     fXsec = xsection;
481     fxsec->Close();
482   }
483   return kTRUE;
484 }
485
486 Bool_t AliAnalysisHelperJetTasks::PrintDirectorySize(const char* currFile){
487
488   TFile *fIn = TFile::Open(currFile);
489   if(!fIn){
490     // not a severe condition but inciate that we have no information
491     return kFALSE;
492   }
493   // find the tlists we want to be independtent of the name so use the Tkey
494   TList* keyList = fIn->GetListOfKeys();
495   Float_t memorySize = 0;
496   Float_t diskSize = 0;
497
498   for(int i = 0;i < keyList->GetEntries();i++){
499     TKey* ikey = (TKey*)keyList->At(i); 
500     
501     //    TList *list = dynamic_cast<TList*>(key->ReadObj());
502     //    TNamed *name = dynamic_cast<TNamed*>(ikey->ReadObj());
503     TDirectory *dir =  dynamic_cast<TDirectory*>(ikey->ReadObj());
504     
505
506
507
508     if(dir){
509       Printf("%03d    : %60s %8d %8d ",i,dir->GetName(),ikey->GetObjlen(),ikey->GetNbytes());
510       TList * dirKeyList = dir->GetListOfKeys();
511       for(int j = 0;j<dirKeyList->GetEntries();j++){
512         TKey* jkey = (TKey*)dirKeyList->At(j); 
513         TList *list =  dynamic_cast<TList*>(jkey->ReadObj());
514
515         memorySize += (Float_t)jkey->GetObjlen()/1024./1024.;
516         diskSize +=  (Float_t)jkey->GetNbytes()/1024./1024.;
517         if(list){
518           Printf("%03d/%03d: %60s %5.2f MB %5.2f MB",i,j,list->GetName(),(Float_t)jkey->GetObjlen()/1024./1024.,(Float_t)jkey->GetNbytes()/1024./1024.);
519         }
520         else{
521           Printf("%03d/%03d: %60s %5.2f MB %5.2f MB",i,j,jkey->GetName(),(Float_t)jkey->GetObjlen()/1024./1024.,(Float_t)jkey->GetNbytes()/1024./1024.);
522         }
523       }
524     }
525   }
526   Printf("Total %5.2f MB %5.2f MB",memorySize,diskSize);
527   return kTRUE;
528 }
529
530
531 Bool_t  AliAnalysisHelperJetTasks::Selected(Bool_t bSet,Bool_t bNew){
532   static Bool_t bSelected = kTRUE; // if service task is not run we acccpet all
533   if(bSet){
534     bSelected = bNew;
535   }
536   return bSelected;
537 }
538
539 Bool_t  AliAnalysisHelperJetTasks::IsCosmic(){
540   return ((SelectInfo()&kIsCosmic)==kIsCosmic);
541 }
542
543 Bool_t  AliAnalysisHelperJetTasks::IsPileUp(){
544   return ((SelectInfo()&kIsPileUp)==kIsPileUp);
545 }
546
547
548 Bool_t  AliAnalysisHelperJetTasks::TestSelectInfo(UInt_t iMask){
549   return ((SelectInfo()&iMask)==iMask);
550 }
551
552
553 UInt_t  AliAnalysisHelperJetTasks::SelectInfo(Bool_t bSet,UInt_t iNew){
554   static UInt_t iSelectInfo = 0; //
555   if(bSet){
556     iSelectInfo = iNew;
557   }
558   return iSelectInfo;
559 }
560
561
562 //___________________________________________________________________________________________________________
563
564 Bool_t AliAnalysisHelperJetTasks::GetEventShapes(TVector3 &n01, TVector3 * pTrack, Int_t nTracks, Double_t * eventShapes)
565 {       
566   // ***
567   // Event shape calculation
568   // sona.pochybova@cern.ch
569
570   const Int_t kTracks = 1000;
571   if(nTracks>kTracks)return kFALSE;
572
573   //variables for thrust calculation
574   TVector3 pTrackPerp[kTracks];
575   Double_t psum2 = 0;
576
577   TVector3 psum;
578   TVector3 psum02;
579   TVector3 psum03;
580
581   Double_t psum1 = 0;
582   Double_t psum102 = 0;
583   Double_t psum103 = 0;
584
585   Double_t thrust[kTracks];
586   Double_t th = -3;
587   Double_t thrust02[kTracks];
588   Double_t th02 = -4;
589   Double_t thrust03[kTracks];
590   Double_t th03 = -5;
591
592   //Sphericity calculation variables
593   TMatrixDSym m(3);
594   Double_t s00 = 0;
595   Double_t s01 = 0;
596   Double_t s02 = 0;
597   
598   Double_t s10 = 0;
599   Double_t s11 = 0;
600   Double_t s12 = 0;
601   
602   Double_t s20 = 0;
603   Double_t s21 = 0;
604   Double_t s22 = 0;
605   
606   Double_t ptot = 0;
607   
608   Double_t c = -10;
609
610 //
611 //loop for thrust calculation  
612 //
613     
614   for(Int_t i = 0; i < nTracks; i++)
615     {
616       pTrackPerp[i].SetXYZ(pTrack[i].X(), pTrack[i].Y(), 0);
617       psum2 += pTrackPerp[i].Mag();
618     }
619
620   //additional starting axis    
621   TVector3 n02;
622   n02 = pTrack[1].Unit();
623   n02.SetZ(0.);   
624   TVector3 n03;
625   n03 = pTrack[2].Unit();
626   n03.SetZ(0.);
627
628   //switches for calculating thrust for different starting points
629   Int_t switch1 = 1;
630   Int_t switch2 = 1;
631   Int_t switch3 = 1;
632
633   //indexes for iteration of different starting points
634   Int_t l1 = 0;
635   Int_t l2 = 0;
636   Int_t l3 = 0;
637
638   //maximal number of iterations
639   //  Int_t nMaxIter = 100;
640  
641   for(Int_t k = 0; k < nTracks; k++)
642     {  
643       
644       if(switch1 == 1){
645         psum.SetXYZ(0., 0., 0.);
646         psum1 = 0;
647         for(Int_t i = 0; i < nTracks; i++)
648           {
649             psum1 += (TMath::Abs(n01.Dot(pTrackPerp[i])));
650             if (n01.Dot(pTrackPerp[i]) > 0) psum += pTrackPerp[i];
651             if (n01.Dot(pTrackPerp[i]) < 0) psum -= pTrackPerp[i];
652           }
653         thrust[l1] = psum1/psum2;
654       }
655
656       if(switch2 == 1){
657         psum02.SetXYZ(0., 0., 0.);
658         psum102 = 0;
659         for(Int_t i = 0; i < nTracks; i++)
660           {
661             psum102 += (TMath::Abs(n02.Dot(pTrackPerp[i])));
662             if (n02.Dot(pTrackPerp[i]) > 0) psum02 += pTrackPerp[i];
663             if (n02.Dot(pTrackPerp[i]) < 0) psum02 -= pTrackPerp[i];
664           }
665         thrust02[l2] = psum102/psum2;
666       }
667       
668       if(switch3 == 1){
669         psum03.SetXYZ(0., 0., 0.);
670         psum103 = 0;
671         for(Int_t i = 0; i < nTracks; i++)
672           {
673             psum103 += (TMath::Abs(n03.Dot(pTrackPerp[i])));
674             if (n03.Dot(pTrackPerp[i]) > 0) psum03 += pTrackPerp[i];
675             if (n03.Dot(pTrackPerp[i]) < 0) psum03 -= pTrackPerp[i];
676           }
677         thrust03[l3] = psum103/psum2;
678       }
679
680       //check whether thrust value converged    
681       if(TMath::Abs(th-thrust[l1]) < 10e-7){
682         switch1 = 0;
683       }
684       
685       if(TMath::Abs(th02-thrust02[l2]) < 10e-7){
686         switch2 = 0;
687       }
688
689       if(TMath::Abs(th03-thrust03[l3]) < 10e-7){
690         switch3 = 0;
691       }
692
693       //if it didn't, continue with the calculation
694       if(switch1 == 1){
695         th = thrust[l1]; 
696         n01 = psum.Unit();
697         l1++;
698       }
699
700       if(switch2 == 1){
701         th02 = thrust02[l2];
702         n02 = psum02.Unit();
703         l2++;
704       }
705
706       if(switch3 == 1){
707         th03 = thrust03[l3];
708         n03 = psum03.Unit();
709         l3++;
710       }
711
712       //if thrust values for all starting direction converged check if to the same value
713       if(switch2 == 0 && switch1 == 0 && switch3 == 0){
714         if(TMath::Abs(th-th02) < 10e-7 && TMath::Abs(th-th03) < 10e-7 && TMath::Abs(th02-th03) < 10e-7){
715           eventShapes[0] = th;
716           AliInfoGeneral(Form(" %s:%d",(char*)__FILE__,__LINE__),Form("===== THRUST VALUE FOUND AT %d :: %f\n", k, th));
717           break;
718         }
719         //if they did not, reset switches
720         else{
721           switch1 = 1;
722           //      th = -1.;
723           switch2 = 1;
724           //      th02 = -2.;
725           switch3 = 1;
726           //      th03 = -4.;
727         }
728       }
729
730       //      Printf("========== %d +++ th :: %f=============\n", l1, th);
731       //      Printf("========== %d +++ th2 :: %f=============\n", l2, th02);
732       //      Printf("========== %d +++ th3 :: %f=============\n", l3, th03);
733       
734     }
735
736   //if no common limitng value was found, take the maximum and take the corresponding thrust axis
737   if(switch1 == 1 && switch2 == 1 && switch3 == 1){
738     eventShapes[0] = TMath::Max(thrust[l1-1], thrust02[l2-1]);
739     eventShapes[0] = TMath::Max(eventShapes[0], thrust03[l3-1]);
740     if(TMath::Abs(eventShapes[0]-thrust[l1-1]) < 10e-7)
741       n01 = n01;
742     if(TMath::Abs(eventShapes[0]-thrust02[l2-1]) < 10e-7)
743       n01 = n02;
744     if(TMath::Abs(eventShapes[0]-thrust03[l3-1]) < 10e-7)
745       n01 = n03;
746     Printf("NO LIMITING VALUE FOUND :: MAXIMUM = %f\n", eventShapes[0]);
747   }
748
749 //
750 //other event shapes variables
751 //
752   for(Int_t j = 0; j < nTracks; j++)
753     {  
754       s00 = s00 + (pTrack[j].Px()*pTrack[j].Px())/pTrack[j].Mag();
755       s01 = s01 + (pTrack[j].Px()*pTrack[j].Py())/pTrack[j].Mag();
756       s02 = s02 + (pTrack[j].Px()*pTrack[j].Pz())/pTrack[j].Mag();
757       
758       s10 = s10 + (pTrack[j].Py()*pTrack[j].Px())/pTrack[j].Mag();
759       s11 = s11 + (pTrack[j].Py()*pTrack[j].Py())/pTrack[j].Mag();
760       s12 = s12 + (pTrack[j].Py()*pTrack[j].Pz())/pTrack[j].Mag();
761       
762       s20 = s20 + (pTrack[j].Pz()*pTrack[j].Px())/pTrack[j].Mag();
763       s21 = s21 + (pTrack[j].Pz()*pTrack[j].Py())/pTrack[j].Mag();
764       s22 = s22 + (pTrack[j].Pz()*pTrack[j].Pz())/pTrack[j].Mag();
765       
766       ptot += pTrack[j].Mag();
767     }
768
769   if(ptot > 0.)
770     {
771       m(0,0) = s00/ptot;
772       m(0,1) = s01/ptot;
773       m(0,2) = s02/ptot;
774
775       m(1,0) = s10/ptot;
776       m(1,1) = s11/ptot;
777       m(1,2) = s12/ptot;
778
779       m(2,0) = s20/ptot;
780       m(2,1) = s21/ptot;
781       m(2,2) = s22/ptot;
782
783       TMatrixDSymEigen eigen(m);
784       TVectorD eigenVal = eigen.GetEigenValues();
785
786       Double_t sphericity = (3/2)*(eigenVal(2)+eigenVal(1));
787       eventShapes[1] = sphericity;
788
789       Double_t aplanarity = (3/2)*(eigenVal(2));
790       eventShapes[2] = aplanarity;
791
792       c = 3*(eigenVal(0)*eigenVal(1)+eigenVal(0)*eigenVal(2)+eigenVal(1)*eigenVal(2));
793       eventShapes[3] = c;
794     }
795   return kTRUE;
796 }
797   
798
799
800  //__________________________________________________________________________________________________________________________
801 // Trigger Decisions copid from PWG0/AliTriggerAnalysis
802
803
804 Bool_t AliAnalysisHelperJetTasks::IsTriggerFired(const AliVEvent* aEv, Trigger trigger)
805 {
806   // checks if an event has been triggered
807   // no usage of ofline trigger here yet
808   
809   // here we do a dirty hack to take also into account the
810   // missing trigger bits and Bunch crossing paatern for real data 
811
812
813   if(aEv->InheritsFrom("AliESDEvent")){
814     const AliESDEvent *esd = (AliESDEvent*)aEv;
815     switch (trigger)
816       {
817       case kAcceptAll:
818         {
819           return kTRUE;
820           break;
821         }
822       case kMB1:
823         {
824           if(esd->GetFiredTriggerClasses().Contains("CINT1B-"))return kTRUE;
825           // does the same but without or'ed V0s
826           if(esd->GetFiredTriggerClasses().Contains("CSMBB"))return kTRUE;  
827           if(esd->GetFiredTriggerClasses().Contains("CINT6B-"))return kTRUE; 
828           // this is for simulated data
829           if(esd->GetFiredTriggerClasses().Contains("MB1"))return kTRUE;   
830           break;
831         }
832       case kMB2:
833         {
834           if(esd->GetFiredTriggerClasses().Contains("MB2"))return kTRUE;   
835           break;
836         }
837       case kMB3:
838         {
839           if(esd->GetFiredTriggerClasses().Contains("MB3"))return kTRUE;   
840           break;
841         }
842       case kSPDGFO:
843         {
844           if(esd->GetFiredTriggerClasses().Contains("CSMBB"))return kTRUE;
845           if(esd->GetFiredTriggerClasses().Contains("CINT6B-"))return kTRUE; 
846           if(esd->GetFiredTriggerClasses().Contains("GFO"))return kTRUE;
847           break;
848         }
849       default:
850         {
851           Printf("IsEventTriggered: ERROR: Trigger type %d not implemented in this method", (Int_t) trigger);
852           break;
853         }
854       }
855   }
856   else if(aEv->InheritsFrom("AliAODEvent")){
857     const AliAODEvent *aod = (AliAODEvent*)aEv;
858     switch (trigger)
859       {
860       case kAcceptAll:
861         {
862           return kTRUE;
863           break;
864         }
865       case kMB1:
866         {
867           if(aod->GetFiredTriggerClasses().Contains("CINT1B"))return kTRUE;
868           // does the same but without or'ed V0s
869           if(aod->GetFiredTriggerClasses().Contains("CSMBB"))return kTRUE;   
870           // for sim data
871           if(aod->GetFiredTriggerClasses().Contains("MB1"))return kTRUE;   
872           break;
873         }
874       case kMB2:
875         {
876           if(aod->GetFiredTriggerClasses().Contains("MB2"))return kTRUE;   
877           break;
878         }
879       case kMB3:
880         {
881           if(aod->GetFiredTriggerClasses().Contains("MB3"))return kTRUE;   
882           break;
883         }
884       case kSPDGFO:
885         {
886           if(aod->GetFiredTriggerClasses().Contains("CSMBB"))return kTRUE;        
887           if(aod->GetFiredTriggerClasses().Contains("GFO"))return kTRUE;   
888           break;
889         }
890       default:
891         {
892           Printf("IsEventTriggered: ERROR: Trigger type %d not implemented in this method", (Int_t) trigger);
893           break;
894         }
895       }
896   }
897     return kFALSE;
898 }
899
900
901  AliAnalysisHelperJetTasks::MCProcessType  AliAnalysisHelperJetTasks::GetPythiaEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
902
903   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader);
904
905   if (!pythiaGenHeader) {
906     //    printf(" AliAnalysisHelperJetTasks::GetProcessType : Unknown gen Header type). \n");
907     return kInvalidProcess;
908   }
909
910
911   Int_t pythiaType = pythiaGenHeader->ProcessType();
912   fgLastProcessType = pythiaType;
913   MCProcessType globalType = kInvalidProcess;  
914
915
916   if (adebug) {
917     printf(" AliAnalysisHelperJetTasks::GetProcessType : Pythia process type found: %d \n",pythiaType);
918   }
919
920
921   if(pythiaType==92||pythiaType==93){
922     globalType = kSD;
923   }
924   else if(pythiaType==94){
925     globalType = kDD;
926   }
927   //else if(pythiaType != 91){ // also exclude elastic to be sure... CKB??
928   else {
929     globalType = kND;
930   }
931   return globalType;
932 }
933
934
935  AliAnalysisHelperJetTasks::MCProcessType  AliAnalysisHelperJetTasks::GetDPMjetEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
936   //
937   // get the process type of the event.
938   //
939
940   // can only read pythia headers, either directly or from cocktalil header
941   AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader);
942
943   if (!dpmJetGenHeader) {
944     printf(" AliAnalysisHelperJetTasks::GetDPMjetProcessType : Unknown header type (not DPMjet or). \n");
945     return kInvalidProcess;
946   }
947
948   Int_t dpmJetType = dpmJetGenHeader->ProcessType();
949   fgLastProcessType = dpmJetType;
950   MCProcessType globalType = kInvalidProcess;  
951
952
953   if (adebug) {
954     printf(" AliAnalysisHelperJetTasks::GetDPMJetProcessType : DPMJet process type found: %d \n",dpmJetType);
955   }
956
957
958   if (dpmJetType == 1 || dpmJetType == 4) { // explicitly inelastic plus central diffraction
959     globalType = kND;
960   }  
961   else if (dpmJetType==5 || dpmJetType==6) {
962     globalType = kSD;
963   }
964   else if (dpmJetType==7) {
965     globalType = kDD;
966   }
967   return globalType;
968 }
969