fd155cdad9398a61edca20e4e9ce833646e11355
[u/mrichter/AliRoot.git] / HLT / MUON / OfflineInterface / AliHLTMUONRootifierComponent.cxx
1 /**************************************************************************
2  * This file is property of and copyright by the ALICE HLT Project        * 
3  * All rights reserved.                                                   *
4  *                                                                        *
5  * Primary Authors:                                                       *
6  *   Artur Szostak <artursz@iafrica.com>                                  *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          * 
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18
19 ///
20 /// @file   AliHLTMUONRootifierComponent.cxx
21 /// @author Artur Szostak <artursz@iafrica.com>
22 /// @date   29 Sep 2007
23 /// @brief  Implementation of the AliHLTMUONRootifierComponent component.
24 ///
25
26 #include "AliHLTMUONRootifierComponent.h"
27 #include "AliHLTMUONEvent.h"
28 #include "AliHLTMUONConstants.h"
29 #include "AliHLTMUONUtils.h"
30 #include "AliHLTMUONRecHit.h"
31 #include "AliHLTMUONTriggerRecord.h"
32 #include "AliHLTMUONMansoTrack.h"
33 #include "AliHLTMUONDecision.h"
34 #include "TClonesArray.h"
35 #include <cassert>
36
37 ClassImp(AliHLTMUONRootifierComponent);
38
39
40 AliHLTMUONRootifierComponent::AliHLTMUONRootifierComponent() :
41         AliHLTMUONProcessor(),
42         fWarnForUnexpecedBlock(false)
43 {
44         ///
45         /// Default constructor.
46         ///
47 }
48
49
50 AliHLTMUONRootifierComponent::~AliHLTMUONRootifierComponent()
51 {
52         ///
53         /// Default destructor.
54         ///
55 }
56
57
58 int AliHLTMUONRootifierComponent::DoInit(int argc, const char** argv)
59 {
60         ///
61         /// Inherited from AliHLTComponent.
62         /// Parses the command line parameters and initialises the component.
63         ///
64         
65         HLTInfo("Initialising dHLT rootifier component.");
66         
67         fWarnForUnexpecedBlock = false;
68         
69         for (int i = 0; i < argc; i++)
70         {
71                 if (strcmp(argv[i], "-warn_on_unexpected_block") == 0)
72                 {
73                         fWarnForUnexpecedBlock = true;
74                         continue;
75                 }
76
77                 HLTError("Unknown option '%s'.", argv[i]);
78                 return -EINVAL;
79         }
80         
81         return 0;
82 }
83
84
85 int AliHLTMUONRootifierComponent::DoDeinit()
86 {
87         ///
88         /// Inherited from AliHLTComponent. Performs a cleanup of the component.
89         ///
90         
91         HLTInfo("Deinitialising dHLT rootifier component.");
92         return 0;
93 }
94
95
96 const char* AliHLTMUONRootifierComponent::GetComponentID()
97 {
98         ///
99         /// Inherited from AliHLTComponent. Returns the component ID.
100         ///
101         
102         return AliHLTMUONConstants::RootifierComponentId();
103 }
104
105
106 AliHLTComponentDataType AliHLTMUONRootifierComponent::GetOutputDataType()
107 {
108         /// Inherited from AliHLTComponent. Returns kAliHLTMultipleDataType
109         /// refer to GetOutputDataTypes for all returned data types.
110         
111         return kAliHLTMultipleDataType;
112 }
113
114
115 int AliHLTMUONRootifierComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
116 {
117         /// Inherited from AliHLTComponent. Returns the output data types.
118         
119         tgtList.push_back(kAliHLTAnyDataType);
120         return 1;
121 }
122
123
124 void AliHLTMUONRootifierComponent::GetInputDataTypes(
125                 vector<AliHLTComponentDataType>& list
126         )
127 {
128         ///
129         /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
130         ///
131         
132         list.push_back(kAliHLTAnyDataType);
133 }
134
135
136 void AliHLTMUONRootifierComponent::GetOutputDataSize(
137                 unsigned long& constBase, double& inputMultiplier
138         )
139 {
140         ///
141         /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
142         ///
143         
144         constBase = 1024*1024;
145         inputMultiplier = 100;
146 }
147
148
149 AliHLTComponent* AliHLTMUONRootifierComponent::Spawn()
150 {
151         ///
152         /// Inherited from AliHLTComponent. Creates a new object instance.
153         ///
154         
155         return new AliHLTMUONRootifierComponent();
156 }
157
158
159 int AliHLTMUONRootifierComponent::DoEvent(
160                 const AliHLTComponentEventData& evtData,
161                 AliHLTComponentTriggerData& /*trigData*/
162         )
163 {
164         ///
165         /// Inherited from AliHLTProcessor. Processes the new event data.
166         ///
167         
168         AliHLTMUONEvent event(evtData.fEventID);
169         const AliHLTComponentBlockData* block = NULL;
170
171         // First process the blocks of reconstructed hits and trigger records.
172         for (int i = 0; i < GetNumberOfInputBlocks(); i++)
173         {
174                 block = GetInputBlock(i);
175                 assert( block != NULL );
176                 
177                 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
178                         i, DataType2Text(block->fDataType).c_str(), block->fPtr, block->fSize
179                 );
180                 
181                 if (block->fDataType == AliHLTMUONConstants::RecHitsBlockDataType())
182                 {
183                         AliHLTUInt8_t* ptr = reinterpret_cast<AliHLTUInt8_t*>(block->fPtr);
184                         ptr += block->fOffset;
185                         AliHLTMUONRecHitsBlockReader inblock(ptr, block->fSize);
186                         if (not BlockStructureOk(inblock)) continue;
187                         
188                         // Decode the source DDL from the specification bits.
189                         Int_t sourceDDL = -1;
190                         bool ddl[22];
191                         AliHLTMUONUtils::UnpackSpecBits(block->fSpecification, ddl);
192                         for (int k = 0; k < 22; k++)
193                         {
194                                 if (ddl[k])
195                                 {
196                                         if (sourceDDL == -1)
197                                         {
198                                                 sourceDDL = k+1;
199                                         }
200                                         else
201                                         {
202                                                 HLTWarning("The input data block %d contains"
203                                                         " data from multiple DDL sources.", i
204                                                 );
205                                         }
206                                 }
207                         }
208                         if (sourceDDL > 20)
209                         {
210                                 HLTWarning("The source DDL for input data block %d is %d."
211                                         " The expected range for the DDL is [1..20].",
212                                         i, sourceDDL
213                                 );
214                         }
215                         
216                         for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
217                         {
218                                 const AliHLTMUONRecHitStruct& h = inblock[n];
219                                 event.Add(new AliHLTMUONRecHit(h.fX, h.fY, h.fZ, sourceDDL));
220                         }
221                 }
222                 else if (block->fDataType == AliHLTMUONConstants::TriggerRecordsBlockDataType())
223                 {
224                         AliHLTUInt8_t* ptr = reinterpret_cast<AliHLTUInt8_t*>(block->fPtr);
225                         ptr += block->fOffset;
226                         AliHLTMUONTriggerRecordsBlockReader inblock(ptr, block->fSize);
227                         if (not BlockStructureOk(inblock)) continue;
228                         
229                         // Decode the source DDL from the specification bits.
230                         Int_t sourceDDL = -1;
231                         bool ddl[22];
232                         AliHLTMUONUtils::UnpackSpecBits(block->fSpecification, ddl);
233                         for (int k = 0; k < 22; k++)
234                         {
235                                 if (ddl[k])
236                                 {
237                                         if (sourceDDL == -1)
238                                         {
239                                                 sourceDDL = k+1;
240                                         }
241                                         else
242                                         {
243                                                 HLTWarning("The input data block %d contains"
244                                                         " data from multiple DDL sources.", i
245                                                 );
246                                         }
247                                 }
248                         }
249                         if (sourceDDL != -1 and (sourceDDL < 21 or sourceDDL > 22))
250                         {
251                                 HLTWarning("The source DDL for input data block %d is %d."
252                                         " The expected range for the DDL is [21..22].",
253                                         i, sourceDDL
254                                 );
255                         }
256                         
257                         for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
258                         {
259                                 const AliHLTMUONTriggerRecordStruct& t = inblock[n];
260                                 
261                                 AliHLTMUONParticleSign sign;
262                                 bool hitset[4];
263                                 AliHLTMUONUtils::UnpackTriggerRecordFlags(
264                                                 t.fFlags, sign, hitset
265                                         );
266                         
267                                 AliHLTMUONTriggerRecord* tr = new AliHLTMUONTriggerRecord(
268                                                 t.fId, sign, t.fPx, t.fPy, t.fPz, sourceDDL
269                                         );
270                                 for (int k = 0; k < 4; k++)
271                                         tr->SetHit(k+11, t.fHit[k].fX, t.fHit[k].fY, t.fHit[k].fZ);
272                                 event.Add(tr);
273                         }
274                 }
275                 else
276                 {
277                         if (block->fDataType != AliHLTMUONConstants::MansoTracksBlockDataType() and
278                             block->fDataType != AliHLTMUONConstants::SinglesDecisionBlockDataType() and
279                             block->fDataType != AliHLTMUONConstants::PairsDecisionBlockDataType()
280                            )
281                         {
282                                 // Log a message indicating that we got a data block that we
283                                 // do not know how to handle.
284                                 if (fWarnForUnexpecedBlock)
285                                         HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
286                                                 DataType2Text(block->fDataType).c_str(), block->fSpecification
287                                         );
288                                 else
289                                         HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
290                                                 DataType2Text(block->fDataType).c_str(), block->fSpecification
291                                         );
292                         }
293                 }
294         }
295         
296         // Now we can look for tracks to add. We needed the ROOT trigger records
297         // and reco hits created before we can create track objects.
298         for (block = GetFirstInputBlock(AliHLTMUONConstants::MansoTracksBlockDataType());
299              block != NULL;
300              block = GetNextInputBlock()
301             )
302         {
303                 AliHLTUInt8_t* ptr = reinterpret_cast<AliHLTUInt8_t*>(block->fPtr);
304                 ptr += block->fOffset;
305                 AliHLTMUONMansoTracksBlockReader inblock(ptr, block->fSize);
306                 if (not BlockStructureOk(inblock)) continue;
307                 
308                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
309                 {
310                         const AliHLTMUONMansoTrackStruct& t = inblock[n];
311                         
312                         AliHLTMUONParticleSign sign;
313                         bool hitset[4];
314                         AliHLTMUONUtils::UnpackMansoTrackFlags(
315                                         t.fFlags, sign, hitset
316                                 );
317                         
318                         // Try find the trigger record in 'event'.
319                         const AliHLTMUONTriggerRecord* trigrec = NULL;
320                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
321                         {
322                                 if (event.Array()[k]->IsA() != AliHLTMUONTriggerRecord::Class())
323                                         continue;
324                                 const AliHLTMUONTriggerRecord* tk =
325                                         static_cast<const AliHLTMUONTriggerRecord*>(event.Array()[k]);
326                                 if (tk->Id() == t.fTrigRec)
327                                 {
328                                         trigrec = tk;
329                                         break;
330                                 }
331                         }
332                         
333                         // Now try find the hits in 'event'.
334                         // If they cannot be found then create new ones.
335                         const AliHLTMUONRecHit* hit7 = NULL;
336                         const AliHLTMUONRecHit* hit8 = NULL;
337                         const AliHLTMUONRecHit* hit9 = NULL;
338                         const AliHLTMUONRecHit* hit10 = NULL;
339                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
340                         {
341                                 if (event.Array()[k]->IsA() != AliHLTMUONRecHit::Class())
342                                         continue;
343                                 const AliHLTMUONRecHit* h =
344                                         static_cast<const AliHLTMUONRecHit*>(event.Array()[k]);
345                                 
346                                 if (hitset[0] and h->X() == t.fHit[0].fX and h->Y() == t.fHit[0].fY
347                                         and h->Z() == t.fHit[0].fZ)
348                                 {
349                                         hit7 = h;
350                                 }
351                                 if (hitset[1] and h->X() == t.fHit[1].fX and h->Y() == t.fHit[1].fY
352                                         and h->Z() == t.fHit[1].fZ)
353                                 {
354                                         hit8 = h;
355                                 }
356                                 if (hitset[2] and h->X() == t.fHit[2].fX and h->Y() == t.fHit[2].fY
357                                         and h->Z() == t.fHit[2].fZ)
358                                 {
359                                         hit9 = h;
360                                 }
361                                 if (hitset[3] and h->X() == t.fHit[3].fX and h->Y() == t.fHit[3].fY
362                                         and h->Z() == t.fHit[3].fZ)
363                                 {
364                                         hit10 = h;
365                                 }
366                         }
367                         AliHLTMUONRecHit* newhit;
368                         if (hitset[0] and hit7 == NULL)
369                         {
370                                 newhit = new AliHLTMUONRecHit(t.fHit[0].fX, t.fHit[0].fY, t.fHit[0].fZ);
371                                 event.Add(newhit);
372                                 hit7 = newhit;
373                         }
374                         if (hitset[1] and hit8 == NULL)
375                         {
376                                 newhit = new AliHLTMUONRecHit(t.fHit[1].fX, t.fHit[1].fY, t.fHit[1].fZ);
377                                 event.Add(newhit);
378                                 hit8 = newhit;
379                         }
380                         if (hitset[2] and hit9 == NULL)
381                         {
382                                 newhit = new AliHLTMUONRecHit(t.fHit[2].fX, t.fHit[2].fY, t.fHit[2].fZ);
383                                 event.Add(newhit);
384                                 hit9 = newhit;
385                         }
386                         if (hitset[3] and hit10 == NULL)
387                         {
388                                 newhit = new AliHLTMUONRecHit(t.fHit[3].fX, t.fHit[3].fY, t.fHit[3].fZ);
389                                 event.Add(newhit);
390                                 hit10 = newhit;
391                         }
392                 
393                         AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(
394                                         t.fId, sign, t.fPx, t.fPy, t.fPz, t.fChi2,
395                                         trigrec, hit7, hit8, hit9, hit10
396                                 );
397                         event.Add(tr);
398                 }
399         }
400         
401         UInt_t numLowPt = 0;
402         UInt_t numHighPt = 0;
403         TClonesArray singlesDecisions("AliHLTMUONDecision::AliTrackDecision");
404         
405         // Find the single tracks decision blocks and add their information.
406         // We just sum the trigger scalars and single decisions.
407         for (block = GetFirstInputBlock(AliHLTMUONConstants::SinglesDecisionBlockDataType());
408              block != NULL;
409              block = GetNextInputBlock()
410             )
411         {
412                 AliHLTUInt8_t* ptr = reinterpret_cast<AliHLTUInt8_t*>(block->fPtr);
413                 ptr += block->fOffset;
414                 AliHLTMUONSinglesDecisionBlockReader inblock(ptr, block->fSize);
415                 if (not BlockStructureOk(inblock)) continue;
416                 
417                 numLowPt += inblock.BlockHeader().fNlowPt;
418                 numHighPt += inblock.BlockHeader().fNhighPt;
419                 
420                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
421                 {
422                         const AliHLTMUONTrackDecisionStruct& t = inblock[n];
423                         
424                         bool highPt, lowPt;
425                         AliHLTMUONUtils::UnpackTrackDecisionBits(t.fTriggerBits, highPt, lowPt);
426                         
427                         // Try find the corresponding track in the 'event'.
428                         const AliHLTMUONMansoTrack* track = NULL;
429                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
430                         {
431                                 if (event.Array()[k]->IsA() != AliHLTMUONMansoTrack::Class())
432                                         continue;
433                                 const AliHLTMUONMansoTrack* tk =
434                                         static_cast<const AliHLTMUONMansoTrack*>(event.Array()[k]);
435                                 if (tk->Id() == t.fTrackId)
436                                 {
437                                         track = tk;
438                                         break;
439                                 }
440                         }
441                         
442                         // If the track was not found then create a dummy one.
443                         if (track == NULL)
444                         {
445                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackId);
446                                 event.Add(tr);
447                                 track = tr;
448                         }
449                         
450                         new (singlesDecisions[singlesDecisions.GetEntriesFast()])
451                                 AliHLTMUONDecision::AliTrackDecision(t.fPt, lowPt, highPt, track);
452                 }
453         }
454         
455         UInt_t numUnlikeAnyPt = 0;
456         UInt_t numUnlikeLowPt = 0;
457         UInt_t numUnlikeHighPt = 0;
458         UInt_t numLikeAnyPt = 0;
459         UInt_t numLikeLowPt = 0;
460         UInt_t numLikeHighPt = 0;
461         UInt_t numAnyMass = 0;
462         UInt_t numLowMass = 0;
463         UInt_t numHighMass = 0;
464         TClonesArray pairsDecisions("AliHLTMUONDecision::AliPairDecision");
465         
466         // Find the track pairs decision blocks and add their information.
467         // We just sum the trigger scalars and track pair decisions.
468         for (block = GetFirstInputBlock(AliHLTMUONConstants::PairsDecisionBlockDataType());
469              block != NULL;
470              block = GetNextInputBlock()
471             )
472         {
473                 AliHLTUInt8_t* ptr = reinterpret_cast<AliHLTUInt8_t*>(block->fPtr);
474                 ptr += block->fOffset;
475                 AliHLTMUONPairsDecisionBlockReader inblock(ptr, block->fSize);
476                 if (not BlockStructureOk(inblock)) continue;
477                 
478                 numUnlikeAnyPt += inblock.BlockHeader().fNunlikeAnyPt;
479                 numUnlikeLowPt += inblock.BlockHeader().fNunlikeLowPt;
480                 numUnlikeHighPt += inblock.BlockHeader().fNunlikeHighPt;
481                 numLikeAnyPt += inblock.BlockHeader().fNlikeAnyPt;
482                 numLikeLowPt += inblock.BlockHeader().fNlikeLowPt;
483                 numLikeHighPt += inblock.BlockHeader().fNlikeHighPt;
484                 numAnyMass += inblock.BlockHeader().fNmassAny;
485                 numLowMass += inblock.BlockHeader().fNmassLow;
486                 numHighMass += inblock.BlockHeader().fNmassHigh;
487                 
488                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
489                 {
490                         const AliHLTMUONPairDecisionStruct& t = inblock[n];
491                         
492                         bool highMass, lowMass, unlike;
493                         AliHLTUInt8_t highPtCount, lowPtCount;
494                         AliHLTMUONUtils::UnpackPairDecisionBits(
495                                         t.fTriggerBits, highMass, lowMass, unlike,
496                                         highPtCount, lowPtCount
497                                 );
498                         
499                         // Try find the corresponding tracks in the 'event'.
500                         const AliHLTMUONMansoTrack* trackA = NULL;
501                         const AliHLTMUONMansoTrack* trackB = NULL;
502                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
503                         {
504                                 if (event.Array()[k]->IsA() != AliHLTMUONMansoTrack::Class())
505                                         continue;
506                                 const AliHLTMUONMansoTrack* tk =
507                                         static_cast<const AliHLTMUONMansoTrack*>(event.Array()[k]);
508                                 if (tk->Id() == t.fTrackAId) trackA = tk;
509                                 if (tk->Id() == t.fTrackBId) trackB = tk;
510                                 if (trackA != NULL and trackB != NULL) break;
511                         }
512                         
513                         // If either of the tracks was not found then create a dummy one.
514                         if (trackA == NULL)
515                         {
516                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackAId);
517                                 event.Add(tr);
518                                 trackA = tr;
519                         }
520                         if (trackB == NULL)
521                         {
522                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackBId);
523                                 event.Add(tr);
524                                 trackB = tr;
525                         }
526                         
527                         new (pairsDecisions[pairsDecisions.GetEntriesFast()])
528                                 AliHLTMUONDecision::AliPairDecision(
529                                         t.fInvMass, lowMass, highMass, unlike,
530                                         lowPtCount, highPtCount, trackA, trackB
531                                 );
532                 }
533         }
534         
535         AliHLTMUONDecision* triggerDecision = new AliHLTMUONDecision(
536                         numLowPt, numHighPt, numUnlikeAnyPt, numUnlikeLowPt,
537                         numUnlikeHighPt, numLikeAnyPt, numLikeLowPt,
538                         numLikeHighPt, numAnyMass, numLowMass, numHighMass
539                 );
540         for (Int_t i = 0; i < singlesDecisions.GetEntriesFast(); i++)
541         {
542                 AliHLTMUONDecision::AliTrackDecision* decision =
543                         static_cast<AliHLTMUONDecision::AliTrackDecision*>( singlesDecisions[i] );
544                 triggerDecision->AddDecision(decision);
545         }
546         for (Int_t j = 0; j < pairsDecisions.GetEntriesFast(); j++)
547         {
548                 AliHLTMUONDecision::AliPairDecision* decision =
549                         static_cast<AliHLTMUONDecision::AliPairDecision*>( pairsDecisions[j] );
550                 triggerDecision->AddDecision(decision);
551         }
552         event.Add(triggerDecision);
553         
554         PushBack(&event, "ROOTEVNT", "MUON");
555         
556         return 0;
557 }
558