]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TRD/AliHLTTRDCalibHistoComponent.cxx
removing the CTPData from the HLTGlobalTrigger decision because of bug #88431 until...
[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->DestroyDebugStreamer();
275   //fTRDCalibraFillHisto->Destroy();
276   //fOutArray->Delete();
277   delete fOutArray; fOutArray=0;
278   fTrgStrings->Delete();
279   delete fTrgStrings; fTrgStrings=0;
280   return 0;
281 }
282
283 Int_t AliHLTTRDCalibHistoComponent::DoEvent(const AliHLTComponent_EventData& /*evtData*/,
284                                             const AliHLTComponent_BlockData* /*blocks*/,
285                                             AliHLTComponent_TriggerData& /*trigData*/,
286                                             AliHLTUInt8_t* /*outputPtr*/,
287                                             AliHLTUInt32_t& /*size*/,
288                                             vector<AliHLTComponent_BlockData>& /*outputBlocks*/)
289 {
290   // Process an event
291  
292   TClonesArray* TCAarray[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
293   Int_t usedEntries = 0;
294   Int_t blockOrObject = 0;
295   Int_t nTimeBins = -1;
296
297   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(AliHLTTRDDefinitions::fgkTracksDataType); pBlock; pBlock=GetNextInputBlock()) 
298     {
299       TCAarray[0] = fTracksArray;
300       AliHLTTRDUtils::ReadTracks(TCAarray[0], pBlock->fPtr, pBlock->fSize, &nTimeBins);
301       fSpec |= pBlock->fSpecification;
302       usedEntries = 1;
303       blockOrObject = -1;
304     }  
305
306   for(const TObject *iter = GetFirstInputObject(AliHLTTRDDefinitions::fgkHiLvlTracksDataType); iter; iter = GetNextInputObject()) 
307     {
308       if(blockOrObject<0){
309         HLTError("You may not mix high level and low level!");
310         return -1;
311       }
312
313       TCAarray[usedEntries] = dynamic_cast<TClonesArray*>(const_cast<TObject*>(iter));
314       if(TCAarray[usedEntries])continue;
315       TObjString* strg = dynamic_cast<TObjString*>(const_cast<TObject*>(GetNextInputObject()));
316       if(!strg)continue;
317
318       nTimeBins = strg->String().Atoi();
319       fSpec |= GetSpecification(iter);
320       usedEntries++;
321       blockOrObject = 1;
322     }
323
324   if(!blockOrObject)
325     return 0;
326
327   if(!fSavedTimeBins){
328     if(nTimeBins<0){
329       HLTFatal("Number of timebins is negative!");
330       return -1;
331     }
332     HLTDebug("Saving number of time bins which was read from input block. Value is: %d", nTimeBins);
333     fTRDCalibraFillHisto->Init2Dhistos(nTimeBins); // initialise the histos
334     fTRDCalibraFillHisto->SetNumberClusters(fMinClusters); // At least fMinClusters clusters
335     fTRDCalibraFillHisto->SetNumberClustersf(nTimeBins); // Not more than %d  clusters
336     fSavedTimeBins=kTRUE;
337   }
338
339   Bool_t bTriggerPassed = fTakeAllEvents;
340
341   if(fAccRejTrg){
342     if(fAccRejTrg>0){
343       bTriggerPassed=kFALSE;
344       for(int i = 0; i < fTrgStrings->GetEntriesFast(); i++){
345         const TObjString *const obString=(TObjString*)fTrgStrings->At(i);
346         const TString tString=obString->GetString();
347         if(CheckCTPTrigger(tString.Data())>0){bTriggerPassed=kTRUE; break;}
348       }
349     }
350     else{
351       bTriggerPassed=kTRUE;
352       for(int i = 0; i < fTrgStrings->GetEntriesFast(); i++){
353         const TObjString *const obString=(TObjString*)fTrgStrings->At(i);
354         const TString tString=obString->GetString();
355         if(CheckCTPTrigger(tString.Data())>0){bTriggerPassed=kFALSE; break;}
356       }
357     }
358   }
359   
360   fTRDCalibraFillHisto->SetCH2dOn(bTriggerPassed);
361   fTRDCalibraFillHisto->SetPH2dOn(bTriggerPassed);
362   for(int i=0; i<usedEntries; i++){
363     const TClonesArray *const inArr = TCAarray[i];
364     Int_t nbEntries = inArr->GetEntries();
365     HLTDebug(" %i TRDtracks in tracksArray", nbEntries);
366     AliTRDtrackV1* trdTrack = 0x0;
367     for (Int_t ii = 0; ii < nbEntries; ii++){
368       HLTDebug("%i/%i: ", ii+1, nbEntries);
369       trdTrack = (AliTRDtrackV1*)inArr->At(ii);
370       if(trdTrack->GetNumberOfTracklets()<fMinTracklets)continue;
371       fTRDCalibraFillHisto->UpdateHistogramsV1(trdTrack);
372       // for(int i3=0; i3<7; i3++)
373       //   if(trdTrack->GetTracklet(i3))trdTrack->GetTracklet(i3)->Bootstrap(fReconstructor);
374     }
375   }
376
377   if(!fOutArray->At(0))FormOutput();
378   PushBack(fOutArray, AliHLTTRDDefinitions::fgkCalibrationDataType, fSpec);
379
380   if(blockOrObject<0){
381     TCAarray[0]->Delete();
382   }
383
384   return 0;
385 }
386
387 /**
388  * Form output array of histrograms
389  */
390 //============================================================================
391 void AliHLTTRDCalibHistoComponent::FormOutput()
392 {
393   // gain histo
394   TH2I *hCH2d = fTRDCalibraFillHisto->GetCH2d();
395   fOutArray->Add(hCH2d);
396   
397   // drift velocity histo
398   TProfile2D *hPH2d = fTRDCalibraFillHisto->GetPH2d();
399   fOutArray->Add(hPH2d);
400   
401   // PRF histo
402   TProfile2D *hPRF2d = fTRDCalibraFillHisto->GetPRF2d();
403   fOutArray->Add(hPRF2d);
404   
405   // Vdrift Linear Fit
406   AliTRDCalibraVdriftLinearFit *hVdriftLinearFitOne=(AliTRDCalibraVdriftLinearFit *)fTRDCalibraFillHisto->GetVdriftLinearFit();
407   fOutArray->Add(hVdriftLinearFitOne);
408   
409   HLTDebug("GetCH2d = 0x%x; NEntries = %i; size = %i", hCH2d, hCH2d->GetEntries(), sizeof(*hCH2d));
410   hCH2d->Print();
411   HLTDebug("GetPH2d = 0x%x; NEntries = %i; size = %i", hPH2d, hPH2d->GetEntries(), sizeof(*hPH2d));
412   hPH2d->Print();
413   HLTDebug("GetPRF2d = 0x%x; NEntries = %i; size = %i", hPRF2d, hPRF2d->GetEntries(), sizeof(*hPRF2d));
414   hPRF2d->Print();
415   HLTDebug("GetVdriftLinearFit = 0x%x; size = %i", hVdriftLinearFitOne, sizeof(hVdriftLinearFitOne)); 
416   
417   HLTDebug("output Array: pointer = 0x%x; NEntries = %i; size = %i", fOutArray, fOutArray->GetEntries(), sizeof(fOutArray));
418    
419 }
420
421 int AliHLTTRDCalibHistoComponent::Reconfigure(const char* cdbEntry, const char* chainId)
422 {
423   // see header file for class documentation
424
425   int iResult=0;
426   const char* path="HLT/ConfigTRD/CalibHistoComponent";
427   const char* defaultNotify="";
428   if (cdbEntry) {
429     path=cdbEntry;
430     defaultNotify=" (default)";
431   }
432   if (path) {
433     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
434     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
435     if (pEntry) {
436       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
437       if (pString) {
438         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
439         iResult=Configure(pString->GetString().Data());
440       } else {
441         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
442       }
443     } else {
444       HLTError("cannot fetch object \"%s\" from CDB", path);
445     }
446   }
447
448   return iResult;
449 }