]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDCalibHistoComponent.cxx
reduced size of TRD cluster for data exchange, make use of new speed improvements...
[u/mrichter/AliRoot.git] / HLT / TRD / AliHLTTRDCalibHistoComponent.cxx
1 // $Id: AliHLTTRDCalibHistoComponent.cxx 40282 2010-04-09 13:29:10Z richterm $
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors:                                                               *
7  *          for The ALICE HLT Project.                                    *
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   AliHLTTRDCalibHistoComponent.cxx
19 //  @author Theodor Rascanu
20 //  @date   25.04.2010
21 //  @brief  A TRDCalibration histogramming component for the HLT. 
22 // 
23
24 #if __GNUC__ >= 3
25 using namespace std;
26 #endif
27
28 #include "TTree.h"
29 #include "TFile.h"
30 #include "TBranch.h"
31 #include "TH2I.h"
32 #include "TH2.h"
33 #include "TProfile2D.h"
34
35 #include "AliHLTReadoutList.h"
36
37 #include "AliHLTTRDCalibHistoComponent.h"
38 #include "AliHLTTRDDefinitions.h"
39 #include "AliHLTTRDUtils.h"
40
41 #include "AliCDBManager.h"
42 #include "AliCDBStorage.h"
43 #include "AliCDBEntry.h"
44 #include "AliRawReaderMemory.h"
45
46 #include "AliTRDCalPad.h"
47 #include "AliTRDCalDet.h"
48
49 #include "AliTRDCalibraFillHisto.h"
50 #include "AliTRDtrackV1.h"
51
52 #include "AliTRDCalibraFit.h"
53 #include "AliTRDCalibraMode.h"
54 #include "AliTRDCalibraVector.h"
55 #include "AliTRDCalibraVdriftLinearFit.h"
56 #include "AliTRDReconstructor.h"
57 #include "AliTRDrecoParam.h"
58
59 #include <cstdlib>
60 #include <cerrno>
61 #include <string>
62
63 ClassImp(AliHLTTRDCalibHistoComponent);
64
65 AliHLTTRDCalibHistoComponent::AliHLTTRDCalibHistoComponent()
66   : AliHLTProcessor(),
67     fOutputSize(500000),
68     fSpec(0),
69     fTracksArray(NULL),
70     fOutArray(NULL),
71     fTRDCalibraFillHisto(NULL),
72     fSavedTimeBins(kFALSE),
73     fTrgStrings(NULL),
74     fAccRejTrg(0),
75     fMinClusters(0),
76     fMinTracklets(0),
77     fTakeAllEvents(kFALSE)
78 {
79   // Default constructor
80 }
81
82 AliHLTTRDCalibHistoComponent::~AliHLTTRDCalibHistoComponent()
83 {
84   // Destructor
85 }
86
87 const char* AliHLTTRDCalibHistoComponent::GetComponentID()
88 {
89   // Return the component ID const char *
90   return "TRDCalibHisto"; // The ID of this component
91 }
92
93 void AliHLTTRDCalibHistoComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
94 {
95   // Get the list of input data
96   list.clear(); // We do not have any requirements for our input data type(s).
97   list.push_back(AliHLTTRDDefinitions::fgkTracksDataType);
98 }
99
100 AliHLTComponentDataType AliHLTTRDCalibHistoComponent::GetOutputDataType()
101 {
102   // Get the output data type
103   return kAliHLTMultipleDataType;
104   //  return AliHLTTRDDefinitions::fgkCalibrationDataType;
105  
106 }
107
108 int AliHLTTRDCalibHistoComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
109 {
110   // Get the output data type
111   tgtList.clear();
112   tgtList.push_back(AliHLTTRDDefinitions::fgkCalibrationDataType);
113   return tgtList.size();
114 }
115
116 void AliHLTTRDCalibHistoComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
117 {
118   // Get the output data size
119   constBase = fOutputSize;
120   inputMultiplier = 0;
121 }
122
123 AliHLTComponent* AliHLTTRDCalibHistoComponent::Spawn()
124 {
125   // Spawn function, return new instance of this class
126   return new AliHLTTRDCalibHistoComponent;
127 };
128
129 int AliHLTTRDCalibHistoComponent::DoInit( int argc, const char** argv )
130 {
131   int iResult=0;
132   if(fTrgStrings)
133     delete fTrgStrings;
134   fTrgStrings = new TObjArray();
135   
136   TString configuration="";
137   TString argument="";
138   for (int i=0; i<argc && iResult>=0; i++) {
139     argument=argv[i];
140     if (!configuration.IsNull()) configuration+=" ";
141     configuration+=argument;
142   }
143
144   if (!configuration.IsNull()) {
145     iResult=Configure(configuration.Data());
146   } else {
147     iResult=Reconfigure(NULL, NULL);
148   }
149   if(iResult>=0){
150     iResult=SetParams();
151   }
152   return iResult;
153 }
154
155 int AliHLTTRDCalibHistoComponent::Configure(const char* arguments){
156   int iResult=0;
157   if (!arguments) return iResult;
158   
159   TString allArgs=arguments;
160   TString argument;
161   int bMissingParam=0;
162
163   TObjArray* pTokens=allArgs.Tokenize(" ");
164   if (pTokens) {
165     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
166       argument=((TObjString*)pTokens->At(i))->GetString();
167       if (argument.IsNull()) continue;
168       
169       if (argument.CompareTo("output_size")==0) {
170         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
171         HLTInfo("Setting output size to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
172         fOutputSize=((TObjString*)pTokens->At(i))->GetString().Atoi();
173         continue;
174       } 
175       else if (argument.CompareTo("-minClusters")==0) {
176         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
177         HLTInfo("Setting minCusters to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
178         fMinClusters=((TObjString*)pTokens->At(i))->GetString().Atoi();
179         continue;
180       } 
181       else if (argument.CompareTo("-minTracklets")==0) {
182         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
183         HLTInfo("Setting minTracklets to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
184         fMinTracklets=((TObjString*)pTokens->At(i))->GetString().Atoi();
185         continue;
186       } 
187       else if (argument.CompareTo("-TrgStr")==0) {
188         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
189         HLTInfo("Select TrgStr: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
190         fTrgStrings->Add(new TObjString(((TObjString*)pTokens->At(i))->GetString().Data()));
191         continue;
192       } 
193       else if (argument.CompareTo("-acceptTrgStr")==0) {
194         fAccRejTrg=1;
195         HLTInfo("Accept selected Trigger Strings only");
196         continue;
197       }
198       else if (argument.CompareTo("-rejectTrgStr")==0) {
199         fAccRejTrg=-1;
200         HLTInfo("Reject all selected Trigger Strings");
201         continue;
202       }
203       else if (argument.CompareTo("-takeAllEvents")==0) {
204         fAccRejTrg=0;
205         fTakeAllEvents = kTRUE;
206         HLTInfo("Take all events independently of the trigger strings");
207         continue;
208       }
209       
210       else {
211         HLTError("unknown argument: %s", argument.Data());
212         iResult=-EINVAL;
213         break;
214       }
215     }
216     delete pTokens;
217   }
218   if (bMissingParam) {
219     HLTError("missing parameter for argument %s", argument.Data());
220     iResult=-EINVAL;
221   }
222   return iResult;
223 }
224
225 int AliHLTTRDCalibHistoComponent::SetParams()
226 {
227
228   if(!fTrgStrings)
229     fTrgStrings = new TObjArray();
230
231   if(!AliCDBManager::Instance()->IsDefaultStorageSet()){
232     HLTError("DefaultStorage is not set in CDBManager");
233     return -EINVAL;
234   }
235   if(AliCDBManager::Instance()->GetRun()<0){
236     HLTError("Run Number is not set in CDBManager");
237     return -EINVAL;
238   }
239   HLTInfo("CDB default storage: %s; RunNo: %i", (AliCDBManager::Instance()->GetDefaultStorage()->GetBaseFolder()).Data(), AliCDBManager::Instance()->GetRun());
240
241   if(fTrgStrings->GetEntriesFast()>0 && !fAccRejTrg){
242     HLTError("Trigger string(s) given, but acceptTrgStr or rejectTrgStr not selected");
243     return -EINVAL;
244   }
245
246   fTRDCalibraFillHisto = AliTRDCalibraFillHisto::Instance();
247   fTRDCalibraFillHisto->SetIsHLT(kTRUE);
248   fTRDCalibraFillHisto->SetHisto2d(); // choose to use histograms
249   fTRDCalibraFillHisto->SetCH2dOn();  // choose to calibrate the gain
250   fTRDCalibraFillHisto->SetPH2dOn();  // choose to calibrate the drift velocity
251   fTRDCalibraFillHisto->SetPRF2dOn(); // choose to look at the PRF
252   fTRDCalibraFillHisto->SetIsHLT(); // per detector
253   //fTRDCalibraFillHisto->SetDebugLevel(1);// debug
254   fTRDCalibraFillHisto->SetFillWithZero(kTRUE);
255   fTRDCalibraFillHisto->SetLinearFitterOn(kTRUE);
256   fTRDCalibraFillHisto->SetNumberBinCharge(100);
257   
258   if(!fTracksArray) fTracksArray = new TClonesArray("AliTRDtrackV1");
259   if(!fOutArray)fOutArray = new TObjArray(4);
260
261   HLTDebug("run SetupCTPData");
262   SetupCTPData();
263
264   return 0;
265 }
266
267 int AliHLTTRDCalibHistoComponent::DoDeinit()
268 {
269   
270   // Deinitialization of the component
271   
272   HLTDebug("DeinitCalibration");
273   delete fTracksArray; fTracksArray=0;
274   //fTRDCalibraFillHisto->Destroy();
275   //fOutArray->Delete();
276   delete fOutArray; fOutArray=0;
277   fTrgStrings->Delete();
278   delete fTrgStrings; fTrgStrings=0;
279   return 0;
280 }
281
282 Int_t AliHLTTRDCalibHistoComponent::DoEvent(const AliHLTComponent_EventData& /*evtData*/,
283                                             const AliHLTComponent_BlockData* /*blocks*/,
284                                             AliHLTComponent_TriggerData& /*trigData*/,
285                                             AliHLTUInt8_t* /*outputPtr*/,
286                                             AliHLTUInt32_t& /*size*/,
287                                             vector<AliHLTComponent_BlockData>& /*outputBlocks*/)
288 {
289   // Process an event
290  
291   TClonesArray* TCAarray[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
292   Int_t usedEntries = 0;
293   Int_t blockOrObject = 0;
294   Int_t nTimeBins = -1;
295
296   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(AliHLTTRDDefinitions::fgkTracksDataType); pBlock; pBlock=GetNextInputBlock()) 
297     {
298       TCAarray[0] = fTracksArray;
299       AliHLTTRDUtils::ReadTracks(TCAarray[0], pBlock->fPtr, pBlock->fSize, &nTimeBins);
300       fSpec |= pBlock->fSpecification;
301       usedEntries = 1;
302       blockOrObject = -1;
303     }  
304
305   for(const TObject *iter = GetFirstInputObject(AliHLTTRDDefinitions::fgkHiLvlTracksDataType); iter; iter = GetNextInputObject()) 
306     {
307       if(blockOrObject<0){
308         HLTError("You may not mix high level and low level!");
309         return -1;
310       }
311
312       TCAarray[usedEntries] = dynamic_cast<TClonesArray*>(const_cast<TObject*>(iter));
313       if(TCAarray[usedEntries])continue;
314       TObjString* strg = dynamic_cast<TObjString*>(const_cast<TObject*>(GetNextInputObject()));
315       if(!strg)continue;
316
317       nTimeBins = strg->String().Atoi();
318       fSpec |= GetSpecification(iter);
319       usedEntries++;
320       blockOrObject = 1;
321     }
322
323   if(!blockOrObject)
324     return 0;
325
326   if(!fSavedTimeBins){
327     if(nTimeBins<0){
328       HLTFatal("Number of timebins is negative!");
329       return -1;
330     }
331     HLTDebug("Saving number of time bins which was read from input block. Value is: %d", nTimeBins);
332     fTRDCalibraFillHisto->Init2Dhistos(nTimeBins); // initialise the histos
333     fTRDCalibraFillHisto->SetNumberClusters(fMinClusters); // At least fMinClusters clusters
334     fTRDCalibraFillHisto->SetNumberClustersf(nTimeBins); // Not more than %d  clusters
335     fSavedTimeBins=kTRUE;
336   }
337
338   Bool_t bTriggerPassed = fTakeAllEvents;
339
340   if(fAccRejTrg){
341     if(fAccRejTrg>0){
342       bTriggerPassed=kFALSE;
343       for(int i = 0; i < fTrgStrings->GetEntriesFast(); i++){
344         const TObjString *const obString=(TObjString*)fTrgStrings->At(i);
345         const TString tString=obString->GetString();
346         if(CheckCTPTrigger(tString.Data())>0){bTriggerPassed=kTRUE; break;}
347       }
348     }
349     else{
350       bTriggerPassed=kTRUE;
351       for(int i = 0; i < fTrgStrings->GetEntriesFast(); i++){
352         const TObjString *const obString=(TObjString*)fTrgStrings->At(i);
353         const TString tString=obString->GetString();
354         if(CheckCTPTrigger(tString.Data())>0){bTriggerPassed=kFALSE; break;}
355       }
356     }
357   }
358   
359   fTRDCalibraFillHisto->SetCH2dOn(bTriggerPassed);
360   fTRDCalibraFillHisto->SetPH2dOn(bTriggerPassed);
361   for(int i=0; i<usedEntries; i++){
362     const TClonesArray *const inArr = TCAarray[i];
363     Int_t nbEntries = inArr->GetEntries();
364     HLTDebug(" %i TRDtracks in tracksArray", nbEntries);
365     AliTRDtrackV1* trdTrack = 0x0;
366     for (Int_t ii = 0; ii < nbEntries; ii++){
367       HLTDebug("%i/%i: ", ii+1, nbEntries);
368       trdTrack = (AliTRDtrackV1*)inArr->At(ii);
369       if(trdTrack->GetNumberOfTracklets()<fMinTracklets)continue;
370       fTRDCalibraFillHisto->UpdateHistogramsV1(trdTrack);
371       // for(int i3=0; i3<7; i3++)
372       //   if(trdTrack->GetTracklet(i3))trdTrack->GetTracklet(i3)->Bootstrap(fReconstructor);
373     }
374   }
375
376   if(!fOutArray->At(0))FormOutput();
377   PushBack(fOutArray, AliHLTTRDDefinitions::fgkCalibrationDataType, fSpec);
378
379   return 0;
380 }
381
382 /**
383  * Form output array of histrograms
384  */
385 //============================================================================
386 void AliHLTTRDCalibHistoComponent::FormOutput()
387 {
388   // gain histo
389   TH2I *hCH2d = fTRDCalibraFillHisto->GetCH2d();
390   fOutArray->Add(hCH2d);
391   
392   // drift velocity histo
393   TProfile2D *hPH2d = fTRDCalibraFillHisto->GetPH2d();
394   fOutArray->Add(hPH2d);
395   
396   // PRF histo
397   TProfile2D *hPRF2d = fTRDCalibraFillHisto->GetPRF2d();
398   fOutArray->Add(hPRF2d);
399   
400   // Vdrift Linear Fit
401   AliTRDCalibraVdriftLinearFit *hVdriftLinearFitOne=(AliTRDCalibraVdriftLinearFit *)fTRDCalibraFillHisto->GetVdriftLinearFit();
402   fOutArray->Add(hVdriftLinearFitOne);
403   
404   HLTDebug("GetCH2d = 0x%x; NEntries = %i; size = %i", hCH2d, hCH2d->GetEntries(), sizeof(*hCH2d));
405   hCH2d->Print();
406   HLTDebug("GetPH2d = 0x%x; NEntries = %i; size = %i", hPH2d, hPH2d->GetEntries(), sizeof(*hPH2d));
407   hPH2d->Print();
408   HLTDebug("GetPRF2d = 0x%x; NEntries = %i; size = %i", hPRF2d, hPRF2d->GetEntries(), sizeof(*hPRF2d));
409   hPRF2d->Print();
410   HLTDebug("GetVdriftLinearFit = 0x%x; size = %i", hVdriftLinearFitOne, sizeof(hVdriftLinearFitOne)); 
411   
412   HLTDebug("output Array: pointer = 0x%x; NEntries = %i; size = %i", fOutArray, fOutArray->GetEntries(), sizeof(fOutArray));
413    
414 }
415
416 int AliHLTTRDCalibHistoComponent::Reconfigure(const char* cdbEntry, const char* chainId)
417 {
418   // see header file for class documentation
419
420   int iResult=0;
421   const char* path="HLT/ConfigTRD/CalibHistoComponent";
422   const char* defaultNotify="";
423   if (cdbEntry) {
424     path=cdbEntry;
425     defaultNotify=" (default)";
426   }
427   if (path) {
428     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
429     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
430     if (pEntry) {
431       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
432       if (pString) {
433         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
434         iResult=Configure(pString->GetString().Data());
435       } else {
436         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
437       }
438     } else {
439       HLTError("cannot fetch object \"%s\" from CDB", path);
440     }
441   }
442
443   return iResult;
444 }