Introduce new NtimeBin consistency checks
[u/mrichter/AliRoot.git] / TRD / AliTRDReconstructor.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // Class for TRD reconstruction                                              //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <TObjString.h>
25 #include <TObjArray.h>
26 #include <TTreeStream.h>
27 #include <TDirectory.h>
28
29 #include "AliRawReader.h"
30
31 #include "AliTRDReconstructor.h"
32 #include "AliTRDclusterizer.h"
33 #include "AliTRDrawData.h"
34 #include "AliTRDrawStreamBase.h"
35 #include "AliTRDdigitsManager.h"
36 #include "AliTRDtrackerV1.h"
37
38 #define SETFLG(n,f) ((n) |= f)
39 #define CLRFLG(n,f) ((n) &= ~f)
40
41 ClassImp(AliTRDReconstructor)
42
43 TClonesArray *AliTRDReconstructor::fgClusters = NULL;
44 TClonesArray *AliTRDReconstructor::fgTracklets = NULL;
45 AliTRDdigitsParam *AliTRDReconstructor::fgDigitsParam = NULL;
46 Char_t const * AliTRDReconstructor::fgSteerNames[kNsteer] = {
47   "DigitsConversion       "
48  ,"Write Clusters         "
49  ,"Write Online Tracklets "
50  ,"Stand Alone Tracking   "
51  ,"HLT Mode               "
52  ,"Process Online Tracklets"
53  ,"Debug Streaming        "
54 };
55 Char_t const * AliTRDReconstructor::fgSteerFlags[kNsteer] = {
56   "dc"// digits conversion [false]
57  ,"cw"// write clusters [true]
58  ,"tw"// write online tracklets [false]
59  ,"sa"// track seeding (stand alone tracking) [true]
60  ,"hlt"// HLT reconstruction [false]
61  ,"tp"// also use online tracklets for reconstruction [false]
62  ,"deb"// Write debug stream [false]
63 };
64 Char_t const * AliTRDReconstructor::fgTaskNames[AliTRDrecoParam::kTRDreconstructionTasks] = {
65   "Clusterizer"
66  ,"Tracker"
67  ,"PID"
68 };
69 Char_t const * AliTRDReconstructor::fgTaskFlags[AliTRDrecoParam::kTRDreconstructionTasks] = {
70   "cl"
71  ,"tr"
72  ,"pd"
73 };
74 Int_t AliTRDReconstructor::fgNTimeBins = -1;
75
76 //_____________________________________________________________________________
77 AliTRDReconstructor::AliTRDReconstructor()
78   :AliReconstructor()
79   ,fSteerParam(0)
80 {
81   // setting default "ON" steering parameters
82   // owner of debug streamers 
83   SETFLG(fSteerParam, kOwner);
84   // write clusters [cw]
85   SETFLG(fSteerParam, kWriteClusters);
86   // track seeding (stand alone tracking) [sa]
87   SETFLG(fSteerParam, kSeeding);
88
89   memset(fDebugStream, 0, sizeof(TTreeSRedirector *) * AliTRDrecoParam::kTRDreconstructionTasks);
90 }
91
92 //_____________________________________________________________________________
93 AliTRDReconstructor::~AliTRDReconstructor()
94 {
95   //
96   // Destructor
97   //
98
99   if(fgDigitsParam){
100     delete fgDigitsParam;
101     fgDigitsParam = NULL;
102   }
103   if(fgClusters) {
104     fgClusters->Delete();
105     delete fgClusters;
106     fgClusters = NULL;
107   }
108   if(fgTracklets) {
109     fgTracklets->Delete();
110     delete fgTracklets;
111     fgTracklets = NULL;
112   }
113   if(fSteerParam&kOwner){
114     for(Int_t itask = 0; itask < AliTRDrecoParam::kTRDreconstructionTasks; itask++)
115       if(fDebugStream[itask]) delete fDebugStream[itask];
116   }
117 }
118
119
120 //_____________________________________________________________________________
121 void AliTRDReconstructor::Init(){
122   //
123   // Init Options
124   //
125   SetOption(GetOption());
126   Options(fSteerParam);
127
128   // Make Debug Streams when Debug Streaming
129   if(IsDebugStreaming()){
130     for(Int_t task = 0; task < AliTRDrecoParam::kTRDreconstructionTasks; task++){
131       TDirectory *savedir = gDirectory;
132       fDebugStream[task] = new TTreeSRedirector(Form("TRD.Debug%s.root", fgTaskNames[task]));
133       savedir->cd();
134       SETFLG(fSteerParam, kOwner);
135     }
136   }
137 }
138
139 //_____________________________________________________________________________
140 void AliTRDReconstructor::ConvertDigits(AliRawReader *rawReader
141               , TTree *digitsTree) const
142 {
143   //
144   // Convert raw data digits into digit objects in a root tree
145   //
146
147   //AliInfo("Convert raw data digits into digit objects [RawReader -> Digit TTree]");
148
149   AliTRDrawData rawData;
150   rawReader->Reset();
151   rawReader->Select("TRD");
152   rawData.OpenOutput();
153   AliTRDrawStreamBase::SetRawStreamVersion(GetRecoParam()->GetRawStreamVersion()->Data());
154   AliTRDdigitsManager *manager = rawData.Raw2Digits(rawReader);
155   manager->MakeBranch(digitsTree);
156   manager->WriteDigits();
157   delete manager;
158
159 }
160
161 //_____________________________________________________________________________
162 void AliTRDReconstructor::Reconstruct(AliRawReader *rawReader
163                                     , TTree *clusterTree) const
164 {
165   //
166   // Reconstruct clusters
167   //
168
169   //AliInfo("Reconstruct TRD clusters from RAW data [RawReader -> Cluster TTree]");
170
171
172   rawReader->Reset();
173   rawReader->Select("TRD");
174   AliTRDrawStreamBase::SetRawStreamVersion(GetRecoParam()->GetRawStreamVersion()->Data());
175
176   // New (fast) cluster finder
177   AliTRDclusterizer clusterer(fgTaskNames[AliTRDrecoParam::kClusterizer], fgTaskNames[AliTRDrecoParam::kClusterizer]);
178   clusterer.SetReconstructor(this);
179   clusterer.OpenOutput(clusterTree);
180   clusterer.OpenTrackletOutput();
181   clusterer.SetUseLabels(kFALSE);
182   clusterer.Raw2ClustersChamber(rawReader);
183   
184   if(IsWritingClusters()) return;
185
186   // take over ownership of clusters
187   fgClusters = clusterer.RecPoints();
188   clusterer.SetClustersOwner(kFALSE);
189
190   // take over ownership of online tracklets
191   fgTracklets = clusterer.TrackletsArray();
192   clusterer.SetTrackletsOwner(kFALSE);
193
194   fgNTimeBins = clusterer.GetNTimeBins();
195 }
196
197 //_____________________________________________________________________________
198 void AliTRDReconstructor::Reconstruct(TTree *digitsTree
199                                     , TTree *clusterTree) const
200 {
201   //
202   // Reconstruct clusters
203   //
204
205   //AliInfo("Reconstruct TRD clusters from Digits [Digit TTree -> Cluster TTree]");
206   
207   AliTRDclusterizer clusterer(fgTaskNames[AliTRDrecoParam::kClusterizer], fgTaskNames[AliTRDrecoParam::kClusterizer]);
208   clusterer.SetReconstructor(this);
209   clusterer.OpenOutput(clusterTree);
210   clusterer.ReadDigits(digitsTree);
211   clusterer.MakeClusters();
212
213   if(IsWritingClusters()) return;
214
215   // take over ownership of clusters
216   fgClusters = clusterer.RecPoints();
217   clusterer.SetClustersOwner(kFALSE);
218
219   // take over ownership of online tracklets
220   fgTracklets = clusterer.TrackletsArray();
221   clusterer.SetTrackletsOwner(kFALSE);
222
223   fgNTimeBins = clusterer.GetNTimeBins();
224 }
225
226 //_____________________________________________________________________________
227 AliTracker *AliTRDReconstructor::CreateTracker() const
228 {
229   //
230   // Create a TRD tracker
231   //
232
233   //return new AliTRDtracker(NULL);
234   AliTRDtrackerV1 *tracker = new AliTRDtrackerV1();
235   tracker->SetReconstructor(this);
236   return tracker;
237
238 }
239
240 //_____________________________________________________________________________
241 void AliTRDReconstructor::FillESD(TTree* /*digitsTree*/
242         , TTree* /*clusterTree*/
243         , AliESDEvent* /*esd*/) const
244 {
245   //
246   // Fill ESD
247   //
248
249 }
250
251 //_____________________________________________________________________________
252 void AliTRDReconstructor::SetDigitsParam(AliTRDdigitsParam const *par)
253
254   if(fgDigitsParam) (*fgDigitsParam) = (*par);
255   else fgDigitsParam = new AliTRDdigitsParam(*par);
256 }
257
258
259 //_____________________________________________________________________________
260 void AliTRDReconstructor::SetOption(Option_t *opt)
261 {
262   //
263   // Read option string into the steer param.
264   //
265
266   AliReconstructor::SetOption(opt);
267
268   TString s(opt);
269   TObjArray *opar = s.Tokenize(",");
270   for(Int_t ipar=0; ipar<opar->GetEntriesFast(); ipar++){
271     Bool_t processed = kFALSE;
272     TString sopt(((TObjString*)(*opar)[ipar])->String());
273     for(Int_t iopt=0; iopt<kNsteer; iopt++){
274       if(!sopt.Contains(fgSteerFlags[iopt])) continue;
275       SETFLG(fSteerParam, BIT(iopt));
276       if(sopt.Contains("!")) CLRFLG(fSteerParam, BIT(iopt));
277       processed = kTRUE;
278       break;    
279     }
280     if(processed) continue;
281
282     AliWarning(Form("Unknown option flag %s.", sopt.Data()));
283   }
284 }
285
286 //_____________________________________________________________________________
287 void AliTRDReconstructor::Options(UInt_t steer)
288 {
289   //
290   // Print the options
291   //
292
293   for(Int_t iopt=0; iopt<kNsteer; iopt++){
294     AliDebugGeneral("AliTRDReconstructor", 1, Form(" %s[%s]%s", fgSteerNames[iopt], fgSteerFlags[iopt], steer ?(((steer>>iopt)&1)?" : ON":" : OFF"):""));
295   }
296 }
297