]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/JET/AliHLTJETReader.cxx
* Added fast interface to fastjet
[u/mrichter/AliRoot.git] / HLT / JET / AliHLTJETReader.cxx
1 //-*- Mode: C++ -*-
2 // $Id: AliHLTJETReaderHeader.cxx  $
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
8  *                  for The ALICE HLT Project.                            *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTJETReader.cxx
20     @author Jochen Thaeder
21     @date   
22     @brief  Reader for jet finder
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt   
30
31 #if __GNUC__ >= 3
32 using namespace std;
33 #endif
34
35 #include "TLorentzVector.h"
36 #include "TParticle.h"
37 #include "TParticlePDG.h"
38 #include "TDatabasePDG.h"
39
40 #include "AliHLTJETReader.h"
41
42 #include "AliHLTJETConeJetCandidate.h"
43
44 /** ROOT macro for the implementation of ROOT specific class methods */
45 ClassImp(AliHLTJETReader)
46
47 /*
48  * ---------------------------------------------------------------------------------
49  *                            Constructor / Destructor
50  * ---------------------------------------------------------------------------------
51  */
52   
53 // #################################################################################
54 AliHLTJETReader::AliHLTJETReader()
55   : 
56   AliJetReader(),
57   fESD(NULL), 
58   fMC(NULL),
59   fHLTMC(NULL),
60   fAOD(NULL),
61 #ifdef HAVE_FASTJET
62   fMomentumVector(NULL),
63 #endif
64   fGrid(NULL),
65   fNJetCandidates(0),
66   fJetCandidates(NULL),
67   fSeedCuts(NULL),
68   fTrackCuts(NULL) {
69   // see header file for class documentation
70   // or
71   // refer to README to build package
72   // or
73   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
74
75 }
76
77 // #################################################################################
78 AliHLTJETReader::~AliHLTJETReader() {
79   // see header file for class documentation
80
81 #ifdef HAVE_FASTJET
82   if ( fMomentumVector )
83     delete fMomentumVector;
84   fMomentumVector = NULL;
85 #endif
86
87   if ( fGrid )
88     delete fGrid;
89   fGrid = NULL;
90
91   if ( fJetCandidates ) {
92     fJetCandidates->Clear();
93     delete fJetCandidates;
94   }
95   fJetCandidates = NULL;
96
97 }
98
99 // #################################################################################
100 Int_t AliHLTJETReader::Initialize() {
101   // see header file for class documentation
102
103   Int_t iResult = 0;
104   AliHLTJETReaderHeader* readerHeader = NULL;
105
106   HLTInfo(" -= AliHLTJETReader =- " );
107
108   // -- Initialize reader header
109   // -----------------------------
110   if ( fReaderHeader ) {
111     readerHeader = GetReaderHeader();
112
113     iResult = readerHeader->Initialize();
114     if ( iResult )
115       HLTError("Error initializing HLT jet reader header");
116   }
117   else {
118     HLTError("Reader Header not present");
119     iResult = -EINPROGRESS;
120   }
121   
122   // -- Initialize Algorithms
123   // --------------------------
124   if ( readerHeader->GetJetAlgorithm() >= AliHLTJETBase::kFFSCSquareCell )
125     iResult = InitializeFFSC();
126   else {
127 #ifdef HAVE_FASTJET
128     iResult = InitializeFastjet();
129 #else  
130     HLTError("Error FastJet not present.");
131     iResult = -EINPROGRESS;
132 #endif
133   }
134  
135   // -- Get ptr to cuts from reader
136   // --------------------------------
137  
138   // -- Seed cuts
139   if ( ! iResult ) {
140     fSeedCuts = readerHeader->GetSeedCuts();
141     if ( ! fSeedCuts ) {
142       HLTError("Error getting ptr to seed cuts.");
143       iResult = -EINPROGRESS;
144     }
145   }
146
147   // -- Track cuts
148   if ( ! iResult ) {
149     fTrackCuts = readerHeader->GetTrackCuts();
150     if ( ! fTrackCuts ) {
151       HLTError("Error getting ptr to track cuts.");
152       iResult = -EINPROGRESS;
153     }
154   }
155
156   // -- Final check
157   // ----------------
158   if ( iResult )
159     HLTError("Error initializing HLT jet reader");
160  
161   return iResult;
162 }
163
164 //#################################################################################
165 void AliHLTJETReader::ResetEvent() {
166   // see header file for class documentation
167
168   // -- Reset FFSC algorithms
169   // --------------------------
170   if (  GetReaderHeader()->GetJetAlgorithm() >= AliHLTJETBase::kFFSCSquareCell ) {
171     // -- clear grid
172     fGrid->Reset();
173     
174     // -- clear jet candidates
175     fJetCandidates->Clear();
176     
177     fNJetCandidates = 0;
178   }
179
180   // -- Reset for FastJet algorithms
181   // ---------------------------------
182   else {
183     // -- Clear input vector
184     if ( fMomentumVector )
185       fMomentumVector->clear();
186   }
187
188   return;  
189 }
190
191 /*
192  * ---------------------------------------------------------------------------------
193  *                                     Setter
194  * ---------------------------------------------------------------------------------
195  */
196
197 // #################################################################################
198 void AliHLTJETReader::SetInputEvent(const TObject* esd, const TObject* aod, const TObject* mc) {
199   // see header file for class documentation
200
201   //  Needs "useMC" flag for running in analysis task only
202
203   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
204
205   // -- Fill ESD
206   if ( esd && !readerHeader->GetUseMC() )
207     fESD = dynamic_cast<AliESDEvent*> (const_cast<TObject*>(esd));
208
209   // -- Fill AOD
210   else if ( aod && !readerHeader->GetUseMC() )
211     fAOD = dynamic_cast<AliAODEvent*> (const_cast<TObject*>(aod));
212
213   // -- Fill MC
214   else if ( mc && readerHeader->GetUseMC() ) {
215
216     // -- if off-line MC event, 
217     if ( !strcmp (mc->ClassName(),"AliMCEvent") )
218       fMC = dynamic_cast<AliMCEvent*> (const_cast<TObject*>(mc));
219     // -- if on-line MC event
220     else 
221       fHLTMC = dynamic_cast<AliHLTMCEvent*> (const_cast<TObject*>(mc));
222   }
223   
224   return;
225 }
226
227 /*
228  * ---------------------------------------------------------------------------------
229  *                            Fastjet Reader functionality
230  * ---------------------------------------------------------------------------------
231  */
232 #ifdef HAVE_FASTJET
233 // #################################################################################
234 Bool_t AliHLTJETReader::FillVector() {
235   // see header file for class documentation
236
237   Bool_t bResult = kFALSE;
238
239   if ( fESD )
240     bResult = FillVectorESD();
241   else if ( fMC )
242     bResult = FillVectorMC();
243   else if ( fHLTMC )
244     bResult = FillVectorHLTMC();
245   else if ( fAOD )
246     bResult = FillVectorAOD();
247   
248   return bResult;
249 }
250
251 // #################################################################################
252 Bool_t AliHLTJETReader::FillVectorMC() {
253   // see header file for class documentation
254
255   Bool_t bResult = kTRUE; 
256
257   if ( ! fMC ) {
258     HLTError( "No MC Event present!" );
259     return kFALSE;
260   }
261   
262   // -- Reset Event
263   ResetEvent();
264    
265   Int_t nTracks = 0;
266
267   AliStack* stack = fMC->Stack();
268
269   for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && bResult; iterStack++) {
270
271     TParticle *particle = stack->Particle(iterStack);
272     if ( !particle) {
273       HLTError( "Error reading particle %i out of %i", iterStack, stack->GetNtrack() );
274       bResult = kFALSE;
275       continue;
276     }
277
278     // ------------------------------
279     // -- Basic cuts on MC particle      --> To be done better XXX
280     // ------------------------------
281
282     // -- primary
283     if ( !(stack->IsPhysicalPrimary(iterStack)) )
284       continue;
285     
286     // -- final state
287     if ( particle->GetNDaughters() != 0 )
288       continue;
289
290     // -- particle in DB
291     TParticlePDG * particlePDG = particle->GetPDG();
292     if ( ! particlePDG ) {
293       particlePDG = TDatabasePDG::Instance()->GetParticle( particle->GetPdgCode() );
294
295       if ( ! particlePDG ) {
296         HLTError("Particle %i not in PDG database", particle->GetPdgCode() );
297         bResult = kFALSE;
298         continue;
299       }
300     }
301
302     // ------------------------
303     // -- Standard track cuts
304     // ------------------------
305
306     // -- Apply track cuts     
307     if ( ! fTrackCuts->IsSelected(particle) )
308       continue;
309     
310     // -- Create PseudoJet object      
311     fastjet::PseudoJet part( particle->Px(), particle->Py(), 
312                              particle->Pz(), particle->Energy() ); 
313     
314     // -- label the particle into Fastjet algortihm
315     part.set_user_index( iterStack ); 
316
317     // -- Add to input_particles vector  
318     fMomentumVector->push_back(part);  
319
320     nTracks++;    
321   } // for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && !bResult; iterStack++) {
322
323   HLTDebug(" Number of selected tracks %d", nTracks);
324
325   return kTRUE;
326 }
327
328 // #################################################################################
329 Bool_t AliHLTJETReader::FillVectorHLTMC() {
330   // see header file for class documentation
331
332   if ( ! fHLTMC ) {
333     HLTError( "No HLT MC Event present!" );
334     return kFALSE;
335   }
336      
337   // -- Reset Event
338   ResetEvent();
339
340   Int_t nTracks = 0;
341
342   TParticle* particle = NULL;
343
344   // -- Loop over particles
345   // ------------------------
346   while ( (particle = fHLTMC->NextParticle() ) ) {
347     
348     // -- Apply track cuts 
349     if ( ! fTrackCuts->IsSelected(particle) )
350       continue;
351     
352     // -- Create PseudoJet object      
353     fastjet::PseudoJet part( particle->Px(), particle->Py(), 
354                              particle->Pz(), particle->Energy() ); 
355     
356     // -- label the particle into Fastjet algortihm
357     part.set_user_index( fHLTMC->GetIndex() ); 
358
359     // -- Add to input_particles vector  
360     fMomentumVector->push_back(part);  
361
362     nTracks++;    
363     
364   } // while ( (particle = fHLTMC->NextParticle() ) ) {
365   
366   HLTInfo(" Number of selected tracks %d", nTracks);
367
368   return kTRUE;
369 }
370
371 // #################################################################################
372 Bool_t AliHLTJETReader::FillVectorESD() {
373   // see header file for class documentation
374
375   Bool_t bResult = kTRUE;
376
377   if ( ! fESD ) {
378     HLTError( "No ESD Event present!" );
379     return kFALSE;
380   }
381
382   // -- Reset Event
383   ResetEvent();
384
385   Int_t nTracks = 0;
386
387   // -- Loop over particles
388   // ------------------------
389   for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && bResult; iter++ ) {
390
391     AliESDtrack* esdTrack = fESD->GetTrack(iter);
392     if ( ! esdTrack ) {
393       HLTError("Could not read ESD track %d from %d", iter, fESD->GetNumberOfTracks() );
394       bResult = kFALSE;
395       continue;
396     }
397
398     // -- Apply track cuts 
399     if ( ! fTrackCuts->IsSelected(esdTrack) )
400       continue;
401
402     // -- Create PseudoJet object      
403     fastjet::PseudoJet part( esdTrack->Px(), esdTrack->Py(), 
404                              esdTrack->Pz(), esdTrack->E() ); 
405     
406     // -- label the particle into Fastjet algortihm
407     part.set_user_index( iter ); 
408
409     // -- Add to input_particles vector  
410     fMomentumVector->push_back(part);  
411
412     nTracks++; 
413
414   } // for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !iResult; iter++ ) {
415   
416   HLTInfo(" Number of selected tracks %d", nTracks);
417
418
419   return bResult;
420 }
421
422 // #################################################################################
423 Bool_t AliHLTJETReader::FillVectorAOD() {
424   // see header file for class documentation
425
426   return kFALSE;
427 }
428 #endif
429
430 /*
431  * ---------------------------------------------------------------------------------
432  *                               Grid functionality
433  * ---------------------------------------------------------------------------------
434  */
435
436 // #################################################################################
437 Bool_t AliHLTJETReader::FillGrid() {
438   // see header file for class documentation
439
440   Bool_t bResult = kFALSE;
441
442   if ( fESD )
443     bResult = FillGridESD();
444   else if ( fMC )
445     bResult = FillGridMC();
446   else if ( fHLTMC )
447     bResult = FillGridHLTMC();
448   else if ( fAOD )
449     bResult = FillGridAOD();
450
451   return bResult;
452 }
453
454 // #################################################################################
455 Bool_t AliHLTJETReader::FillGridMC() {
456   // see header file for class documentation
457
458   Bool_t bResult = kTRUE; 
459
460   if ( ! fMC ) {
461     HLTError( "No MC Event present!" );
462     return kFALSE;
463   }
464
465   // -- Reset Event
466   ResetEvent();
467
468   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
469
470   Int_t nTracks = 0;
471
472   AliStack* stack = fMC->Stack();
473
474   for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && bResult; iterStack++) {
475
476     TParticle *particle = stack->Particle(iterStack);
477     if ( !particle) {
478       HLTError( "Error reading particle %i out of %i", iterStack, stack->GetNtrack() );
479       bResult = kFALSE;
480       continue;
481     }
482
483     // ------------------------------
484     // -- Basic cuts on MC particle      --> To be done better XXX
485     // ------------------------------
486
487     // -- primary
488     if ( !(stack->IsPhysicalPrimary(iterStack)) )
489       continue;
490
491     // -- final state
492     if ( particle->GetNDaughters() != 0 )
493       continue;
494
495     // -- particle in DB
496     TParticlePDG * particlePDG = particle->GetPDG();
497     if ( ! particlePDG ) {
498       particlePDG = TDatabasePDG::Instance()->GetParticle( particle->GetPdgCode() );
499
500       if ( ! particlePDG ) {
501         HLTError("Particle %i not in PDG database", particle->GetPdgCode() );
502         bResult = kFALSE;
503         continue;
504       }
505     }
506
507     // ------------------------
508     // -- Standard track cuts
509     // ------------------------
510
511     // -- Apply track cuts     
512     if ( ! fTrackCuts->IsSelected(particle) )
513       continue;
514
515     const Float_t aEtaPhi[]  = { particle->Eta(), particle->Phi(), particle->Pt() }; 
516           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
517
518     fGrid->FillTrack(particle, aEtaPhi, aGridIdx);
519           
520     nTracks++;    
521
522     // -- Apply seed cuts 
523     if ( ! fSeedCuts->IsSelected(particle) )
524       continue;  
525
526     // -- Add Seed
527     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx), readerHeader->GetConeRadius());
528     
529   } // for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && !bResult; iterStack++) {
530
531   HLTInfo(" Number of selected tracks %d", nTracks);
532   HLTInfo(" Number of seeds %d", fNJetCandidates);
533
534   return kTRUE;
535 }
536
537 // #################################################################################
538 Bool_t AliHLTJETReader::FillGridHLTMC() {
539   // see header file for class documentation
540
541   if ( ! fHLTMC ) {
542     HLTError( "No HLT MC Event present!" );
543     return kFALSE;
544   }
545
546   // -- Reset Event
547   ResetEvent();
548
549   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
550
551   Int_t nTracks = 0;
552   TParticle* particle = NULL;
553
554   // -- Loop over particles
555   // ------------------------
556   while ( ( particle = fHLTMC->NextParticle() ) ) {
557     
558     //    HLTError("=== nTracks %d ===",nTracks);
559
560     // -- Apply track cuts 
561     if ( ! fTrackCuts->IsSelected(particle) )
562       continue;
563
564     const Float_t aEtaPhi[]  = { particle->Eta(), particle->Phi(), particle->Pt() }; 
565           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
566
567     // -- Fill grid
568     fGrid->FillTrack(particle, aEtaPhi, aGridIdx);
569
570     nTracks++;    
571
572     // -- Apply seed cuts 
573     if ( ! fSeedCuts->IsSelected(particle) )
574       continue;  
575
576     // -- Add Seed
577     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx),
578             readerHeader->GetConeRadius());
579   
580   } // while ( (particle = fHLTMC->NextParticle() ) ) {
581   
582   HLTInfo(" Number of selected tracks %d", nTracks);
583   HLTInfo(" Number of seeds %d", fNJetCandidates);
584
585   return kTRUE;
586 }
587
588 // #################################################################################
589 Bool_t AliHLTJETReader::FillGridESD() {
590   // see header file for class documentation
591
592   Bool_t bResult = kTRUE;
593
594   if ( ! fESD ) {
595     HLTError( "No ESD Event present!" );
596     return kFALSE;
597   }
598
599   // -- Reset Event
600   ResetEvent();
601
602   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
603
604   Int_t nTracks = 0;
605
606   // -- Loop over particles
607   // ------------------------
608   for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !bResult; iter++ ) {
609
610     AliESDtrack* esdTrack = fESD->GetTrack(iter);
611     if ( ! esdTrack ) {
612       HLTError("Could not read ESD track %d from %d", iter, fESD->GetNumberOfTracks() );
613       bResult = kFALSE;
614       continue;
615     }
616
617     // -- Apply track cuts 
618     if ( ! fTrackCuts->IsSelected(esdTrack) )
619       continue;
620
621     const Float_t aEtaPhi[]  = { esdTrack->Eta(), esdTrack->Phi(), esdTrack->Pt() }; 
622           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
623
624     // -- Fill grid
625     fGrid->FillTrack(esdTrack, aEtaPhi, aGridIdx);
626
627     nTracks++;    
628
629     // -- Apply seed cuts 
630     if ( ! fSeedCuts->IsSelected(esdTrack) )
631       continue;
632
633     // -- Add Seed
634     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx),
635             readerHeader->GetConeRadius());
636   
637   } // for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !iResult; iter++ ) {
638   
639   HLTInfo(" Number of selected tracks %d", nTracks);
640   HLTInfo(" Number of seeds %d", fNJetCandidates);
641
642   return bResult;
643 }
644
645 // #################################################################################
646 Bool_t AliHLTJETReader::FillGridAOD() {
647   // see header file for class documentation
648
649   return kFALSE;
650 }
651
652 /*
653  * ---------------------------------------------------------------------------------
654  *                                     Seeds
655  * ---------------------------------------------------------------------------------
656  */
657
658 //#################################################################################
659 void AliHLTJETReader::AddSeed( const Float_t* aEtaPhi, const Int_t* aGridIdx, 
660                                const Float_t coneRadius ) {
661   // see header file for class documentation
662
663   Bool_t useWholeCell = kTRUE;
664
665   if ( GetReaderHeader()->GetJetAlgorithm() == AliHLTJETBase::kFFSCRadiusCell )
666     useWholeCell = kFALSE ;
667
668   // -- Add track / particle
669   new( (*fJetCandidates) [fNJetCandidates] ) AliHLTJETConeJetCandidate( aEtaPhi, 
670                                                                         aGridIdx,
671                                                                         coneRadius,
672                                                                         useWholeCell );
673   fNJetCandidates++;
674
675   HLTDebug("Added Seed Pt=%f, Eta=%f, Phi=%f", aEtaPhi[kIdxPt], 
676            aEtaPhi[kIdxEta], aEtaPhi[kIdxPhi] );
677
678   return;
679 }
680
681 /*
682  * ---------------------------------------------------------------------------------
683  *                         Initialize - private
684  * ---------------------------------------------------------------------------------
685  */
686
687 // #################################################################################
688 Int_t AliHLTJETReader::InitializeFFSC() {
689   // see header file for class documentation
690
691   Int_t iResult = 0;
692   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
693
694   // -- Initialize grid
695   // --------------------
696   if ( fGrid )
697     delete fGrid;
698   
699   if ( ! (fGrid = new AliHLTJETConeGrid()) ) {
700     HLTError("Error instanciating grid.");
701     iResult = -EINPROGRESS;
702   }
703   
704   if ( ! iResult ) {
705     fGrid->SetEtaRange(   readerHeader->GetFiducialEtaMin(),
706                           readerHeader->GetFiducialEtaMax(),
707                           readerHeader->GetGridEtaRange() );
708     
709     fGrid->SetPhiRange(   readerHeader->GetFiducialPhiMin(),
710                           readerHeader->GetFiducialPhiMax(),
711                           readerHeader->GetGridPhiRange() );
712     
713     fGrid->SetBinning(    readerHeader->GetGridEtaBinning(),
714                           readerHeader->GetGridEtaBinning() );
715     
716     fGrid->SetConeRadius( readerHeader->GetConeRadius() );
717     
718     iResult = fGrid->Initialize();
719   }
720   
721   // -- Initialize jet candidates
722   // ------------------------------
723   if ( ! iResult ) {
724     fJetCandidates = new TClonesArray("AliHLTJETConeJetCandidate", 30);
725     if ( ! fJetCandidates) {
726       HLTError("Error instanciating jet candidates.");
727       iResult = -EINPROGRESS;
728     }
729   }
730  
731   return iResult;
732 }
733
734 #ifdef HAVE_FASTJET
735 // #################################################################################
736 Int_t AliHLTJETReader::InitializeFastjet() {
737   // see header file for class documentation
738
739   Int_t iResult = 0;
740
741   // -- Initialize Vector
742   // ----------------------
743   if ( fMomentumVector )
744     delete fMomentumVector;
745   
746   if ( ! (fMomentumVector = new vector<fastjet::PseudoJet>) ) {
747     HLTError("Error instanciating momentum vector.");
748     iResult = -EINPROGRESS;
749   }
750  
751   return iResult;
752 }
753 #endif