Code revisited after debugging. Optimization for functions in V3. Calculations of...
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDPreprocessor.cxx
1 #include "AliHMPIDPreprocessor.h" //header no includes
2 #include "AliHMPIDDigit.h"        //ProcPed()
3 #include "AliHMPIDRawStream.h"    //ProcPed()
4 #include <Riostream.h>            //ProcPed()  
5 #include <AliLog.h>               //all
6 #include <AliCDBMetaData.h>       //ProcPed(), ProcDcs()
7 #include <AliDCSValue.h>          //ProcDcs()
8 #include <TObjString.h>           //ProcDcs(), ProcPed()
9 #include <TTimeStamp.h>           //Initialize()
10 #include <TF1.h>                  //Process()
11 #include <TF2.h>                  //Process()
12 #include <TString.h>
13 #include <TGraph.h>               //Process()
14 #include <TMatrix.h>              //ProcPed()
15 #include <TList.h>                //ProcPed()
16 #include <TSystem.h>              //ProcPed()
17 //.
18 // HMPID Preprocessor base class
19 //.
20 //.
21 //.
22 ClassImp(AliHMPIDPreprocessor)
23
24 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25 void AliHMPIDPreprocessor::Initialize(Int_t run, UInt_t startTime,UInt_t endTime)
26 {
27 // Initialize the parameter coming from AliPreprocessor
28 //  run -> run number
29 // startTime -> starting time 
30 // endTime   -> ending time
31   AliPreprocessor::Initialize(run, startTime, endTime);
32   
33   AliInfo(Form("HMPID started for Run %d \n\tStartTime %s \n\t  EndTime %s", run,TTimeStamp(startTime).AsString(),TTimeStamp(endTime).AsString()));
34
35 }
36 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
37 UInt_t AliHMPIDPreprocessor::Process(TMap* pMap)
38 {
39 // Process all information from DCS and DAQ
40 // Arguments: pMap- map of DCS aliases
41 // Returns: 0 on success or 1 on error (opposite to Store!)
42
43   TString runType = GetRunType();
44   Log(Form(" AliHMPIDPreprocessor: RunType is %s",runType.Data()));
45   
46 // start to check event type and procedures
47   
48   Log("HMPID - Process in Preprocessor started");
49   if(! pMap) {
50     Log("HMPID - ERROR - Not map of DCS aliases for HMPID - ");             return kTRUE;   // error in the DCS mapped aliases
51   }   
52   if (runType == "CALIBRATION"){
53     if (!ProcPed()){
54         Log("HMPID - ERROR - Pedestal processing failed!!");                return kTRUE;   // error in pedestal processing
55     } else {
56         Log("HMPID - Pedestal processing successful!!");                    return kFALSE;  // ok for pedestals
57     }
58   } else if ( runType=="STANDALONE" || runType=="PHYSICS"){
59   if (!ProcDcs(pMap)){
60         Log("HMPID - ERROR - DCS processing failed!!");                     return kTRUE;   // error in DCS processing
61     } else {
62         Log("HMPID - DCS processing successful!!");                         return kFALSE;  // ok for DCS
63     }
64   } else {
65     Log("HMPID - Nothing to do with preprocessor for HMPID, bye!");         return kFALSE;  // ok - nothing done
66   }
67 }//Process()
68 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
69 Bool_t AliHMPIDPreprocessor::ProcDcs(TMap* pMap)
70 {
71 // Process: 1 (old). inlet and outlet C6F14 temperature, stores TObjArray of 21 TF1, where TF1 is Nmean=f(t), one per radiator
72 // Process: 1. inlet and outlet C6F14 temperature, stores TObjArray of 42 TF1, where TF1 are Tin and Tout per radiator
73 //             + one function for the mean energy photon (in total 43).
74 //          2. CH4 pressure and HV                 stores TObjArray of 7 TF1 where TF1 is thr=f(t), one per chamber
75 // Arguments: pDcsMap - map of structure "alias name" - TObjArray of AliDCSValue
76 // Assume that: HV is the same during the run for a given chamber, different chambers might have different HV
77 //              P=f(t), different for different chambers
78 // Returns: kTRUE on success  
79
80   Bool_t stDcsStore=kFALSE;
81
82 // Qthr=f(HV,P) [V,mBar]  logA0=k*HV+b is taken from p. 64 TDR plot 2.59 for PC32 
83 //                           A0=f(P) is taken from DiMauro mail
84 // Qthr is estimated as 3*A0
85
86   TF2 thr("RthrCH4"  ,"3*10^(3.01e-3*x-4.72)+170745848*exp(-y*0.0162012)"             ,2000,3000,900,1200); 
87   
88   TObjArray arNmean(43);       arNmean.SetOwner(kTRUE);     //42 Tin and Tout one per radiator + 1 for ePhotMean
89   TObjArray arQthre(42);       arQthre.SetOwner(kTRUE);     //42 Qthre=f(time) one per sector
90   
91   AliDCSValue *pVal; Int_t cnt=0;
92   
93   Double_t xP,yP;
94
95   TF1 **pTin  = new TF1*[21];
96   TF1 **pTout = new TF1*[21];
97
98 // evaluate Environment Pressure
99   
100   TObjArray *pPenv=(TObjArray*)pMap->GetValue("HMP_DET/HMP_ENV/HMP_ENV_PENV.actual.value");
101   Log(Form(" Environment Pressure data              ---> %3i entries",pPenv->GetEntries()));
102   if(pPenv->GetEntries()) {
103     TIter nextPenv(pPenv);
104     TGraph *pGrPenv=new TGraph; cnt=0;
105     while((pVal=(AliDCSValue*)nextPenv())) pGrPenv->SetPoint(cnt++,pVal->GetTimeStamp(),pVal->GetFloat());        //P env
106     if( cnt==1) {
107       pGrPenv->GetPoint(0,xP,yP);
108       new TF1("Penv",Form("%f",yP),fStartTime,fEndTime);
109     } else {
110       pGrPenv->Fit(new TF1("Penv","1000+x*[0]",fStartTime,fEndTime),"Q");
111     }
112     delete pGrPenv;
113   } else {AliWarning(" No Data Points from HMP_ENV_PENV.actual.value!");return kFALSE;}
114     
115 // evaluate Pressure
116   
117   for(Int_t iCh=0;iCh<7;iCh++){                   
118     TObjArray *pP =(TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_MP%i/HMP_MP%i_GAS/HMP_MP%i_GAS_PMWPC.actual.value",iCh,iCh,iCh));
119       Log(Form(" Pressure for module %i data             ---> %3i entries",iCh,pP->GetEntries()));
120     if(pP->GetEntries()) {
121       TIter nextP(pP);    
122       TGraph *pGrP=new TGraph; cnt=0; 
123       while((pVal=(AliDCSValue*)nextP())) pGrP->SetPoint(cnt++,pVal->GetTimeStamp(),pVal->GetFloat());            //P
124       if( cnt==1) {
125         pGrP->GetPoint(0,xP,yP);
126         new TF1(Form("P%i",iCh),Form("%f",yP),fStartTime,fEndTime);
127       } else {
128         pGrP->Fit(new TF1(Form("P%i",iCh),"[0] + x*[1]",fStartTime,fEndTime),"Q");
129       }
130       delete pGrP;
131     } else {AliWarning(" No Data Points from HMP_MP0-6_GAS_PMWPC.actual.value!");return kFALSE;}
132     
133 // evaluate High Voltage
134     
135     for(Int_t iSec=0;iSec<6;iSec++){
136       TObjArray *pHV=(TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_MP%i/HMP_MP%i_PW/HMP_MP%i_SEC%i/HMP_MP%i_SEC%i_HV.actual.vMon",iCh,iCh,iCh,iSec,iCh,iSec));
137       Log(Form(" HV for module %i and secto %i data       ---> %3i entries",iCh,iSec,pHV->GetEntries()));
138       if(pHV->GetEntries()) {
139         TIter nextHV(pHV);
140         TGraph *pGrHV=new TGraph; cnt=0;
141         while((pVal=(AliDCSValue*)nextHV())) pGrHV->SetPoint(cnt++,pVal->GetTimeStamp(),pVal->GetFloat());            //HV
142         if( cnt==1) {
143           pGrHV->GetPoint(0,xP,yP);
144           new TF1(Form("HV%i_%i",iCh,iSec),Form("%f",yP),fStartTime,fEndTime);               
145         } else {
146           pGrHV->Fit(new TF1(Form("HV%i_%i",iCh,iSec),"[0]+x*[1]",fStartTime,fEndTime),"Q");               
147         }
148         delete pGrHV;
149       } else {AliWarning(" No Data Points from HMP_MP0-6_SEC0-5_HV.actual.vMon!");return kFALSE;}
150      
151 // evaluate Qthre
152      
153       arQthre.AddAt(new TF1(Form("HMP_QthreC%iS%i",iCh,iSec),
154           Form("3*10^(3.01e-3*HV%i_%i - 4.72)+170745848*exp(-(P%i+Penv)*0.0162012)",iCh,iSec,iCh),fStartTime,fEndTime),6*iCh+iSec);
155     }
156 // evaluate Temperatures: in and out of the radiators    
157     // T in
158     for(Int_t iRad=0;iRad<3;iRad++){
159       
160       pTin[3*iCh+iRad]  = new TF1(Form("Tin%i%i" ,iCh,iRad),"[0]+[1]*x",fStartTime,fEndTime);
161       pTout[3*iCh+iRad] = new TF1(Form("Tout%i%i",iCh,iRad),"[0]+[1]*x",fStartTime,fEndTime);          
162       
163       TObjArray *pT1=(TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_MP%i/HMP_MP%i_LIQ_LOOP.actual.sensors.Rad%iIn_Temp",iCh,iCh,iRad));  
164       Log(Form(" Temperatures for module %i inside data  ---> %3i entries",iCh,pT1->GetEntries()));
165       if(pT1->GetEntries()) {
166         TIter nextT1(pT1);//Tin
167         TGraph *pGrT1=new TGraph; cnt=0;  while((pVal=(AliDCSValue*)nextT1())) pGrT1->SetPoint(cnt++,pVal->GetTimeStamp(),pVal->GetFloat()); //T inlet
168         if(cnt==1) { 
169           pGrT1->GetPoint(0,xP,yP);
170           pTin[3*iCh+iRad]->SetParameter(0,yP);
171           pTin[3*iCh+iRad]->SetParameter(1,0);
172         } else {
173           pGrT1->Fit(pTin[3*iCh+iRad],"Q");
174         }
175         delete pGrT1;
176       } else {AliWarning(" No Data Points from HMP_MP0-6_LIQ_LOOP.actual.sensors.Rad0-2In_Temp!");return kFALSE;}
177     // T out
178       TObjArray *pT2=(TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_MP%i/HMP_MP%i_LIQ_LOOP.actual.sensors.Rad%iOut_Temp",iCh,iCh,iRad)); 
179       Log(Form(" Temperatures for module %i outside data ---> %3i entries",iCh,pT2->GetEntries()));
180       if(pT2->GetEntries()) {
181         TIter nextT2(pT2);//Tout      
182         TGraph *pGrT2=new TGraph; cnt=0;  while((pVal=(AliDCSValue*)nextT2())) pGrT2->SetPoint(cnt++,pVal->GetTimeStamp(),pVal->GetFloat()); //T outlet 
183         if(cnt==1) { 
184           pGrT2->GetPoint(0,xP,yP);
185           pTout[3*iCh+iRad]->SetParameter(0,yP);
186           pTout[3*iCh+iRad]->SetParameter(1,0);
187         } else {
188           pGrT2->Fit(pTout[3*iCh+iRad],"Q");
189         }
190         delete pGrT2;
191       } else {AliWarning(" No Data Points from HMP_MP0-6_LIQ_LOOP.actual.sensors.Rad0-2Out_Temp!");return kFALSE;}
192         
193 // evaluate Mean Refractive Index
194       
195       arNmean.AddAt(pTin[3*iCh+iRad] ,6*iCh+2*iRad  ); //Tin =f(t)
196       arNmean.AddAt(pTout[3*iCh+iRad],6*iCh+2*iRad+1); //Tout=f(t)
197       
198     }//radiators loop
199   }//chambers loop
200   
201   Double_t eMean = ProcTrans(pMap);
202   arNmean.AddAt(new TF1("HMP_PhotEmean",Form("%f",eMean),fStartTime,fEndTime),42); //Photon energy mean
203     
204   AliCDBMetaData metaData; 
205   metaData.SetBeamPeriod(0); 
206   metaData.SetResponsible("AliHMPIDPreprocessor"); 
207   metaData.SetComment("HMPID preprocessor fills TObjArrays.");
208
209   stDcsStore =   Store("Calib","Qthre",&arQthre,&metaData) &&    // from DCS 
210                  Store("Calib","Nmean",&arNmean,&metaData);      // from DCS
211   if(!stDcsStore) {
212     Log("HMPID - failure to store DCS data results in OCDB");    
213   }
214   
215 //  arNmean.Delete();
216 //  arQthre.Delete();
217
218 //  for(Int_t i=0;i<21;i++) delete pTin[i]; delete []pTin;
219 //  for(Int_t i=0;i<21;i++) delete pTout[i]; delete []pTout;
220   
221   return stDcsStore;
222 }//Process()
223 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
224 Bool_t AliHMPIDPreprocessor::ProcPed()
225 {
226 // Process pedestal files and create 7 M(padx,pady)=sigma, one for each chamber
227 // Arguments:
228 // Returns: kTRUE on success
229   
230   Bool_t stPedStore=kFALSE;
231   AliHMPIDDigit dig;
232   AliHMPIDRawStream rs;
233   Int_t nSigCut,r,d,a,hard;  Float_t mean,sigma;
234   Int_t  runNumber,ldcId,timeStamp,nEv,nDdlEv,nBadEv;  Char_t tName[10]; 
235   Float_t nBadEvPer;
236   
237   TObjArray aDaqSig(7); aDaqSig.SetOwner(kTRUE); for(Int_t i=0;i<7;i++) aDaqSig.AddAt(new TMatrix(160,144),i); //TObjArray of 7 TMatrixF, m(padx,pady)=sigma
238   
239   for(Int_t iddl=0;iddl<AliHMPIDRawStream::kNDDL;iddl++)            //retrieve the files from LDCs independently the DDL<->LDC connection
240   {
241     TList *pLdc=GetFileSources(kDAQ,Form("HmpidPedDdl%02i.txt",iddl)); //get list of LDC names containing id "pedestals"
242     if(!pLdc) {Log(Form("ERROR: Retrieval of sources for pedestals: HmpidPedDdl%02i.txt failed!",iddl));continue;}
243     
244     Log(Form("HMPID - Pedestal files to be read --> %i LDCs for HMPID",pLdc->GetEntries()));
245     for(Int_t i=0;i<pLdc->GetEntries();i++) {//lists of LDCs -- but in general we have 1 LDC for 1 ped file
246     TString fileName = GetFile(kDAQ,Form("HmpidPedDdl%02i.txt",iddl),((TObjString*)pLdc->At(i))->GetName());
247     if(fileName.Length()==0) {Log(Form("ERROR retrieving pedestal file: HmpidPedDdl%02i.txt!",iddl));continue;}
248   
249     //reading pedestal file
250     ifstream infile(fileName.Data()); 
251     
252     if(!infile.is_open()) {Log("No pedestal file found for HMPID,bye!");continue;}
253     TMatrix *pM=(TMatrixF*)aDaqSig.At(iddl/2);
254   
255     infile>>tName>>runNumber;Printf("Xcheck: reading run %i",runNumber);
256     infile>>tName>>ldcId;
257     infile>>tName>>timeStamp;
258     infile>>tName>>nEv; 
259     infile>>tName>>nDdlEv;
260     infile>>tName>>nBadEv;
261     infile>>tName>>nBadEvPer;
262     infile>>tName>>nSigCut; pM->SetUniqueID(nSigCut); //n. of pedestal distribution sigmas used to create zero suppresion table
263     while(!infile.eof()){
264       infile>>dec>>r>>d>>a>>mean>>sigma>>hex>>hard;
265       if(rs.GetPad(iddl,r,d,a)>=0){                     //the GetPad returns meaningful abs pad number                                                          
266       dig.SetPad(rs.GetPad(iddl,r,d,a));
267       dig.SetQ((Int_t)mean);
268       (*pM)(dig.PadChX(),dig.PadChY()) = sigma;
269       }
270     }
271     infile.close();
272     Log(Form("Pedestal file for DDL %i read successfully",iddl));
273   
274   }//LDCs reading entries
275
276  }//DDL 
277
278   AliCDBMetaData metaData; 
279   metaData.SetBeamPeriod(0); 
280   metaData.SetResponsible("AliHMPIDPreprocessor"); 
281   metaData.SetComment("HMPID processor fills TObjArrays.");  
282   stPedStore = Store("Calib","DaqSig",&aDaqSig,&metaData,0,kTRUE);
283   if(!stPedStore) {
284     Log("HMPID - failure to store PEDESTAL data results in OCDB");    
285   }
286   return stPedStore;
287   
288 }//ProcPed()  
289 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
290 Double_t AliHMPIDPreprocessor::ProcTrans(TMap* pMap)
291 {
292   //  Process transparency monitoring data and calculates Emean  
293
294   
295   Double_t sEnergProb=0, sProb=0;
296
297   Double_t tRefCR5 = 19. ;                                      // mean temperature of CR5 where the system is in place
298
299   Double_t eMean = 0;
300       
301   AliDCSValue *pVal;
302
303   for(Int_t i=0; i<30; i++){
304
305     // evaluate wavelenght 
306     TObjArray *pWaveLenght  = (TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_INFR/HMP_INFR_TRANPLANT/HMP_INFR_TRANPLANT_MEASURE.measure[%i].waveLenght",i));
307     TIter NextWl(pWaveLenght); pVal=(AliDCSValue*)NextWl();
308     Double_t lambda = pVal->GetFloat();
309
310     Double_t photEn = 1239.842609/lambda;     // 1239.842609 from nm to eV
311     
312     if(photEn<AliHMPIDParam::EPhotMin() || photEn>AliHMPIDParam::EPhotMax()) continue;
313     
314     // evaluate phototube current for argon reference
315     TObjArray *pArgonRef  = (TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_INFR/HMP_INFR_TRANPLANT/HMP_INFR_TRANPLANT_MEASURE.measure[%i].argonReference",i));
316     TIter NextArRef(pArgonRef); pVal=(AliDCSValue*)NextArRef();
317     Double_t aRefArgon = pVal->GetFloat();
318
319     // evaluate phototube current for argon cell
320     TObjArray *pArgonCell = (TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_INFR/HMP_INFR_TRANPLANT/HMP_INFR_TRANPLANT_MEASURE.measure[%i].argonCell",i));
321     TIter NextArCell(pArgonCell); pVal=(AliDCSValue*)NextArCell();
322     Double_t aCellArgon = pVal->GetFloat();
323
324     //evaluate phototube current for freon reference
325     TObjArray *pFreonRef  = (TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_INFR/HMP_INFR_TRANPLANT/HMP_INFR_TRANPLANT_MEASURE.measure[%i].c6f14Reference",i));
326     TIter NextFrRef(pFreonRef); pVal=(AliDCSValue*)NextFrRef();
327     Double_t aRefFreon = pVal->GetFloat();
328
329     //evaluate phototube current for freon cell
330     TObjArray *pFreonCell = (TObjArray*)pMap->GetValue(Form("HMP_DET/HMP_INFR/HMP_INFR_TRANPLANT/HMP_INFR_TRANPLANT_MEASURE.measure[%i].c6f14Cell",i));
331     TIter NextFrCell(pFreonCell); pVal=(AliDCSValue*)NextFrCell();
332     Double_t aCellFreon = pVal->GetFloat();
333  
334    //evaluate correction factor to calculate trasparency (Ref. NIMA 486 (2002) 590-609)
335     
336     Double_t aN1 = AliHMPIDParam::NIdxRad(photEn,tRefCR5);
337     Double_t aN2 = AliHMPIDParam::NMgF2Idx(photEn);
338     Double_t aN3 = 1;                              // Argon Idx
339
340     Double_t aR1               = ((aN1 - aN2)*(aN1 - aN2))/((aN1 + aN2)*(aN1 + aN2));
341     Double_t aR2               = ((aN2 - aN3)*(aN2 - aN3))/((aN2 + aN3)*(aN2 + aN3));
342     Double_t aT1               = (1 - aR1);
343     Double_t aT2               = (1 - aR2);
344     Double_t aCorrFactor       = (aT1*aT1)/(aT2*aT2);
345
346     // evaluate 15 mm of thickness C6F14 Trans
347     Double_t aTransRad;
348     
349     if(aRefFreon*aRefArgon>0) {
350       aTransRad  = TMath::Power((aCellFreon/aRefFreon)/(aCellArgon/aRefArgon)*aCorrFactor,1.5);
351     } else {
352       return DefaultEMean();
353     }
354
355     // evaluate 0.5 mm of thickness SiO2 Trans
356     Double_t aTransSiO2 = TMath::Exp(-0.5/AliHMPIDParam::LAbsWin(photEn));
357
358     // evaluate 80 cm of thickness Gap (low density CH4) transparency
359     Double_t aTransGap  = TMath::Exp(-80./AliHMPIDParam::LAbsGap(photEn));
360
361     // evaluate CsI quantum efficiency
362     Double_t aCsIQE            = AliHMPIDParam::QEffCSI(photEn);
363
364     // evaluate total convolution of all material optical properties
365     Double_t aTotConvolution   = aTransRad*aTransSiO2*aTransGap*aCsIQE;
366
367     sEnergProb+=aTotConvolution*photEn;  
368   
369     sProb+=aTotConvolution;  
370 }
371
372   if(sProb>0) {
373     eMean = sEnergProb/sProb;
374   } else {
375     return DefaultEMean();
376   }
377
378   Log(Form(" Mean energy photon calculated ---> %f eV ",eMean));
379
380   if(eMean<AliHMPIDParam::EPhotMin() || eMean>AliHMPIDParam::EPhotMax()) return DefaultEMean();
381   
382   return eMean;
383
384 }   
385 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
386 Double_t AliHMPIDPreprocessor::DefaultEMean()
387 {
388     Double_t eMean = 6.675;      //just set a refractive index for C6F14 at ephot=6.675 eV @ T=25 C
389     AliWarning(Form("Mean energy for photons out of range [%f,%f] in Preprocessor. Default value Eph=%f eV taken.",AliHMPIDParam::EPhotMin(),
390                                                                                                                    AliHMPIDParam::EPhotMax(),
391                                                                                                                    eMean));
392     return eMean;
393 }