]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/rec/AliHLTOUTDigitReader.cxx
coding rules and class documentation
[u/mrichter/AliRoot.git] / HLT / rec / AliHLTOUTDigitReader.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the                          * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                                                                        *
9 //* Permission to use, copy, modify and distribute this software and its   *
10 //* documentation strictly for non-commercial purposes is hereby granted   *
11 //* without fee, provided that the above copyright notice appears in all   *
12 //* copies and that both the copyright notice and this permission notice   *
13 //* appear in the supporting documentation. The authors make no claims     *
14 //* about the suitability of this software for any purpose. It is          *
15 //* provided "as is" without express or implied warranty.                  *
16 //**************************************************************************
17
18 /// @file   AliHLTOUTDigitReader.cxx
19 /// @author Matthias Richter
20 /// @date   
21 /// @brief  HLTOUT data wrapper for simulated AliRoot HLT digit data.
22 ///
23
24 #include "AliHLTOUTDigitReader.h"
25 #include "AliRawDataHeader.h"
26 #include "AliDAQ.h"
27 #include "TTree.h"
28 #include "TFile.h"
29 #include "TArrayC.h"
30 #include "TBranch.h"
31 #include "TString.h"
32 #include <cassert>
33
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTOUTDigitReader)
36
37 AliHLTOUTDigitReader::AliHLTOUTDigitReader(int event, AliHLTEsdManager* pEsdManager, const char* digitFile)
38   :
39   AliHLTOUTHomerCollection(event, pEsdManager),
40   fDigitFileName(digitFile),
41   fpDigitFile(NULL),
42   fpDigitTree(NULL),
43   fMinDDL(-1),
44   fMaxDDL(-1),
45   fppDigitArrays(NULL),
46   fpEquipments(NULL),
47   fNofDDLs(0),
48   fCurrentLink(-1)
49 {
50   // constructor
51   //
52   // HLTOUT data wrapper for simulated AliRoot HLT digit data
53   // 
54   // see header file for class documentation
55 }
56
57 AliHLTOUTDigitReader::~AliHLTOUTDigitReader()
58 {
59   // destructor
60   CloseTree();
61
62   if (fpDigitFile) {
63     fpDigitFile->Close();
64     delete fpDigitFile;
65     fpDigitFile=NULL;
66   }
67 }
68
69 Bool_t AliHLTOUTDigitReader::ReadNextData(UChar_t*& data)
70 {
71   // overloaded from AliHLTOUTHomerCollection: switch to next DDL
72   if (!fppDigitArrays && (!ReadArrays() || !fppDigitArrays))
73     return kFALSE;
74
75   if (fCurrentLink>=fNofDDLs)
76     return kFALSE;
77
78   while (++fCurrentLink<fNofDDLs) {
79     if (fMinDDL>-1 && fMinDDL>fpEquipments[fCurrentLink]) continue;
80     if (fMaxDDL>-1 && fMaxDDL<fpEquipments[fCurrentLink]) continue;
81     if (fppDigitArrays[fCurrentLink]->GetSize()>(int)sizeof(AliRawDataHeader)) {
82       data=reinterpret_cast<UChar_t*>(fppDigitArrays[fCurrentLink]->GetArray());
83       data+=sizeof(AliRawDataHeader);
84       return kTRUE;
85     }
86   }
87   return kFALSE;
88 }
89
90 int AliHLTOUTDigitReader::Reset()
91 {
92   // overloaded from AliHLTOUTHomerCollection: reset DDL position
93   fCurrentLink=-1;
94   return 0;
95 }
96
97 int AliHLTOUTDigitReader::GetDataSize()
98 {
99   // overloaded from AliHLTOUTHomerCollection: get size of current DDL
100   if (fCurrentLink>=0 && fCurrentLink<fNofDDLs && fppDigitArrays) {
101     return fppDigitArrays[fCurrentLink]->GetSize()-sizeof(AliRawDataHeader);
102   }
103   return 0;
104 }
105
106 const AliRawDataHeader* AliHLTOUTDigitReader::GetDataHeader()
107 {
108   // overloaded from AliHLTOUTHomerCollection: get data header of current DDL
109   if (fCurrentLink>=0 && fCurrentLink<fNofDDLs && fppDigitArrays) {
110     return reinterpret_cast<AliRawDataHeader*>(fppDigitArrays[fCurrentLink]->GetArray());
111   }
112   return NULL;
113 }
114
115 void AliHLTOUTDigitReader::SelectEquipment(int /*equipmentType*/, int minEquipmentId, int maxEquipmentId)
116 {
117   // overloaded from AliHLTOUTHomerCollection: select equipment range
118   fMinDDL=minEquipmentId;
119   fMaxDDL=maxEquipmentId;
120 }
121
122 int AliHLTOUTDigitReader::GetEquipmentId()
123 {
124   // overloaded from AliHLTOUTHomerCollection: get id of current DDL
125   if (fCurrentLink>=0 && fCurrentLink<fNofDDLs && fpEquipments) {
126     return fpEquipments[fCurrentLink];
127   }
128   return -1;
129 }
130
131 bool AliHLTOUTDigitReader::ReadArrays()
132 {
133   // Read the data from the root file and HLTOUT raw tree.
134   // Retrieve the number of branches and allocate arrays acording
135   // to that. After initialization of the arrays and variables, the
136   // event fEnvent is read.
137   bool result=false;
138
139   if (GetCurrentEventNo()<0) {
140     HLTWarning("no event selected, no data available");
141     return false;
142   }
143
144   // 2011-06-06 in order to support AliHLTReconstructor option 'ignore-hltout' for
145   // digits, the file name can be set to NULL, nothing done in that case 
146   if (!fDigitFileName) return false;
147
148   if (!fpDigitFile) {
149     fpDigitFile=new TFile(fDigitFileName);
150   }
151   if (!fpDigitFile) return false;
152   if (fpDigitFile->IsZombie()) return false;
153
154   fpDigitFile->GetObject("rawhltout", fpDigitTree);
155   if (!fpDigitTree) {
156     return false;
157   }
158
159   vector<int> equipments;
160   int iTotal=0;
161   TIter iter(fpDigitTree->GetListOfBranches());
162   while (TBranch* br=(TBranch *)iter.Next()) {
163     iTotal++;
164     TString bname=br->GetName();
165     if (bname.BeginsWith("HLT_") && bname.EndsWith(".ddl")) {
166       bname.ReplaceAll("HLT_", "");
167       bname.ReplaceAll(".ddl", "");
168       if (bname.IsDigit()) {
169         int equipment=bname.Atoi();
170         int index=equipment-AliDAQ::DdlIDOffset("HLT");
171         if (index>=0 && strcmp(br->GetName(), AliDAQ::DdlFileName("HLT", index))==0) {
172           equipments.push_back(equipment);
173         } else {
174           HLTWarning("equipment mismatch: skipping id %d (out of range of HLT equipments)", equipment);
175         }
176       }
177     }
178   }
179   HLTDebug("found %d HLT branche(s) out of %d", equipments.size(), iTotal);
180
181   if (equipments.size()>0) {
182     fNofDDLs=equipments.size();
183     fpEquipments=new int[fNofDDLs];
184     fppDigitArrays=new TArrayC*[fNofDDLs];
185     if (fpEquipments && fppDigitArrays) {
186       memcpy(fpEquipments, &equipments[0], fNofDDLs*sizeof(int));
187       int i=0;
188       for (i=0; i<fNofDDLs; i++) {
189         fppDigitArrays[i]=NULL;
190         fpDigitTree->SetBranchAddress(AliDAQ::DdlFileName("HLT", fpEquipments[i]-AliDAQ::DdlIDOffset("HLT")), &fppDigitArrays[i]);
191       }
192       fpDigitTree->GetEvent(GetCurrentEventNo());
193       for (i=0; i<fNofDDLs; i++) {
194         if (fppDigitArrays[i]) {
195           HLTDebug("branch %s: %d byte(s)", AliDAQ::DdlFileName("HLT", fpEquipments[i]-AliDAQ::DdlIDOffset("HLT")), fppDigitArrays[i]);
196         }
197       }
198       result=true;
199     } else {
200       fNofDDLs=0;
201     }
202   }
203   return result;
204 }
205
206 int AliHLTOUTDigitReader::CloseTree()
207 {
208   // Cleanup tree and data arrays.
209   int iResult=0;
210   for (int i=0; i<fNofDDLs; i++) {
211     if (fppDigitArrays[i]) delete fppDigitArrays[i];
212     fppDigitArrays[i]=NULL;
213   }
214   if (fppDigitArrays) delete[] fppDigitArrays;
215   fppDigitArrays=NULL;
216   if (fpEquipments) delete[] fpEquipments;
217   fpEquipments=NULL;
218   if (fpDigitTree) delete fpDigitTree;
219   fpDigitTree=NULL;
220   fNofDDLs=0;
221   fCurrentLink=-1;
222
223   return iResult;
224 }
225
226 void AliHLTOUTDigitReader::SetParam(TTree* /*pDigitTree*/, int event)
227 {
228   // set parameter for this HLTOUT instance
229   // The function is for internal use only in conjunction with the
230   // AliHLTOUT::New() functions.
231
232   // TODO: here we have the implemented to correct loading of
233   // the digit file from the run loader. At time of writing
234   // (Jun 2008) the AliLoader for the HLT is missing in the AliRoot
235   // framework.
236   fEvent=event;
237 }
238
239 void AliHLTOUTDigitReader::SetParam(const char* filename, int event)
240 {
241   // set parameter for this HLTOUT instance
242   // The function is for internal use only in conjunction with the
243   // AliHLTOUT::New() functions.
244
245   if (filename && filename[0]!=0) {
246     fDigitFileName=filename;
247   } else {
248     HLTWarning("no valid digit file provided, using default file %s", fDigitFileName.Data());
249   }
250   fEvent=event;
251 }