]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDPreprocessor.cxx
Rename AliTRDCalPIDLQ
[u/mrichter/AliRoot.git] / TRD / AliTRDPreprocessor.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 // This class is a first implementation for the TRD.                      //
21 // It takes data from HLT and computes the parameters                     //
22 // and stores both reference data and online calibration                  //
23 // parameters in the CDB                                                  //
24 // It alsotakes DCS data, does spline fits                                //
25 // and stores both reference data and spline fits results                 //
26 // in the CDB                                                             //
27 //                                                                        //
28 // Author:                                                                //
29 //   R. Bailhache (R.Bailhache@gsi.de)                                    //
30 //   W. Monange   (w.monange@gsi.de)                                      //
31 //                                                                        //
32 ////////////////////////////////////////////////////////////////////////////
33
34 #include "AliTRDPreprocessor.h"
35
36 #include <TFile.h>
37 #include <TProfile2D.h>
38 #include <TStopwatch.h>
39 #include <TObjString.h>
40 #include <TString.h>
41 #include <TList.h>
42 #include <TCollection.h>
43
44 #include "AliCDBMetaData.h"
45 #include "AliLog.h"
46
47 #include "AliTRDSensorArray.h"
48 #include "AliTRDCalibraFit.h"
49 #include "AliTRDCalibraMode.h"
50 #include "AliTRDCalibPadStatus.h"
51 #include "Cal/AliTRDCalDet.h"
52 #include "Cal/AliTRDCalPadStatus.h"
53
54 ClassImp(AliTRDPreprocessor)
55
56 //______________________________________________________________________________________________
57 AliTRDPreprocessor::AliTRDPreprocessor(AliShuttleInterface *shuttle)
58                    :AliPreprocessor("TRD", shuttle)
59 {
60   //
61   // Constructor
62   //
63
64 }
65
66 //______________________________________________________________________________________________
67 AliTRDPreprocessor::~AliTRDPreprocessor()
68 {
69   //
70   // Destructor
71   //
72
73 }
74
75 //______________________________________________________________________________________________
76 void AliTRDPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
77 {
78   //
79   // Initialization routine for the TRD preprocessor
80   //
81
82   AliPreprocessor::Initialize(run,startTime,endTime);
83
84 }
85
86 //______________________________________________________________________________________________
87 UInt_t AliTRDPreprocessor::Process(TMap* dcsAliasMap)
88 {
89   //
90   // Process DCS and calibration part for HLT
91   //
92
93   UInt_t result = 0;
94
95   //
96   // DCS
97   //
98   
99   AliCDBMetaData metaData;
100   metaData.SetBeamPeriod(0);
101   metaData.SetResponsible("Wilfried Monange/Raphaelle Bailhache");
102   metaData.SetComment("TRD calib test");
103         
104         
105   Log ("****** DCS ******\n");
106         
107   TObjArray * list = AliTRDSensorArray::GetList ();
108         
109   if (list == 0x0) {
110           Log ("Error during AliTRDSensorArray::GetList");
111           Log ("DCS will not be processing");
112   }else { 
113         
114         Int_t nEntries = list->GetEntries ();
115         Log (Form ("%d alias loaded", nEntries));
116                 
117         Bool_t * results = new Bool_t [nEntries];
118         Int_t  * nGraph = new Int_t [nEntries];
119                 
120         for (Int_t iAlias = 0; iAlias < nEntries; iAlias++) {
121                         
122                 AliTRDSensorArray * oneTRDDCS = (AliTRDSensorArray *)list->At (iAlias);
123                         
124                 oneTRDDCS->SetStartTime (TTimeStamp (fStartTime));
125                 oneTRDDCS->SetEndTime (TTimeStamp (fEndTime));
126                         
127                 Log (Form("Processing DCS : \"%s\"", 
128                         oneTRDDCS->GetStoreName ().Data ()));
129                         
130                 TMap * map = oneTRDDCS->ExtractDCS (dcsAliasMap);
131                 
132                 nGraph [iAlias] = map->GetEntries ();
133                 
134                 if (nGraph [iAlias] == 0) {
135                         Log("No TGraph for this dcsDatapointAlias : not stored");
136                         results [iAlias] = kFALSE;
137                         result |= kEStoreRefDCS;
138                         continue;
139                 }
140                 
141                 oneTRDDCS->SetGraph (map);
142
143                 results [iAlias] = Store("Calib", 
144                                                                  oneTRDDCS->GetStoreName ().Data (), 
145                                                                  oneTRDDCS,  
146                                                                  &metaData,
147                                                                  0, 
148                                                                  kTRUE); 
149                 
150                 /*      
151                 results [iAlias] = StoreReferenceData("Calib", 
152                                                                                                 oneTRDDCS->GetStoreName ().Data (), 
153                                                                                                 oneTRDDCS, 
154                                                                                                 &metaData); 
155                 */
156                 if (!results [iAlias]) {
157                         AliError("Problem during StoreRef DCS"); 
158                         result |= kEStoreRefDCS;
159                 }
160                         
161                 delete map;
162                         
163                 /*
164                         //BEGIN TEST
165                 oneTRDDCS->SetDiffCut2 (0.1);
166                 map = oneTRDDCS->ExtractDCS (dcsAliasMap);
167                 oneTRDDCS->SetGraph (map);
168                         
169                 StoreReferenceData("Calib", 
170                                                         (oneTRDDCS->GetStoreName ()+"Cut").Data(), 
171                                                         oneTRDDCS, &metaData); 
172                 delete map;
173                         //END TEST
174                 */
175         }
176                 
177         Log ("         Summury of DCS :\n");
178         Log (Form("%30s %10s %10s", "dcsDatapointAlias", "Stored ?", "# graph"));
179         for (Int_t iAlias = 0; iAlias < nEntries; iAlias++) {
180                 AliTRDSensorArray * oneTRDDCS = (AliTRDSensorArray *)list->At (iAlias);
181                 Log (Form ("%30s %10s %4d", 
182                         oneTRDDCS->GetStoreName ().Data (),
183                         results[iAlias] ? "ok" : "X",
184                         nGraph [iAlias]));
185         }
186         Log ("*********** End of DCS **********");
187         
188         delete results;
189         delete nGraph;
190   }
191
192   //
193   // Process the calibration data for the HLT part
194   //
195
196  // Objects for HLT and DAQ zusammen
197   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
198   Bool_t hltVdrifthisto = kFALSE;
199   // Store the infos for the detector
200   AliCDBMetaData *md1= new AliCDBMetaData(); 
201   md1->SetObjectClassName("AliTRDCalDet");
202   md1->SetResponsible("Raphaelle Bailhache");
203   md1->SetBeamPeriod(0);
204   md1->SetComment("TRD calib test");
205   // Store the infos for the pads
206   AliCDBMetaData *md2= new AliCDBMetaData(); 
207   md2->SetObjectClassName("AliTRDCalPad");
208   md2->SetResponsible("Raphaelle Bailhache");
209   md2->SetBeamPeriod(0);
210   md2->SetComment("TRD calib test");
211  
212   //
213   // Process the calibration data for the HLT part
214   //
215
216   // How long does it take for the HLT part?
217   TStopwatch timer;
218   timer.Start();
219
220   //Run type
221   TString runType = GetRunType();
222   Log(Form("Run type for run %d: %s", fRun, runType.Data()));
223   if (strcmp(runType, "PHYSICS") != 0){
224     Log("Nothing to do for HLT!");
225   }
226   else{
227     // note that the parameters are returned as character strings!
228     const char* nEvents = GetRunParameter("totalEvents");
229     if (nEvents) {
230       Log(Form("Number of events for run %d: %s",fRun, nEvents));
231     } else {
232       Log(Form("Number of events not put in logbook!"));
233     }
234
235     // Take the file from the HLT file exchange server
236     TList *filesourceshlt = GetFileSources(kHLT,"GAINDRIFTPRF");
237     if (!filesourceshlt) {
238       Log(Form("No sources found for GAINDRIFTPRF for run %d !",fRun));
239       result |= kEListFileHLT;
240     }
241     else{
242       if (filesourceshlt->GetSize() != 1) {
243         Log(Form("More than one source found for GAINDRIFTPRF for run %d!",fRun));
244         filesourceshlt->Print();
245         result |= kEListFileHLT;
246       }
247       else {
248
249         //Debug mode
250         //calibra->SetDebugLevel(2);       //Debug
251
252         // Loop over the files taken from the HLT
253         TIter iter(filesourceshlt);
254         TObjString *sourcehlt;
255         while ((sourcehlt = dynamic_cast<TObjString *> (iter.Next()))) {
256     
257           TString filenamehlt = GetFile(kHLT,"GAINDRIFTPRF",sourcehlt->GetName());
258           if (filenamehlt.Length() == 0) {
259             Log(Form("Error retrieving file from source %d failed!", sourcehlt->GetName()));
260             result |= kEOpenFileHLT;
261           }
262           else{
263
264             // Init some things
265             TH2I         *histogain           = 0x0; // Histogram taken from HLT for gain factor
266             TProfile2D   *histodriftvelocity  = 0x0; // Profile taken from HLT for drift velocity and time0
267             TProfile2D   *histoprf            = 0x0; // Profile taken from HLT for prf
268
269
270             // Take the histos
271             TFile *filehlt = TFile::Open(filenamehlt);
272             histogain = (TH2I *) filehlt->Get("CH2d");
273             histogain->SetDirectory(0);
274             if (!histogain) {
275               Log("Error retrieving 2D histos for gain failed!");
276               result |= kETakeHistoHLT;
277             }
278             histodriftvelocity = (TProfile2D *) filehlt->Get("PH2d");
279             histodriftvelocity->SetDirectory(0);
280             if (!histodriftvelocity) {
281               Log("Error retrieving 2D Profile for average pulse height failed!");
282               result |= kETakeHistoHLT;
283             }
284             histoprf = (TProfile2D *) filehlt->Get("PRF2d");
285             histoprf->SetDirectory(0);
286             if (!histoprf) {
287               Log("Error retrieving 2D Profile for Pad Response Function failed!");
288               result |= kETakeHistoHLT;
289             }
290             filehlt->Close();
291             
292             // try to fit them and store
293             if (histogain) {
294               if(!StoreReferenceData("HLTData","Gain",(TObject *) histogain,&metaData)){
295                 Log("Error storing 2D histos for gain as reference data");
296                 result |= kEStoreHistoHLT;
297               }
298               Log("Take the CH reference data. Now we will try to fit\n");
299               calibra->SetMinEntries(100); // If there is less than 100 entries in the histo: no fit
300               calibra->AnalyseCH(histogain);
301               Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
302                 + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
303               Int_t nbfit       = calibra->GetNumberFit();
304               Int_t nbE         = calibra->GetNumberEnt();
305               if ((nbtg >                  0) && 
306                   (nbfit        >= 0.95*nbE)) {
307                 TObjArray object             = calibra->GetVectorFit();
308                 AliTRDCalDet *objgaindet   = calibra->CreateDetObjectGain(&object,calibra->GetScaleFitFactor(),kTRUE);
309                 TObject *objgainpad        = calibra->CreatePadObjectGain();
310                 if(!Store("Calib","ChamberGainFactor",(TObject *) objgaindet         ,md1,0,kTRUE)){
311                   Log("Error storing the calibration object for the chamber gain");
312                   result |= kEStoreCalHLT;
313                 }
314                 if(!Store("Calib","LocalGainFactor"  ,(TObject *) objgainpad         ,md2,0,kTRUE)){
315                   Log("Error storing the calibration object for the local gain factor");
316                   result |= kEStoreCalHLT;
317                 }
318               }
319               else{
320                 Log("Not enough statistics for the gain");
321                 result |= kEFitHistoHLT;
322               }
323               calibra->ResetVectorFit();
324             }
325     
326             if (histodriftvelocity) {
327               if(!StoreReferenceData("HLTData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
328                 Log("Error storing 2D Profile for average pulse height as reference data");
329                 result |= kEStoreHistoHLT;
330               }
331               Log("Take the PH reference data. Now we will try to fit\n");
332               calibra->SetMinEntries(100*20); // If there is less than 2000
333               calibra->AnalysePH(histodriftvelocity);
334               Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
335                 + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
336               Int_t nbfit        = calibra->GetNumberFit();
337               Int_t nbE          = calibra->GetNumberEnt();
338               if ((nbtg >                  0) && 
339                   (nbfit        >= 0.95*nbE)) {
340                 TObjArray object  = calibra->GetVectorFit();
341                 AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
342                 TObject *objdriftvelocitypad      = calibra->CreatePadObjectVdrift();
343                 object              = calibra->GetVectorFit2();
344                 AliTRDCalDet *objtime0det  = calibra->CreateDetObjectT0(&object,kTRUE);
345                 TObject *objtime0pad       = calibra->CreatePadObjectT0();
346                 if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,md1,0,kTRUE)){
347                   Log("Error storing the calibration object for the chamber vdrift");
348                   result |= kEStoreCalHLT;    
349                 }
350                 if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,md1,0,kTRUE)){
351                   Log("Error storing the calibration object for the chamber t0");
352                   result |= kEStoreCalHLT;    
353                 }
354                 if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,md2,0,kTRUE)){
355                   Log("Error storing the calibration object for the local drift velocity");
356                   result |= kEStoreCalHLT;
357                 }
358                 if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,md2,0,kTRUE)){
359                   Log("Error storing the calibration object for the local time0");
360                   result |= kEStoreCalHLT;
361                 }
362                 hltVdrifthisto = kTRUE;
363               }
364               else{
365                 Log("Not enough statistics for the average pulse height");
366                 result |= kEFitHistoHLT;
367               }      
368               calibra->ResetVectorFit();
369             }
370             
371             if (histoprf) {
372               if(!StoreReferenceData("HLTData","PRF",(TObject *) histoprf,&metaData)){
373                 Log("Error storing the 2D Profile for Pad Response Function as reference data");
374                 result |= kEStoreHistoHLT;
375               }
376               Log("Take the PRF reference data. Now we will try to fit\n");
377               calibra->SetMinEntries(100*20); // If there is less than 2000
378               calibra->AnalysePRFMarianFit(histoprf);
379               Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
380                 + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
381               Int_t nbfit        = calibra->GetNumberFit();
382               Int_t nbE          = calibra->GetNumberEnt();
383               if ((nbtg >                  0) && 
384                   (nbfit        >= 0.95*nbE)) {
385                 TObjArray object              = calibra->GetVectorFit();
386                 TObject *objPRFpad          = calibra->CreatePadObjectPRF(&object);
387                 if(!Store("Calib","PRFWidth"         ,(TObject *) objPRFpad          ,md2,0,kTRUE)){
388                   Log("Error storing the calibration object for the Pad Response Function");
389                   result |= kEStoreCalHLT;
390                 }
391               }
392               else{
393                 Log("Not enough statistics for the Pad Response Function");
394                 result |= kEFitHistoHLT;
395               }
396               calibra->ResetVectorFit();
397             }
398           } // if HLT openfile
399         } // while iter list
400       } // if HLT size of tlist
401     } //if HLT tlist
402     delete filesourceshlt;
403   } //if run type physics
404   // time
405   timer.Stop();
406   timer.Print();
407
408   //
409   // Process the calibration data for the DAQ part
410   //
411
412   // How long does it take for the DAQ part?
413   timer.Reset();
414   timer.Start();
415   
416   // Take the file from the DAQ file exchange server
417   TList *filesourcesdaq = GetFileSources(kDAQ,"PADSTATUSVDRIFT");
418   if (!filesourcesdaq) {
419     Log(Form("No sources found for GAINDRIFTPRF for run %d !",fRun));
420     result |= kEListFileDAQ;
421   }
422   else{
423     if (filesourcesdaq->GetSize() != 1) {
424       Log(Form("More than one source found for PADSTATUSVDRIFT for run %d!",fRun));
425       filesourcesdaq->Print();
426       result |= kEListFileDAQ;
427     }
428     else{
429       // Loop over the files taken from the DAQ
430       TIter iter(filesourcesdaq);
431       TObjString *sourcedaq;
432       while ((sourcedaq = dynamic_cast<TObjString *> (iter.Next()))) {
433     
434         TString filenamedaq = GetFile(kDAQ,"PADSTATUSVDRIFT",sourcedaq->GetName());
435         if (filenamedaq.Length() == 0) {
436           Log(Form("Error retrieving file from source %d failed!", sourcedaq->GetName()));
437           result |= kEOpenFileDAQ;
438         }
439         else {
440
441           // Init some things
442           TProfile2D   *histodriftvelocity  = 0x0; // Profile taken from DAQ for drift velocity and time0
443           AliTRDCalibPadStatus *calibpadstatus = 0x0; // AliTRDCalibPadStatus from DAQ for pad status
444           
445           // Take the histos
446           Bool_t something = kFALSE;
447           TFile *filedaq = TFile::Open(filenamedaq);
448           calibpadstatus = (AliTRDCalibPadStatus *) filedaq->Get("calibpadstatus");
449           if (!calibpadstatus) {
450             Log("No pedetral run!");
451           }
452           else something = kTRUE;
453           histodriftvelocity = (TProfile2D *) filedaq->Get("PH2d");
454           if (!histodriftvelocity) {
455             Log("No Vdrift TProfile2D!");
456           }
457           else{
458             histodriftvelocity->SetDirectory(0);
459           }
460           if(histodriftvelocity) something = kTRUE;
461           if(!something){
462             Log("Error DAQ, nothing in the file!");
463             result |= kETakeObjectDAQ;
464           }
465                   
466           // try to fit and store reference data
467           if(calibpadstatus){
468             calibpadstatus->AnalyseHisto();
469             if(!StoreReferenceData("DAQData","PadStatus",(TObject *) calibpadstatus,&metaData)){
470               Log("Error storing AliTRDCalibPadStatus object as reference data");
471               result |= kEStoreRefDAQ;
472             }
473             AliTRDCalPadStatus *calPadStatus = calibpadstatus->CreateCalPadStatus();
474             AliCDBMetaData *md3= new AliCDBMetaData(); 
475             md3->SetObjectClassName("AliTRDCalPadStatus");
476             md3->SetResponsible("Raphaelle Bailhache");
477             md3->SetBeamPeriod(1);
478             md3->SetComment("TRD calib test");
479             if(!Store("Calib","PadStatus"    ,(TObject *)calPadStatus, md3, 0, kTRUE)){
480               Log("Error storing the calibration object for the chamber vdrift");
481               result |= kEStoreCalDAQ;    
482             }
483           }
484           if (histodriftvelocity) {
485             if(!StoreReferenceData("DAQData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
486               Log("Error storing 2D Profile for average pulse height as reference data");
487               result |= kEStoreRefDAQ;
488             }
489             if(!hltVdrifthisto){
490               Log("Take the PH reference data. Now we will try to fit\n");
491               calibra->SetMinEntries(100*20); // If there is less than 2000
492               calibra->AnalysePH(histodriftvelocity);
493               Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
494                 + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
495               Int_t nbfit        = calibra->GetNumberFit();
496               Int_t nbE        = calibra->GetNumberEnt();
497               if ((nbtg >                  0) && 
498                   (nbfit        >= 0.95*nbE)) {
499                 TObjArray object      = calibra->GetVectorFit();
500                 AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
501                 TObject *objdriftvelocitypad = calibra->CreatePadObjectVdrift();
502                 object              = calibra->GetVectorFit2();
503                 AliTRDCalDet *objtime0det         = calibra->CreateDetObjectT0(&object,kTRUE);
504                 TObject *objtime0pad         = calibra->CreatePadObjectT0();
505                 calibra->ResetVectorFit();
506                 if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,md1,0,kTRUE)){
507                   Log("Error storing the calibration object for the chamber vdrift");
508                   result |= kEStoreCalDAQ;    
509                 }
510                 if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,md1,0,kTRUE)){
511                   Log("Error storing the calibration object for the chamber t0");
512                   result |= kEStoreCalDAQ;    
513                 }
514                 if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,md2,0,kTRUE)){
515                   Log("Error storing the calibration object for the local drift velocity");
516                   result |= kEStoreCalDAQ;
517                 }
518                 if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,md2,0,kTRUE)){
519                   Log("Error storing the calibration object for the local time0");
520                   result |= kEStoreCalDAQ;
521                 }
522               }
523               else{
524                 Log("Not enough statistics for the average pulse height");
525                 result |= kEFitObjectDAQ;
526               }
527             }
528           }
529           filedaq->Close();
530         }// if DAQ open file
531       } // while iter DAQ list
532     } // size DAQ list
533   } // tlist
534   delete filesourcesdaq;
535   // time
536   timer.Stop();
537   timer.Print();
538   
539   return result;  
540
541 }