Fixing treatment of fOffset value in input data blocks.
[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         AliHLTUInt32_t specification = 0;  // Contains the output data block spec bits.
171
172         // First process the blocks of reconstructed hits and trigger records.
173         for (int i = 0; i < GetNumberOfInputBlocks(); i++)
174         {
175                 block = GetInputBlock(i);
176                 assert( block != NULL );
177                 
178                 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
179                         i, DataType2Text(block->fDataType).c_str(), block->fPtr, block->fSize
180                 );
181                 
182                 if (block->fDataType == AliHLTMUONConstants::RecHitsBlockDataType())
183                 {
184                         specification |= block->fSpecification;
185                         AliHLTMUONRecHitsBlockReader inblock(block->fPtr, 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                         specification |= block->fSpecification;
225                         AliHLTMUONTriggerRecordsBlockReader inblock(block->fPtr, block->fSize);
226                         if (not BlockStructureOk(inblock)) continue;
227                         
228                         // Decode the source DDL from the specification bits.
229                         Int_t sourceDDL = -1;
230                         bool ddl[22];
231                         AliHLTMUONUtils::UnpackSpecBits(block->fSpecification, ddl);
232                         for (int k = 0; k < 22; k++)
233                         {
234                                 if (ddl[k])
235                                 {
236                                         if (sourceDDL == -1)
237                                         {
238                                                 sourceDDL = k+1;
239                                         }
240                                         else
241                                         {
242                                                 HLTWarning("The input data block %d contains"
243                                                         " data from multiple DDL sources.", i
244                                                 );
245                                         }
246                                 }
247                         }
248                         if (sourceDDL != -1 and (sourceDDL < 21 or sourceDDL > 22))
249                         {
250                                 HLTWarning("The source DDL for input data block %d is %d."
251                                         " The expected range for the DDL is [21..22].",
252                                         i, sourceDDL
253                                 );
254                         }
255                         
256                         for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
257                         {
258                                 const AliHLTMUONTriggerRecordStruct& t = inblock[n];
259                                 
260                                 AliHLTMUONParticleSign sign;
261                                 bool hitset[4];
262                                 AliHLTMUONUtils::UnpackTriggerRecordFlags(
263                                                 t.fFlags, sign, hitset
264                                         );
265                         
266                                 AliHLTMUONTriggerRecord* tr = new AliHLTMUONTriggerRecord(
267                                                 t.fId, sign, t.fPx, t.fPy, t.fPz, sourceDDL
268                                         );
269                                 for (int k = 0; k < 4; k++)
270                                         tr->SetHit(k+11, t.fHit[k].fX, t.fHit[k].fY, t.fHit[k].fZ);
271                                 event.Add(tr);
272                         }
273                 }
274                 else
275                 {
276                         if (block->fDataType != AliHLTMUONConstants::MansoTracksBlockDataType() and
277                             block->fDataType != AliHLTMUONConstants::SinglesDecisionBlockDataType() and
278                             block->fDataType != AliHLTMUONConstants::PairsDecisionBlockDataType()
279                            )
280                         {
281                                 // Log a message indicating that we got a data block that we
282                                 // do not know how to handle.
283                                 if (fWarnForUnexpecedBlock)
284                                         HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
285                                                 DataType2Text(block->fDataType).c_str(), block->fSpecification
286                                         );
287                                 else
288                                         HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
289                                                 DataType2Text(block->fDataType).c_str(), block->fSpecification
290                                         );
291                         }
292                 }
293         }
294         
295         // Now we can look for tracks to add. We needed the ROOT trigger records
296         // and reco hits created before we can create track objects.
297         for (block = GetFirstInputBlock(AliHLTMUONConstants::MansoTracksBlockDataType());
298              block != NULL;
299              block = GetNextInputBlock()
300             )
301         {
302                 specification |= block->fSpecification;
303                 AliHLTMUONMansoTracksBlockReader inblock(block->fPtr, block->fSize);
304                 if (not BlockStructureOk(inblock)) continue;
305                 
306                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
307                 {
308                         const AliHLTMUONMansoTrackStruct& t = inblock[n];
309                         
310                         AliHLTMUONParticleSign sign;
311                         bool hitset[4];
312                         AliHLTMUONUtils::UnpackMansoTrackFlags(
313                                         t.fFlags, sign, hitset
314                                 );
315                         
316                         // Try find the trigger record in 'event'.
317                         const AliHLTMUONTriggerRecord* trigrec = NULL;
318                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
319                         {
320                                 if (event.Array()[k]->IsA() != AliHLTMUONTriggerRecord::Class())
321                                         continue;
322                                 const AliHLTMUONTriggerRecord* tk =
323                                         static_cast<const AliHLTMUONTriggerRecord*>(event.Array()[k]);
324                                 if (tk->Id() == t.fTrigRec)
325                                 {
326                                         trigrec = tk;
327                                         break;
328                                 }
329                         }
330                         
331                         // Now try find the hits in 'event'.
332                         // If they cannot be found then create new ones.
333                         const AliHLTMUONRecHit* hit7 = NULL;
334                         const AliHLTMUONRecHit* hit8 = NULL;
335                         const AliHLTMUONRecHit* hit9 = NULL;
336                         const AliHLTMUONRecHit* hit10 = NULL;
337                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
338                         {
339                                 if (event.Array()[k]->IsA() != AliHLTMUONRecHit::Class())
340                                         continue;
341                                 const AliHLTMUONRecHit* h =
342                                         static_cast<const AliHLTMUONRecHit*>(event.Array()[k]);
343                                 
344                                 if (hitset[0] and h->X() == t.fHit[0].fX and h->Y() == t.fHit[0].fY
345                                         and h->Z() == t.fHit[0].fZ)
346                                 {
347                                         hit7 = h;
348                                 }
349                                 if (hitset[1] and h->X() == t.fHit[1].fX and h->Y() == t.fHit[1].fY
350                                         and h->Z() == t.fHit[1].fZ)
351                                 {
352                                         hit8 = h;
353                                 }
354                                 if (hitset[2] and h->X() == t.fHit[2].fX and h->Y() == t.fHit[2].fY
355                                         and h->Z() == t.fHit[2].fZ)
356                                 {
357                                         hit9 = h;
358                                 }
359                                 if (hitset[3] and h->X() == t.fHit[3].fX and h->Y() == t.fHit[3].fY
360                                         and h->Z() == t.fHit[3].fZ)
361                                 {
362                                         hit10 = h;
363                                 }
364                         }
365                         AliHLTMUONRecHit* newhit;
366                         if (hitset[0] and hit7 == NULL)
367                         {
368                                 newhit = new AliHLTMUONRecHit(t.fHit[0].fX, t.fHit[0].fY, t.fHit[0].fZ);
369                                 event.Add(newhit);
370                                 hit7 = newhit;
371                         }
372                         if (hitset[1] and hit8 == NULL)
373                         {
374                                 newhit = new AliHLTMUONRecHit(t.fHit[1].fX, t.fHit[1].fY, t.fHit[1].fZ);
375                                 event.Add(newhit);
376                                 hit8 = newhit;
377                         }
378                         if (hitset[2] and hit9 == NULL)
379                         {
380                                 newhit = new AliHLTMUONRecHit(t.fHit[2].fX, t.fHit[2].fY, t.fHit[2].fZ);
381                                 event.Add(newhit);
382                                 hit9 = newhit;
383                         }
384                         if (hitset[3] and hit10 == NULL)
385                         {
386                                 newhit = new AliHLTMUONRecHit(t.fHit[3].fX, t.fHit[3].fY, t.fHit[3].fZ);
387                                 event.Add(newhit);
388                                 hit10 = newhit;
389                         }
390                 
391                         AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(
392                                         t.fId, sign, t.fPx, t.fPy, t.fPz, t.fChi2,
393                                         trigrec, hit7, hit8, hit9, hit10
394                                 );
395                         event.Add(tr);
396                 }
397         }
398         
399         bool decisionBlockFound = false;
400         UInt_t numLowPt = 0;
401         UInt_t numHighPt = 0;
402         TClonesArray singlesDecisions("AliHLTMUONDecision::AliTrackDecision");
403         
404         // Find the single tracks decision blocks and add their information.
405         // We just sum the trigger scalars and single decisions.
406         for (block = GetFirstInputBlock(AliHLTMUONConstants::SinglesDecisionBlockDataType());
407              block != NULL;
408              block = GetNextInputBlock()
409             )
410         {
411                 decisionBlockFound = true;
412                 specification |= block->fSpecification;
413                 AliHLTMUONSinglesDecisionBlockReader inblock(block->fPtr, block->fSize);
414                 if (not BlockStructureOk(inblock)) continue;
415                 
416                 numLowPt += inblock.BlockHeader().fNlowPt;
417                 numHighPt += inblock.BlockHeader().fNhighPt;
418                 
419                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
420                 {
421                         const AliHLTMUONTrackDecisionStruct& t = inblock[n];
422                         
423                         bool highPt, lowPt;
424                         AliHLTMUONUtils::UnpackTrackDecisionBits(t.fTriggerBits, highPt, lowPt);
425                         
426                         // Try find the corresponding track in the 'event'.
427                         const AliHLTMUONMansoTrack* track = NULL;
428                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
429                         {
430                                 if (event.Array()[k]->IsA() != AliHLTMUONMansoTrack::Class())
431                                         continue;
432                                 const AliHLTMUONMansoTrack* tk =
433                                         static_cast<const AliHLTMUONMansoTrack*>(event.Array()[k]);
434                                 if (tk->Id() == t.fTrackId)
435                                 {
436                                         track = tk;
437                                         break;
438                                 }
439                         }
440                         
441                         // If the track was not found then create a dummy one.
442                         if (track == NULL)
443                         {
444                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackId);
445                                 event.Add(tr);
446                                 track = tr;
447                         }
448                         
449                         new (singlesDecisions[singlesDecisions.GetEntriesFast()])
450                                 AliHLTMUONDecision::AliTrackDecision(t.fPt, lowPt, highPt, track);
451                 }
452         }
453         
454         UInt_t numUnlikeAnyPt = 0;
455         UInt_t numUnlikeLowPt = 0;
456         UInt_t numUnlikeHighPt = 0;
457         UInt_t numLikeAnyPt = 0;
458         UInt_t numLikeLowPt = 0;
459         UInt_t numLikeHighPt = 0;
460         UInt_t numAnyMass = 0;
461         UInt_t numLowMass = 0;
462         UInt_t numHighMass = 0;
463         TClonesArray pairsDecisions("AliHLTMUONDecision::AliPairDecision");
464         
465         // Find the track pairs decision blocks and add their information.
466         // We just sum the trigger scalars and track pair decisions.
467         for (block = GetFirstInputBlock(AliHLTMUONConstants::PairsDecisionBlockDataType());
468              block != NULL;
469              block = GetNextInputBlock()
470             )
471         {
472                 decisionBlockFound = true;
473                 specification |= block->fSpecification;
474                 AliHLTMUONPairsDecisionBlockReader inblock(block->fPtr, block->fSize);
475                 if (not BlockStructureOk(inblock)) continue;
476                 
477                 numUnlikeAnyPt += inblock.BlockHeader().fNunlikeAnyPt;
478                 numUnlikeLowPt += inblock.BlockHeader().fNunlikeLowPt;
479                 numUnlikeHighPt += inblock.BlockHeader().fNunlikeHighPt;
480                 numLikeAnyPt += inblock.BlockHeader().fNlikeAnyPt;
481                 numLikeLowPt += inblock.BlockHeader().fNlikeLowPt;
482                 numLikeHighPt += inblock.BlockHeader().fNlikeHighPt;
483                 numAnyMass += inblock.BlockHeader().fNmassAny;
484                 numLowMass += inblock.BlockHeader().fNmassLow;
485                 numHighMass += inblock.BlockHeader().fNmassHigh;
486                 
487                 for (AliHLTUInt32_t n = 0; n < inblock.Nentries(); n++)
488                 {
489                         const AliHLTMUONPairDecisionStruct& t = inblock[n];
490                         
491                         bool highMass, lowMass, unlike;
492                         AliHLTUInt8_t highPtCount, lowPtCount;
493                         AliHLTMUONUtils::UnpackPairDecisionBits(
494                                         t.fTriggerBits, highMass, lowMass, unlike,
495                                         highPtCount, lowPtCount
496                                 );
497                         
498                         // Try find the corresponding tracks in the 'event'.
499                         const AliHLTMUONMansoTrack* trackA = NULL;
500                         const AliHLTMUONMansoTrack* trackB = NULL;
501                         for (Int_t k = 0; k < event.Array().GetEntriesFast(); k++)
502                         {
503                                 if (event.Array()[k]->IsA() != AliHLTMUONMansoTrack::Class())
504                                         continue;
505                                 const AliHLTMUONMansoTrack* tk =
506                                         static_cast<const AliHLTMUONMansoTrack*>(event.Array()[k]);
507                                 if (tk->Id() == t.fTrackAId) trackA = tk;
508                                 if (tk->Id() == t.fTrackBId) trackB = tk;
509                                 if (trackA != NULL and trackB != NULL) break;
510                         }
511                         
512                         // If either of the tracks was not found then create a dummy one.
513                         if (trackA == NULL)
514                         {
515                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackAId);
516                                 event.Add(tr);
517                                 trackA = tr;
518                         }
519                         if (trackB == NULL)
520                         {
521                                 AliHLTMUONMansoTrack* tr = new AliHLTMUONMansoTrack(t.fTrackBId);
522                                 event.Add(tr);
523                                 trackB = tr;
524                         }
525                         
526                         new (pairsDecisions[pairsDecisions.GetEntriesFast()])
527                                 AliHLTMUONDecision::AliPairDecision(
528                                         t.fInvMass, lowMass, highMass, unlike,
529                                         lowPtCount, highPtCount, trackA, trackB
530                                 );
531                 }
532         }
533         
534         
535         // Do not add the decision if no decision blocks were found.
536         if (decisionBlockFound)
537         {
538                 AliHLTMUONDecision* triggerDecision = new AliHLTMUONDecision(
539                                 numLowPt, numHighPt, numUnlikeAnyPt, numUnlikeLowPt,
540                                 numUnlikeHighPt, numLikeAnyPt, numLikeLowPt,
541                                 numLikeHighPt, numAnyMass, numLowMass, numHighMass
542                         );
543                 for (Int_t i = 0; i < singlesDecisions.GetEntriesFast(); i++)
544                 {
545                         AliHLTMUONDecision::AliTrackDecision* decision =
546                                 static_cast<AliHLTMUONDecision::AliTrackDecision*>( singlesDecisions[i] );
547                         triggerDecision->AddDecision(decision);
548                 }
549                 for (Int_t j = 0; j < pairsDecisions.GetEntriesFast(); j++)
550                 {
551                         AliHLTMUONDecision::AliPairDecision* decision =
552                                 static_cast<AliHLTMUONDecision::AliPairDecision*>( pairsDecisions[j] );
553                         triggerDecision->AddDecision(decision);
554                 }
555                 
556                 event.Add(triggerDecision);
557         }
558         
559         PushBack(&event, "ROOTEVNT", "MUON", specification);
560         
561         return 0;
562 }
563