]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/AliHLTMUONProcessor.cxx
Adding extra checks to protect from fatal aborts.
[u/mrichter/AliRoot.git] / HLT / MUON / AliHLTMUONProcessor.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   AliHLTMUONProcessor.cxx
21 /// @author Artur Szostak <artursz@iafrica.com>
22 /// @date   19 May 2008
23 /// @brief  Implementation of the abstract base dHLT processor component.
24 ///
25 /// This component is the abstract base class of dHLT specific components.
26 /// It implements some common methods used by all the dHLT components.
27 ///
28
29 #include "AliHLTMUONProcessor.h"
30 #include "AliHLTMUONConstants.h"
31 #include "AliMUONRecoParam.h"
32 #include "AliCDBManager.h"
33 #include "AliCDBStorage.h"
34 #include "AliCDBEntry.h"
35 #include "AliGRPManager.h"
36 #include "AliGRPObject.h"
37 #include "AliMagF.h"
38 #include "AliMpCDB.h"
39 #include "AliMpDDLStore.h"
40 #include "AliMpDEStore.h"
41 #include "TGeoGlobalMagField.h"
42 #include "TMap.h"
43 #include "TObjString.h"
44 #include "TString.h"
45 #include <string>
46 #include <cstdlib>
47 #include <fstream>
48
49
50 ClassImp(AliHLTMUONProcessor)
51
52
53 AliHLTMUONProcessor::AliHLTMUONProcessor() :
54         AliHLTProcessor(),
55         fWarnForUnexpecedBlock(false),
56         fDelaySetup(false),
57         fDumpDataOnError(false),
58         fDumpPath("./")
59 {
60         /// Default constructor.
61 }
62
63
64 int AliHLTMUONProcessor::DoInit(int argc, const char** argv)
65 {
66         /// Parses common dHLT component arguments.
67
68         // Set the default values for various arguments comming from the command line.
69         fDelaySetup = false;
70         fDumpDataOnError = false;
71         fDumpPath = "./";
72         const char* cdbPath = NULL;
73         Int_t run = -1;
74
75         for (int i = 0; i < argc; i++)
76         {
77                 // Ignore the argument if the child class indicates to do so.
78                 if (IgnoreArgument(argv[i])) continue;
79         
80                 if (strcmp(argv[i], "-cdbpath") == 0)
81                 {
82                         if (cdbPath != NULL)
83                         {
84                                 HLTWarning("CDB path was already specified. Will"
85                                         " replace previous value given by -cdbpath."
86                                 );
87                         }
88                         if (argc <= i+1)
89                         {
90                                 HLTError("The CDB path was not specified." );
91                                 return -EINVAL;
92                         }
93                         cdbPath = argv[i+1];
94                         i++;
95                         continue;
96                 }
97         
98                 if (strcmp(argv[i], "-run") == 0)
99                 {
100                         if (run != -1)
101                         {
102                                 HLTWarning("Run number was already specified. Will"
103                                         " replace previous value given by -run."
104                                 );
105                         }
106                         if (argc <= i+1)
107                         {
108                                 HLTError("The run number was not specified.");
109                                 return -EINVAL;
110                         }
111                         
112                         char* cpErr = NULL;
113                         run = Int_t( strtol(argv[i+1], &cpErr, 0) );
114                         if (cpErr == NULL or *cpErr != '\0' or run < 0)
115                         {
116                                 HLTError("Cannot convert '%s' to a valid run number."
117                                         " Expected a positive integer value.", argv[i+1]
118                                 );
119                                 return -EINVAL;
120                         }
121                         
122                         i++;
123                         continue;
124                 }
125                 
126                 if (strcmp(argv[i], "-delaysetup") == 0)
127                 {
128                         fDelaySetup = true;
129                         continue;
130                 }
131                 
132                 if (strcmp(argv[i], "-dumponerror") == 0)
133                 {
134                         fDumpDataOnError = true;
135                         continue;
136                 }
137                 
138                 if (strcmp(argv[i], "-dumppath") == 0)
139                 {
140                         if (fDumpPath != NULL)
141                         {
142                                 HLTWarning("The dump path was already specified. Will"
143                                         " replace previous value given by -dumppath."
144                                 );
145                         }
146                         if (argc <= i+1)
147                         {
148                                 HLTError("The dump path was not specified.");
149                                 return -EINVAL;
150                         }
151                         fDumpPath = argv[i+1];
152                         i++;
153                         continue;
154                 }
155         }
156         
157         if (cdbPath != NULL or run != -1)
158         {
159                 int result = SetCDBPathAndRunNo(cdbPath, run);
160                 if (result != 0)
161                 {
162                         // Error messages already generated in SetCDBPathAndRunNo.
163                         return result;
164                 }
165         }
166
167         return 0;
168 }
169
170
171 bool AliHLTMUONProcessor::ArgumentAlreadyHandled(int& i, const char* argi) const
172 {
173         /// This method can be used by the derivind child class to check if a particular
174         /// argument in argv was already processed.
175
176         if (strcmp(argi, "-cdbpath") == 0)
177         {
178                 if (IgnoreArgument(argi)) return false;
179                 i++;  // argument takes one parameter
180                 return true;
181         }
182
183         if (strcmp(argi, "-run") == 0)
184         {
185                 if (IgnoreArgument(argi)) return false;
186                 i++;  // argument takes one parameter
187                 return true;
188         }
189         
190         if (strcmp(argi, "-delaysetup") == 0)
191         {
192                 if (IgnoreArgument(argi)) return false;
193                 return true;
194         }
195         
196         if (strcmp(argi, "-dumponerror") == 0)
197         {
198                 if (IgnoreArgument(argi)) return false;
199                 return true;
200         }
201         
202         if (strcmp(argi, "-dumppath") == 0)
203         {
204                 if (IgnoreArgument(argi)) return false;
205                 i++;  // argument takes one parameter
206                 return true;
207         }
208
209         return false;
210 }
211
212
213 int AliHLTMUONProcessor::SetCDBPathAndRunNo(
214                 const char* cdbPath, Int_t run, bool useDefault
215         ) const
216 {
217         /// Sets the CDB path and run number to read from.
218         /// \param cdbPath  The CDB path to use. If set to NULL and the path has
219         ///      not been set in the CDB manager then the default path
220         ///      "local://$ALICE_ROOT/OCDB" is used if the 'useDefault' flag is also true.
221         /// \param run  The run number to use. If set to -1 and the run number has
222         ///      not been set in the CDB manager then a value of zero is used if
223         ///      the 'useDefault' flag is also true.
224         /// \param useDefault  If set to true then a default CDB path and/or run number
225         ///      is used if they have not been set and 'cdbPath' == NULL or
226         ///      'run' == -1.
227         /// \return Zero if the object could be loaded. Otherwise an error code,
228         ///      compatible with the HLT framework, is returned.
229         
230         const char* defaultPath = "local://$ALICE_ROOT/OCDB";
231         Int_t defaultRun = 0;
232         
233         AliCDBManager* cdbManager = AliCDBManager::Instance();
234         if (cdbManager == NULL)
235         {
236                 HLTError("CDB manager instance does not exist.");
237                 return -EIO;
238         }
239         
240         // Setup the CDB path.
241         if (cdbPath != NULL)
242         {
243                 cdbManager->SetDefaultStorage(cdbPath);
244         }
245         else if (not cdbManager->IsDefaultStorageSet() and useDefault)
246         {
247                 cdbManager->SetDefaultStorage(defaultPath);
248         }
249         
250         // Now setup the run number.
251         if (run != -1)
252         {
253                 cdbManager->SetRun(run);
254         }
255         else
256         {
257                 if (useDefault) cdbManager->SetRun(defaultRun);
258         }
259         
260         return 0;
261 }
262
263
264 int AliHLTMUONProcessor::FetchMappingStores() const
265 {
266         /// Fetches the DDL and detector element store objects for MUON mapping.
267         /// \return Zero if the objects could be loaded. Otherwise an error code,
268         ///      which is compatible with the HLT framework, is returned.
269         /// \note AliMpDDLStore::Instance() and AliMpDEStore::Instance() must be used
270         ///      to fetch the objects after this method returns a code equal to zero.
271         
272         Bool_t warn = kFALSE;
273         
274         // Check if the objects are already loaded. If they are then exit early,
275         // otherwise we need to try load the objects.
276         if (AliMpDDLStore::Instance(warn) != NULL and AliMpDEStore::Instance(warn) != NULL)
277                 return 0;
278         
279         AliCDBManager* cdbManager = AliCDBManager::Instance();
280         if (cdbManager == NULL)
281         {
282                 HLTError("CDB manager instance does not exist.");
283                 return -EIO;
284         }
285         
286         const char* cdbPathUsed = "unknown (not set)";
287         AliCDBStorage* store = cdbManager->GetDefaultStorage();
288         if (store != NULL) cdbPathUsed = store->GetURI().Data();
289         
290         Int_t runUsed = cdbManager->GetRun();
291         
292         // Now we can try load the DDL and DE store objects.
293         if (cdbManager->GetId("MUON/Calib/MappingData", runUsed) == NULL)
294         {
295                 HLTError("Could not find entry in CDB path '%s/MUON/Calib/MappingData' and run no. %d.",
296                         cdbPathUsed, runUsed
297                 );
298                 return -ENOENT;
299         }
300         if (cdbManager->GetId("MUON/Calib/Gains", runUsed) == NULL)
301         {
302                 HLTError("Could not find entry in CDB path '%s/MUON/Calib/Gains' and run no. %d.",
303                         cdbPathUsed, runUsed
304                 );
305                 return -ENOENT;
306         }
307         if (cdbManager->GetId("MUON/Calib/Pedestals", runUsed) == NULL)
308         {
309                 HLTError("Could not find entry in CDB path '%s/MUON/Calib/Pedestals' and run no. %d.",
310                         cdbPathUsed, runUsed
311                 );
312                 return -ENOENT;
313         }
314         if (not AliMpCDB::LoadDDLStore(warn))
315         {
316                 HLTError("Failed to load DDL or detector element store specified"
317                          " for CDB path '%s' and run no. %d.",
318                         cdbPathUsed, runUsed
319                 );
320                 return -EIO;
321         }
322         
323         if (AliMpDDLStore::Instance(warn) == NULL or AliMpDEStore::Instance(warn) == NULL)
324         {
325                 HLTError("Could not find or load the DDL or detector element store instance.");
326                 return -EIO;
327         }
328         
329         return 0;
330 }
331
332
333 int AliHLTMUONProcessor::FetchTMapFromCDB(const char* pathToEntry, TMap*& map) const
334 {
335         /// Fetches a TMap object from the CDB.
336         /// [in] \param pathToEntry  The relative path to the entry in the CDB to fetch.
337         /// [out] \param map  This will be filled with the TMap object found if
338         ///      a successful status code is returned. Otherwise it will be unchanged.
339         /// \return Zero if the object could be found. Otherwise an error code,
340         ///      which is compatible with the HLT framework, is returned.
341         
342         assert(AliCDBManager::Instance() != NULL);
343         
344         AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage();
345         if (store == NULL)
346         {
347                 HLTError("Could not get the the default storage for the CDB.");
348                 return -EIO;
349         }
350
351         Int_t version = store->GetLatestVersion(pathToEntry, GetRunNo());
352         Int_t subVersion = store->GetLatestSubVersion(pathToEntry, GetRunNo(), version);
353         AliCDBId* entryId = AliCDBManager::Instance()->GetId(pathToEntry, GetRunNo(), version, subVersion);
354         if (entryId == NULL)
355         {
356                 HLTError("Could not find the CDB entry for \"%s\".", pathToEntry);
357                 return -ENOENT;
358         }
359         AliCDBEntry* entry = AliCDBManager::Instance()->Get(*entryId);
360         if (entry == NULL)
361         {
362                 HLTError("Could not fetch the CDB entry for \"%s\".", pathToEntry);
363                 return -EIO;
364         }
365         
366         TObject* obj = entry->GetObject();
367         if (obj == NULL)
368         {
369                 HLTError("Configuration object for \"%s\" is missing.", pathToEntry);
370                 return -ENOENT;
371         }
372         
373         if (obj->IsA() != TMap::Class())
374         {
375                 HLTError("Wrong type for configuration object in \"%s\". Found a %s but we need a TMap.",
376                         pathToEntry, obj->ClassName()
377                 );
378                 return -EPROTO;
379         }
380         map = dynamic_cast<TMap*>(obj);
381         
382         return 0;
383 }
384
385
386 int AliHLTMUONProcessor::GetValueFromTMap(
387                 TMap* map, const char* paramName, TString& value,
388                 const char* pathToEntry, const char* prettyName
389         ) const
390 {
391         /// Tries to find the string value associated with a certain parameter in a TMap.
392         /// [in] \param map  The TMap object to search in.
393         /// [in] \param paramName  The name of the parameter to search for.
394         /// [out] \param value  Will be filled with the object found.
395         /// [in] \param pathToEntry  The relative path to the entry in the CDB.
396         ///      Used when printing error messages. If set to NULL then a string of
397         ///      "(unknown)" is used. (default is NULL).
398         /// [in] \param prettyName  Should be the name of the parameter which will
399         ///      be used when printing error messages. If this is set to NULL then
400         ///      the paramName will be used instead (default is NULL).
401         /// \return Zero if the object could be found. Otherwise an error code,
402         ///      which is compatible with the HLT framework, is returned.
403         
404         if (pathToEntry == NULL) pathToEntry = "(unknown)";
405         if (prettyName == NULL) prettyName = paramName;
406         
407         TPair* pair = static_cast<TPair*>(map->FindObject(paramName));
408         if (pair == NULL)
409         {
410                 HLTError("Configuration object for \"%s\" does not contain the %s value.",
411                         pathToEntry, prettyName
412                 );
413                 return -ENOENT;
414         }
415         TObject* valueObj = pair->Value();
416         if (valueObj->IsA() != TObjString::Class())
417         {
418                 HLTError("The %s parameter found in configuration object \"%s\""
419                         " is not a TObjString. Found an object of type %s instead.",
420                         prettyName, pathToEntry, valueObj->ClassName()
421                 );
422                 return -EPROTO;
423         }
424         value = dynamic_cast<TObjString*>(valueObj)->GetString();
425         
426         return 0;
427 }
428
429
430 int AliHLTMUONProcessor::GetIntFromTMap(
431                 TMap* map, const char* paramName, Int_t& value,
432                 const char* pathToEntry, const char* prettyName
433         ) const
434 {
435         /// Tries to find a certain parameter in the TMap object and convert it to
436         /// an integer value.
437         /// [in] \param map  The TMap object to search in.
438         /// [in] \param paramName  The name of the parameter to search for.
439         /// [out] \param value  Will be filled with the integer value for the parameter,
440         ///       if it was found and it was an integer value.
441         /// [in] \param pathToEntry  The relative path to the entry in the CDB.
442         ///      Used when printing error messages. If set to NULL then a string of
443         ///      "(unknown)" is used. (default is NULL).
444         /// [in] \param prettyName  Should be the name of the parameter which will
445         ///      be used when printing error messages. If this is set to NULL then
446         ///      the paramName will be used instead (default is NULL).
447         /// \return Zero if the object could be found and is valid. Otherwise an
448         ///       error code, which is compatible with the HLT framework, is returned.
449         
450         if (pathToEntry == NULL) pathToEntry = "(unknown)";
451         if (prettyName == NULL) prettyName = paramName;
452         
453         TString valueStr;
454         int result = GetValueFromTMap(map, paramName, valueStr, pathToEntry, prettyName);
455         if (result != 0) return result;
456         
457         if (not valueStr.IsDigit())
458         {
459                 HLTError("The %s parameter found in configuration object \"%s\""
460                         "is not a valid integer number string; found \"%s\".",
461                         prettyName, pathToEntry, valueStr.Data()
462                 );
463                 return -EPROTO;
464         }
465         value = valueStr.Atoi();
466         
467         return 0;
468 }
469
470
471 int AliHLTMUONProcessor::GetPositiveIntFromTMap(
472                 TMap* map, const char* paramName, Int_t& value,
473                 const char* pathToEntry, const char* prettyName
474         ) const
475 {
476         /// Tries to find a certain parameter in the TMap object and convert it to
477         /// a positive integer value.
478         /// [in] \param map  The TMap object to search in.
479         /// [in] \param paramName  The name of the parameter to search for.
480         /// [out] \param value  Will be filled with the integer value for the parameter,
481         ///       if it was found and it was a positive integer value.
482         /// [in] \param pathToEntry  The relative path to the entry in the CDB.
483         ///      Used when printing error messages. If set to NULL then a string of
484         ///      "(unknown)" is used. (default is NULL).
485         /// [in] \param prettyName  Should be the name of the parameter which will
486         ///      be used when printing error messages. If this is set to NULL then
487         ///      the paramName will be used instead (default is NULL).
488         /// \return Zero if the object could be found and is valid. Otherwise an
489         ///       error code, which is compatible with the HLT framework, is returned.
490         
491         if (pathToEntry == NULL) pathToEntry = "(unknown)";
492         if (prettyName == NULL) prettyName = paramName;
493         
494         TString valueStr;
495         int result = GetValueFromTMap(map, paramName, valueStr, pathToEntry, prettyName);
496         if (result != 0) return result;
497         
498         if (not valueStr.IsDigit())
499         {
500                 HLTError("The %s parameter found in configuration object \"%s\""
501                         "is not a valid integer number string; found \"%s\".",
502                         prettyName, pathToEntry, valueStr.Data()
503                 );
504                 return -EPROTO;
505         }
506         Int_t val = valueStr.Atoi();
507         if (val < 0)
508         {
509                 HLTError("The %s parameter found in configuration object \"%s\""
510                         "is not a positive integer number; found \"%d\".",
511                         prettyName, pathToEntry, val
512                 );
513                 return -EPROTO;
514         }
515         value = val;
516         
517         return 0;
518 }
519
520
521 int AliHLTMUONProcessor::GetFloatFromTMap(
522                 TMap* map, const char* paramName, Double_t& value,
523                 const char* pathToEntry, const char* prettyName
524         ) const
525 {
526         /// Tries to find a certain parameter in the TMap object and convert it to
527         /// an floating point value.
528         /// [in] \param map  The TMap object to search in.
529         /// [in] \param paramName  The name of the parameter to search for.
530         /// [out] \param value  Will be filled with the floating point value for the
531         ///       parameter, if it was found and it was a floating point value.
532         /// [in] \param pathToEntry  The relative path to the entry in the CDB.
533         ///      Used when printing error messages. If set to NULL then a string of
534         ///      "(unknown)" is used. (default is NULL).
535         /// [in] \param prettyName  Should be the name of the parameter which will
536         ///      be used when printing error messages. If this is set to NULL then
537         ///      the paramName will be used instead (default is NULL).
538         /// \return Zero if the object could be found and is valid. Otherwise an
539         ///       error code, which is compatible with the HLT framework, is returned.
540         
541         if (pathToEntry == NULL) pathToEntry = "(unknown)";
542         if (prettyName == NULL) prettyName = paramName;
543         
544         TString valueStr;
545         int result = GetValueFromTMap(map, paramName, valueStr, pathToEntry, prettyName);
546         if (result != 0) return result;
547         
548         if (not valueStr.IsFloat())
549         {
550                 HLTError("The %s parameter found in configuration object \"%s\""
551                         "is not a valid floating point number string; found \"%s\".",
552                         prettyName, pathToEntry, valueStr.Data()
553                 );
554                 return -EPROTO;
555         }
556         value = valueStr.Atof();
557         
558         return 0;
559 }
560
561
562 int AliHLTMUONProcessor::GetPositiveFloatFromTMap(
563                 TMap* map, const char* paramName, Double_t& value,
564                 const char* pathToEntry, const char* prettyName
565         ) const
566 {
567         /// Tries to find a certain parameter in the TMap object and convert it to
568         /// an positive floating point value.
569         /// [in] \param map  The TMap object to search in.
570         /// [in] \param paramName  The name of the parameter to search for.
571         /// [out] \param value  Will be filled with the floating point value for the
572         ///       parameter, if it was found and it was a positive floating point value.
573         /// [in] \param pathToEntry  The relative path to the entry in the CDB.
574         ///      Used when printing error messages. If set to NULL then a string of
575         ///      "(unknown)" is used. (default is NULL).
576         /// [in] \param prettyName  Should be the name of the parameter which will
577         ///      be used when printing error messages. If this is set to NULL then
578         ///      the paramName will be used instead (default is NULL).
579         /// \return Zero if the object could be found and is valid. Otherwise an
580         ///       error code, which is compatible with the HLT framework, is returned.
581         
582         if (pathToEntry == NULL) pathToEntry = "(unknown)";
583         if (prettyName == NULL) prettyName = paramName;
584         
585         TString valueStr;
586         int result = GetValueFromTMap(map, paramName, valueStr, pathToEntry, prettyName);
587         if (result != 0) return result;
588         
589         if (not valueStr.IsFloat())
590         {
591                 HLTError("The %s parameter found in configuration object \"%s\""
592                         "is not a valid floating point number string; found \"%s\".",
593                         prettyName, pathToEntry, valueStr.Data()
594                 );
595                 return -EPROTO;
596         }
597         Double_t val = valueStr.Atof();
598         if (val < 0)
599         {
600                 HLTError("The %s parameter found in configuration object \"%s\""
601                         "is not a positive floating point number; found \"%d\".",
602                         prettyName, pathToEntry, val
603                 );
604                 return -EPROTO;
605         }
606         value = val;
607         
608         return 0;
609 }
610
611
612 int AliHLTMUONProcessor::FetchFieldIntegral(Double_t& bfieldintegral) const
613 {
614         // Fetches the correct dipole magnetic field integral to use.
615         
616         Float_t currentL3 = 0;
617         Float_t currentDip = 0;
618         
619         if (TGeoGlobalMagField::Instance() == NULL or
620             (TGeoGlobalMagField::Instance() != NULL and TGeoGlobalMagField::Instance()->GetField() == NULL)
621            )
622         {
623                 HLTWarning("The magnetic field has not been set in TGeoGlobalMagField."
624                         " Will try and load the GRP entry directly."
625                 );
626                 
627                 AliGRPManager grpman;
628                 if (not grpman.ReadGRPEntry() or grpman.GetGRPData() == NULL)
629                 {
630                         HLTError("GRP entry could not be loaded.");
631                         return -EIO;
632                 }
633                 
634                 const AliGRPObject* grp = grpman.GetGRPData();
635                 Char_t polarityL3 = grp->GetL3Polarity();
636                 Char_t polarityDip = grp->GetDipolePolarity();
637                 currentL3 = grp->GetL3Current(AliGRPObject::kMean);
638                 currentDip = grp->GetDipoleCurrent(AliGRPObject::kMean);
639                 if (polarityL3 == AliGRPObject::GetInvalidChar())
640                 {
641                         HLTError("L3 polarity in GRP is invalid.");
642                         return -EPROTO;
643                 }
644                 if (polarityDip == AliGRPObject::GetInvalidChar())
645                 {
646                         HLTError("Dipole polarity in GRP is invalid.");
647                         return -EPROTO;
648                 }
649                 if (currentL3 == AliGRPObject::GetInvalidFloat())
650                 {
651                         HLTError("L3 current in GRP is invalid.");
652                         return -EPROTO;
653                 }
654                 if (currentDip == AliGRPObject::GetInvalidFloat())
655                 {
656                         HLTError("Dipole current in GRP is invalid.");
657                         return -EPROTO;
658                 }
659                 if (grp->IsPolarityConventionLHC())
660                 {
661                         currentL3 *= (polarityL3 ? -1:1);
662                         currentDip *= (polarityDip ? -1:1);
663                 }
664                 else
665                 {
666                         currentL3 *= (polarityL3 ? -1:1);
667                         currentDip *= (polarityDip ? 1:-1);
668                 }
669         }
670         else
671         {
672                 TVirtualMagField* vfield = TGeoGlobalMagField::Instance()->GetField();
673                 AliMagF* field = dynamic_cast<AliMagF*>(vfield);
674                 if (vfield->IsA() != AliMagF::Class() and field != NULL)
675                 {
676                         HLTError(Form(
677                                 "The magnetic field is not of type AliMagF."
678                                 " Do not know how to handle class of type '%s'.",
679                                 vfield->ClassName()
680                         ));
681                         return -EPROTO;
682                 }
683                 currentL3 = field->GetCurrentSol();
684                 currentDip = field->GetCurrentDip();
685         }
686         
687         const char* path = AliHLTMUONConstants::FieldIntegralsCDBPath();
688         TMap* map = NULL;
689         int result = FetchTMapFromCDB(path, map);
690         if (result != 0) return result;
691         const char* paramName = Form("L3_current=%0.2e;Dipole_current=%0.2e", currentL3, currentDip);
692         Double_t value;
693         result = GetFloatFromTMap(map, paramName, value, path);
694         if (result != 0) return result;
695         bfieldintegral = value;
696         return 0;
697 }
698
699
700 int AliHLTMUONProcessor::LoadRecoParamsFromCDB(AliMUONRecoParam*& params) const
701 {
702         /// Fetches the reconstruction parameters object from the CDB for MUON.
703         /// [out] \param params  This will be filled with the reconstruction
704         ///      parameters object found if a successful status code is returned.
705         ///      Otherwise it will be unchanged.
706         /// \return Zero if the object could be found. Otherwise an error code,
707         ///      which is compatible with the HLT framework, is returned.
708         
709         assert(AliCDBManager::Instance() != NULL);
710
711         AliCDBStorage* store = AliCDBManager::Instance()->GetDefaultStorage();
712         if (store == NULL)
713         {
714                 HLTError("Could not get the the default storage for the CDB.");
715                 return -EIO;
716         }
717
718         const char* pathToEntry = "MUON/Calib/RecoParam";
719         Int_t version = store->GetLatestVersion(pathToEntry, GetRunNo());
720         Int_t subVersion = store->GetLatestSubVersion(pathToEntry, GetRunNo(), version);
721         AliCDBId* entryId = AliCDBManager::Instance()->GetId(pathToEntry, GetRunNo(), version, subVersion);
722         if (entryId == NULL)
723         {
724                 HLTError("Could not find the CDB entry for \"%s\".", pathToEntry);
725                 return -ENOENT;
726         }
727         AliCDBEntry* entry = AliCDBManager::Instance()->Get(*entryId);
728         if (entry == NULL)
729         {
730                 HLTError("Could not fetch the CDB entry for \"%s\".", pathToEntry);
731                 return -EIO;
732         }
733         
734         TObject* obj = entry->GetObject();
735         if (obj == NULL)
736         {
737                 HLTError("Reconstruction parameters object for \"%s\" is missing.", pathToEntry);
738                 return -ENOENT;
739         }
740         
741         TObjArray* objarr = dynamic_cast<TObjArray*>(obj);
742         if (objarr != NULL)
743         {
744                 obj = objarr->Last();
745         }
746         
747         AliMUONRecoParam* par = dynamic_cast<AliMUONRecoParam*>(obj);
748         if (par == NULL)
749         {
750                 HLTError("No AliMUONRecoParam class found for entry \"%s\". Found a %s class instead.",
751                         pathToEntry, obj->ClassName()
752                 );
753                 return -EPROTO;
754         }
755         
756         params = par;
757         return 0;
758 }
759
760
761 void AliHLTMUONProcessor::DumpBuffer(
762                 const void* buffer, AliHLTUInt32_t size, const char* filename
763         ) const
764 {
765         /// Dumps the data contained in a buffer to file as is.
766
767         using std::fstream;
768
769         fstream file(filename, fstream::out | fstream::trunc | fstream::binary);
770         if (file.good())
771         {
772                 file.write(reinterpret_cast<const char*>(buffer), size);
773                 if (file.fail())
774                 {
775                         HLTError("Could not write data block to file %s during"
776                                 " dumping operation!",
777                                 filename
778                         );
779                 }
780         }
781         else
782         {
783                 HLTError("Could not open file %s for dumping data block!", filename);
784         }
785 }
786
787
788 void AliHLTMUONProcessor::DumpBlock(
789                 const AliHLTComponentBlockData* block, const char* fileNamePrefix
790         ) const
791 {
792         /// Dumps the data block and meta information to file.
793
794         std::string filename = fDumpPath;
795         filename += fileNamePrefix;
796         filename += "-blockmeta.bin";
797         DumpBuffer(block, sizeof(AliHLTComponentBlockData), filename.c_str());
798         filename = fDumpPath;
799         filename += fileNamePrefix;
800         filename += "-data.bin";
801         DumpBuffer(block->fPtr, block->fSize, filename.c_str());
802 }
803
804
805 void AliHLTMUONProcessor::DumpEvent(
806                 const AliHLTComponentEventData& evtData,
807                 const AliHLTComponentBlockData* blocks,
808                 AliHLTComponentTriggerData& trigData,
809                 AliHLTUInt8_t* outputPtr,
810                 AliHLTUInt32_t& size,
811                 AliHLTComponentBlockDataList& outputBlocks
812         ) const
813 {
814         /// Dumps the event information to files in the dump path given by the
815         /// method DumpPath, which can be set by the command line argument -dumppath.
816
817         using std::fstream;
818         char strbuf[1024];
819
820         std::string filename = fDumpPath;
821         sprintf(strbuf, "dump_event-0x%16.16llX.log", evtData.fEventID);
822         filename += strbuf;
823         fstream logfile(filename.c_str(), fstream::out | fstream::trunc);
824         if (logfile.fail())
825         {
826                 HLTError("Could not open log file '%s' for dump information.", filename.c_str());
827                 return;
828         }
829
830         filename = fDumpPath;
831         sprintf(strbuf, "dump_event-0x%16.16llX-eventdata.bin", evtData.fEventID);
832         filename += strbuf;
833         logfile << "Dumping event data structure to file: " << filename << std::endl;
834         DumpBuffer(&evtData, sizeof(AliHLTComponentEventData), filename.c_str());
835
836         filename = fDumpPath;
837         sprintf(strbuf, "dump_event-0x%16.16llX-triggerdata.bin", evtData.fEventID);
838         filename += strbuf;
839         logfile << "Dumping trigger data structure to file: " << filename << std::endl;
840         DumpBuffer(&trigData, sizeof(AliHLTComponentTriggerData), filename.c_str());
841
842         for (unsigned int n = 0; n < evtData.fBlockCnt; n++)
843         {
844                 sprintf(strbuf, "dump_event-0x%16.16llX-block-0x%8.8X", evtData.fEventID, n);
845                 filename = strbuf;
846                 sprintf(strbuf, "0x%8.8X", blocks[n].fSpecification);
847                 logfile << "Found block with data type = " << DataType2Text(blocks[n].fDataType)
848                         << ", specification = " << strbuf << ". Dumping to file: "
849                         << filename << "-data.bin" << std::endl;
850                 DumpBlock(&blocks[n], filename.c_str());
851         }
852
853         filename = fDumpPath;
854         sprintf(strbuf, "dump_event-0x%16.16llX-output-buffer.bin", evtData.fEventID);
855         filename += strbuf;
856         logfile << "Dumping output buffer to file: " << filename << std::endl;
857         DumpBuffer(outputPtr, size, filename.c_str());
858
859         for (size_t i = 0; i < outputBlocks.size(); i++)
860         {
861                 sprintf(strbuf, "dump_event-0x%16.16llX-output-block-0x%8.8X", evtData.fEventID, int(i));
862                 filename = strbuf;
863                 sprintf(strbuf, "0x%8.8X", outputBlocks[i].fSpecification);
864                 logfile << "Generated output data block with type = "
865                         << DataType2Text(outputBlocks[i].fDataType)
866                         << ", specification = " << strbuf << ". Dumping to file: "
867                         << filename << "-data.bin" << std::endl;
868                 DumpBlock(&outputBlocks[i], filename.c_str());
869         }
870 }
871
872
873 void AliHLTMUONProcessor::DumpEvent(
874                 const AliHLTComponentEventData& evtData,
875                 AliHLTComponentTriggerData& trigData
876         ) const
877 {
878         /// Dumps the event information to files in the dump path given by the
879         /// method DumpPath, which can be set by the command line argument -dumppath.
880
881         using std::fstream;
882         char strbuf[1024];
883
884         std::string filename = fDumpPath;
885         sprintf(strbuf, "dump_event-0x%16.16llX.log", evtData.fEventID);
886         filename += strbuf;
887         fstream logfile(filename.c_str(), fstream::out | fstream::trunc);
888         if (logfile.fail())
889         {
890                 HLTError("Could not open log file '%s' for dump information.", filename.c_str());
891                 return;
892         }
893
894         filename = fDumpPath;
895         sprintf(strbuf, "dump_event-0x%16.16llX-eventdata.bin", evtData.fEventID);
896         filename += strbuf;
897         logfile << "Dumping event data structure to file: " << filename << std::endl;
898         DumpBuffer(&evtData, sizeof(AliHLTComponentEventData), filename.c_str());
899
900         filename = fDumpPath;
901         sprintf(strbuf, "dump_event-0x%16.16llX-triggerdata.bin", evtData.fEventID);
902         filename += strbuf;
903         logfile << "Dumping trigger data structure to file: " << filename << std::endl;
904         DumpBuffer(&trigData, sizeof(AliHLTComponentTriggerData), filename.c_str());
905
906         for (int i = 0; i < GetNumberOfInputBlocks(); i++)
907         {
908                 const AliHLTComponentBlockData* block = GetInputBlock(i);
909                 sprintf(strbuf, "dump_event-0x%16.16llX-block-0x%8.8X", evtData.fEventID, i);
910                 filename = strbuf;
911                 sprintf(strbuf, "0x%8.8X", block->fSpecification);
912                 logfile << "Found block with data type = " << DataType2Text(block->fDataType)
913                         << ", specification = " << strbuf << ". Dumping to file: "
914                         << filename << "-data.bin" << std::endl;
915                 DumpBlock(block, filename.c_str());
916         }
917 }
918