]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/JET/AliHLTJETReader.cxx
adding more default configurations
[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 #ifdef HAVE_FASTJET
184     // -- Clear input vector
185     if ( fMomentumVector )
186       fMomentumVector->clear();
187 #endif
188   }
189
190   return;  
191 }
192
193 /*
194  * ---------------------------------------------------------------------------------
195  *                                     Setter
196  * ---------------------------------------------------------------------------------
197  */
198
199 // #################################################################################
200 void AliHLTJETReader::SetInputEvent(const TObject* esd, const TObject* aod, const TObject* mc) {
201   // see header file for class documentation
202
203   //  Needs "useMC" flag for running in analysis task only
204
205   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
206
207   // -- Fill ESD
208   if ( esd && !readerHeader->GetUseMC() )
209     fESD = dynamic_cast<AliESDEvent*> (const_cast<TObject*>(esd));
210
211   // -- Fill AOD
212   else if ( aod && !readerHeader->GetUseMC() )
213     fAOD = dynamic_cast<AliAODEvent*> (const_cast<TObject*>(aod));
214
215   // -- Fill MC
216   else if ( mc && readerHeader->GetUseMC() ) {
217
218     // -- if off-line MC event, 
219     if ( !strcmp (mc->ClassName(),"AliMCEvent") )
220       fMC = dynamic_cast<AliMCEvent*> (const_cast<TObject*>(mc));
221     // -- if on-line MC event
222     else 
223       fHLTMC = dynamic_cast<AliHLTMCEvent*> (const_cast<TObject*>(mc));
224   }
225   
226   return;
227 }
228
229 /*
230  * ---------------------------------------------------------------------------------
231  *                            Fastjet Reader functionality
232  * ---------------------------------------------------------------------------------
233  */
234 #ifdef HAVE_FASTJET
235 // #################################################################################
236 Bool_t AliHLTJETReader::FillVector() {
237   // see header file for class documentation
238
239   Bool_t bResult = kFALSE;
240
241   if ( fESD )
242     bResult = FillVectorESD();
243   else if ( fMC )
244     bResult = FillVectorMC();
245   else if ( fHLTMC )
246     bResult = FillVectorHLTMC();
247   else if ( fAOD )
248     bResult = FillVectorAOD();
249   
250   return bResult;
251 }
252
253 // #################################################################################
254 Bool_t AliHLTJETReader::FillVectorMC() {
255   // see header file for class documentation
256
257   Bool_t bResult = kTRUE; 
258
259   if ( ! fMC ) {
260     HLTError( "No MC Event present!" );
261     return kFALSE;
262   }
263   
264   // -- Reset Event
265   ResetEvent();
266    
267   Int_t nTracks = 0;
268
269   AliStack* stack = fMC->Stack();
270
271   for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && bResult; iterStack++) {
272
273     TParticle *particle = stack->Particle(iterStack);
274     if ( !particle) {
275       HLTError( "Error reading particle %i out of %i", iterStack, stack->GetNtrack() );
276       bResult = kFALSE;
277       continue;
278     }
279
280     // ------------------------------
281     // -- Basic cuts on MC particle      --> To be done better XXX
282     // ------------------------------
283
284     // -- primary
285     if ( !(stack->IsPhysicalPrimary(iterStack)) )
286       continue;
287     
288     // -- final state
289     if ( particle->GetNDaughters() != 0 )
290       continue;
291
292     // -- particle in DB
293     TParticlePDG * particlePDG = particle->GetPDG();
294     if ( ! particlePDG ) {
295       particlePDG = TDatabasePDG::Instance()->GetParticle( particle->GetPdgCode() );
296
297       if ( ! particlePDG ) {
298         HLTError("Particle %i not in PDG database", particle->GetPdgCode() );
299         bResult = kFALSE;
300         continue;
301       }
302     }
303
304     // ------------------------
305     // -- Standard track cuts
306     // ------------------------
307
308     // -- Apply track cuts     
309     if ( ! fTrackCuts->IsSelected(particle) )
310       continue;
311     
312     // -- Create PseudoJet object      
313     fastjet::PseudoJet part( particle->Px(), particle->Py(), 
314                              particle->Pz(), particle->Energy() ); 
315     
316     // -- label the particle into Fastjet algortihm
317     part.set_user_index( iterStack ); 
318
319 #ifdef HAVE_FASTJET
320     // -- Add to input_particles vector  
321     fMomentumVector->push_back(part);  
322 #endif
323
324     nTracks++;    
325   } // for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && !bResult; iterStack++) {
326
327   HLTDebug(" Number of selected tracks %d", nTracks);
328
329   return kTRUE;
330 }
331
332 // #################################################################################
333 Bool_t AliHLTJETReader::FillVectorHLTMC() {
334   // see header file for class documentation
335
336   if ( ! fHLTMC ) {
337     HLTError( "No HLT MC Event present!" );
338     return kFALSE;
339   }
340      
341   // -- Reset Event
342   ResetEvent();
343
344   Int_t nTracks = 0;
345
346   TParticle* particle = NULL;
347
348   // -- Loop over particles
349   // ------------------------
350   while ( (particle = fHLTMC->NextParticle() ) ) {
351     
352     // -- Apply track cuts 
353     if ( ! fTrackCuts->IsSelected(particle) )
354       continue;
355     
356     // -- Create PseudoJet object      
357     fastjet::PseudoJet part( particle->Px(), particle->Py(), 
358                              particle->Pz(), particle->Energy() ); 
359     
360     // -- label the particle into Fastjet algortihm
361     part.set_user_index( fHLTMC->GetIndex() ); 
362
363 #ifdef HAVE_FASTJET
364     // -- Add to input_particles vector  
365     fMomentumVector->push_back(part);  
366 #endif
367
368     nTracks++;    
369     
370   } // while ( (particle = fHLTMC->NextParticle() ) ) {
371   
372   HLTInfo(" Number of selected tracks %d", nTracks);
373
374   return kTRUE;
375 }
376
377 // #################################################################################
378 Bool_t AliHLTJETReader::FillVectorESD() {
379   // see header file for class documentation
380
381   Bool_t bResult = kTRUE;
382
383   if ( ! fESD ) {
384     HLTError( "No ESD Event present!" );
385     return kFALSE;
386   }
387
388   // -- Reset Event
389   ResetEvent();
390
391   Int_t nTracks = 0;
392
393   // -- Loop over particles
394   // ------------------------
395   for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && bResult; iter++ ) {
396
397     AliESDtrack* esdTrack = fESD->GetTrack(iter);
398     if ( ! esdTrack ) {
399       HLTError("Could not read ESD track %d from %d", iter, fESD->GetNumberOfTracks() );
400       bResult = kFALSE;
401       continue;
402     }
403
404     // -- Apply track cuts 
405     if ( ! fTrackCuts->IsSelected(esdTrack) )
406       continue;
407
408     // -- Create PseudoJet object      
409     fastjet::PseudoJet part( esdTrack->Px(), esdTrack->Py(), 
410                              esdTrack->Pz(), esdTrack->E() ); 
411     
412     // -- label the particle into Fastjet algortihm
413     part.set_user_index( iter ); 
414
415 #ifdef HAVE_FASTJET
416     // -- Add to input_particles vector  
417     fMomentumVector->push_back(part);  
418 #endif
419
420     nTracks++; 
421
422   } // for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !iResult; iter++ ) {
423   
424   HLTInfo(" Number of selected tracks %d", nTracks);
425
426
427   return bResult;
428 }
429
430 // #################################################################################
431 Bool_t AliHLTJETReader::FillVectorAOD() {
432   // see header file for class documentation
433
434   return kFALSE;
435 }
436 #endif
437
438 /*
439  * ---------------------------------------------------------------------------------
440  *                               Grid functionality
441  * ---------------------------------------------------------------------------------
442  */
443
444 // #################################################################################
445 Bool_t AliHLTJETReader::FillGrid() {
446   // see header file for class documentation
447
448   Bool_t bResult = kFALSE;
449
450   if ( fESD )
451     bResult = FillGridESD();
452   else if ( fMC )
453     bResult = FillGridMC();
454   else if ( fHLTMC )
455     bResult = FillGridHLTMC();
456   else if ( fAOD )
457     bResult = FillGridAOD();
458
459   return bResult;
460 }
461
462 // #################################################################################
463 Bool_t AliHLTJETReader::FillGridMC() {
464   // see header file for class documentation
465
466   Bool_t bResult = kTRUE; 
467
468   if ( ! fMC ) {
469     HLTError( "No MC Event present!" );
470     return kFALSE;
471   }
472
473   // -- Reset Event
474   ResetEvent();
475
476   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
477
478   Int_t nTracks = 0;
479
480   AliStack* stack = fMC->Stack();
481
482   for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && bResult; iterStack++) {
483
484     TParticle *particle = stack->Particle(iterStack);
485     if ( !particle) {
486       HLTError( "Error reading particle %i out of %i", iterStack, stack->GetNtrack() );
487       bResult = kFALSE;
488       continue;
489     }
490
491     // ------------------------------
492     // -- Basic cuts on MC particle      --> To be done better XXX
493     // ------------------------------
494
495     // -- primary
496     if ( !(stack->IsPhysicalPrimary(iterStack)) )
497       continue;
498
499     // -- final state
500     if ( particle->GetNDaughters() != 0 )
501       continue;
502
503     // -- particle in DB
504     TParticlePDG * particlePDG = particle->GetPDG();
505     if ( ! particlePDG ) {
506       particlePDG = TDatabasePDG::Instance()->GetParticle( particle->GetPdgCode() );
507
508       if ( ! particlePDG ) {
509         HLTError("Particle %i not in PDG database", particle->GetPdgCode() );
510         bResult = kFALSE;
511         continue;
512       }
513     }
514
515     // ------------------------
516     // -- Standard track cuts
517     // ------------------------
518
519     // -- Apply track cuts     
520     if ( ! fTrackCuts->IsSelected(particle) )
521       continue;
522
523     const Float_t aEtaPhi[]  = { particle->Eta(), particle->Phi(), particle->Pt() }; 
524           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
525
526     fGrid->FillTrack(particle, aEtaPhi, aGridIdx);
527           
528     nTracks++;    
529
530     // -- Apply seed cuts 
531     if ( ! fSeedCuts->IsSelected(particle) )
532       continue;  
533
534     // -- Add Seed
535     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx), readerHeader->GetConeRadius());
536     
537   } // for (Int_t iterStack = 0; iterStack < stack->GetNtrack() && !bResult; iterStack++) {
538
539   HLTInfo(" Number of selected tracks %d", nTracks);
540   HLTInfo(" Number of seeds %d", fNJetCandidates);
541
542   return kTRUE;
543 }
544
545 // #################################################################################
546 Bool_t AliHLTJETReader::FillGridHLTMC() {
547   // see header file for class documentation
548
549   if ( ! fHLTMC ) {
550     HLTError( "No HLT MC Event present!" );
551     return kFALSE;
552   }
553
554   // -- Reset Event
555   ResetEvent();
556
557   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
558
559   Int_t nTracks = 0;
560   TParticle* particle = NULL;
561
562   // -- Loop over particles
563   // ------------------------
564   while ( ( particle = fHLTMC->NextParticle() ) ) {
565     
566     //    HLTError("=== nTracks %d ===",nTracks);
567
568     // -- Apply track cuts 
569     if ( ! fTrackCuts->IsSelected(particle) )
570       continue;
571
572     const Float_t aEtaPhi[]  = { particle->Eta(), particle->Phi(), particle->Pt() }; 
573           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
574
575     // -- Fill grid
576     fGrid->FillTrack(particle, aEtaPhi, aGridIdx);
577
578     nTracks++;    
579
580     // -- Apply seed cuts 
581     if ( ! fSeedCuts->IsSelected(particle) )
582       continue;  
583
584     // -- Add Seed
585     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx),
586             readerHeader->GetConeRadius());
587   
588   } // while ( (particle = fHLTMC->NextParticle() ) ) {
589   
590   HLTInfo(" Number of selected tracks %d", nTracks);
591   HLTInfo(" Number of seeds %d", fNJetCandidates);
592
593   return kTRUE;
594 }
595
596 // #################################################################################
597 Bool_t AliHLTJETReader::FillGridESD() {
598   // see header file for class documentation
599
600   Bool_t bResult = kTRUE;
601
602   if ( ! fESD ) {
603     HLTError( "No ESD Event present!" );
604     return kFALSE;
605   }
606
607   // -- Reset Event
608   ResetEvent();
609
610   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
611
612   Int_t nTracks = 0;
613
614   // -- Loop over particles
615   // ------------------------
616   for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !bResult; iter++ ) {
617
618     AliESDtrack* esdTrack = fESD->GetTrack(iter);
619     if ( ! esdTrack ) {
620       HLTError("Could not read ESD track %d from %d", iter, fESD->GetNumberOfTracks() );
621       bResult = kFALSE;
622       continue;
623     }
624
625     // -- Apply track cuts 
626     if ( ! fTrackCuts->IsSelected(esdTrack) )
627       continue;
628
629     const Float_t aEtaPhi[]  = { esdTrack->Eta(), esdTrack->Phi(), esdTrack->Pt() }; 
630           Int_t   aGridIdx[] = { -1, -1, -1, -1, -1 };
631
632     // -- Fill grid
633     fGrid->FillTrack(esdTrack, aEtaPhi, aGridIdx);
634
635     nTracks++;    
636
637     // -- Apply seed cuts 
638     if ( ! fSeedCuts->IsSelected(esdTrack) )
639       continue;
640
641     // -- Add Seed
642     AddSeed(aEtaPhi, const_cast<const Int_t*> (aGridIdx),
643             readerHeader->GetConeRadius());
644   
645   } // for ( Int_t iter = 0; iter < fESD->GetNumberOfTracks() && !iResult; iter++ ) {
646   
647   HLTInfo(" Number of selected tracks %d", nTracks);
648   HLTInfo(" Number of seeds %d", fNJetCandidates);
649
650   return bResult;
651 }
652
653 // #################################################################################
654 Bool_t AliHLTJETReader::FillGridAOD() {
655   // see header file for class documentation
656
657   return kFALSE;
658 }
659
660 /*
661  * ---------------------------------------------------------------------------------
662  *                                     Seeds
663  * ---------------------------------------------------------------------------------
664  */
665
666 //#################################################################################
667 void AliHLTJETReader::AddSeed( const Float_t* aEtaPhi, const Int_t* aGridIdx, 
668                                const Float_t coneRadius ) {
669   // see header file for class documentation
670
671   Bool_t useWholeCell = kTRUE;
672
673   if ( GetReaderHeader()->GetJetAlgorithm() == AliHLTJETBase::kFFSCRadiusCell )
674     useWholeCell = kFALSE ;
675
676   // -- Add track / particle
677   new( (*fJetCandidates) [fNJetCandidates] ) AliHLTJETConeJetCandidate( aEtaPhi, 
678                                                                         aGridIdx,
679                                                                         coneRadius,
680                                                                         useWholeCell );
681   fNJetCandidates++;
682
683   HLTDebug("Added Seed Pt=%f, Eta=%f, Phi=%f", aEtaPhi[kIdxPt], 
684            aEtaPhi[kIdxEta], aEtaPhi[kIdxPhi] );
685
686   return;
687 }
688
689 /*
690  * ---------------------------------------------------------------------------------
691  *                         Initialize - private
692  * ---------------------------------------------------------------------------------
693  */
694
695 // #################################################################################
696 Int_t AliHLTJETReader::InitializeFFSC() {
697   // see header file for class documentation
698
699   Int_t iResult = 0;
700   AliHLTJETReaderHeader* readerHeader = GetReaderHeader();
701
702   // -- Initialize grid
703   // --------------------
704   if ( fGrid )
705     delete fGrid;
706   
707   if ( ! (fGrid = new AliHLTJETConeGrid()) ) {
708     HLTError("Error instanciating grid.");
709     iResult = -EINPROGRESS;
710   }
711   
712   if ( ! iResult ) {
713     fGrid->SetEtaRange(   readerHeader->GetFiducialEtaMin(),
714                           readerHeader->GetFiducialEtaMax(),
715                           readerHeader->GetGridEtaRange() );
716     
717     fGrid->SetPhiRange(   readerHeader->GetFiducialPhiMin(),
718                           readerHeader->GetFiducialPhiMax(),
719                           readerHeader->GetGridPhiRange() );
720     
721     fGrid->SetBinning(    readerHeader->GetGridEtaBinning(),
722                           readerHeader->GetGridEtaBinning() );
723     
724     fGrid->SetConeRadius( readerHeader->GetConeRadius() );
725     
726     iResult = fGrid->Initialize();
727   }
728   
729   // -- Initialize jet candidates
730   // ------------------------------
731   if ( ! iResult ) {
732     fJetCandidates = new TClonesArray("AliHLTJETConeJetCandidate", 30);
733     if ( ! fJetCandidates) {
734       HLTError("Error instanciating jet candidates.");
735       iResult = -EINPROGRESS;
736     }
737   }
738  
739   return iResult;
740 }
741
742 #ifdef HAVE_FASTJET
743 // #################################################################################
744 Int_t AliHLTJETReader::InitializeFastjet() {
745   // see header file for class documentation
746
747   Int_t iResult = 0;
748
749   // -- Initialize Vector
750   // ----------------------
751   if ( fMomentumVector )
752     delete fMomentumVector;
753   
754   if ( ! (fMomentumVector = new vector<fastjet::PseudoJet>) ) {
755     HLTError("Error instanciating momentum vector.");
756     iResult = -EINPROGRESS;
757   }
758  
759   return iResult;
760 }
761 #endif