]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDCPreprocessor.cxx
Adding an additional ascii format for the entry collection + a getter to the TEntryLi...
[u/mrichter/AliRoot.git] / ZDC / AliZDCPreprocessor.cxx
1 // --- ROOT system
2 #include <TFile.h>
3 #include <TClonesArray.h>
4 #include <TList.h>
5 #include <TObjString.h>
6 #include <TTimeStamp.h>
7
8 #include "AliZDCPreprocessor.h"
9 #include "AliCDBManager.h"
10 #include "AliCDBEntry.h"
11 #include "AliCDBMetaData.h"
12 #include "AliDCSValue.h"
13 #include "AliAlignObj.h"
14 #include "AliAlignObjParams.h"
15 #include "AliLog.h"
16 #include "AliZDCDataDCS.h"
17 #include "AliZDCChMap.h"
18 #include "AliZDCPedestals.h"
19 #include "AliZDCLaserCalib.h"
20 #include "AliZDCCalib.h"
21
22 /////////////////////////////////////////////////////////////////////
23 //                                                                 //
24 // Class implementing ZDC pre-processor.                           //
25 // It takes data from DCS and passes it to the class AliZDCDataDCS //
26 // The class is then written to the CDB.                           //
27 //                                                                 //
28 /////////////////////////////////////////////////////////////////////
29
30 ClassImp(AliZDCPreprocessor)
31
32 //______________________________________________________________________________________________
33 AliZDCPreprocessor::AliZDCPreprocessor(AliShuttleInterface* shuttle) :
34   AliPreprocessor("ZDC", shuttle),
35   fData(0)
36 {
37   // constructor
38   AddRunType("STANDALONE_PEDESTAL");
39   AddRunType("STANDALONE_LASER");
40   AddRunType("STANDALONE_EMD");
41   AddRunType("STANDALONE_COSMIC");
42   AddRunType("STANDALONE_BC");
43   AddRunType("PHYSICS");
44 }
45
46
47 //______________________________________________________________________________________________
48 AliZDCPreprocessor::~AliZDCPreprocessor()
49 {
50   // destructor
51 }
52
53
54 //______________________________________________________________________________________________
55 void AliZDCPreprocessor::Initialize(Int_t run, UInt_t startTime,
56         UInt_t endTime)
57 {
58   // Creates AliZDCDataDCS object
59
60   AliPreprocessor::Initialize(run, startTime, endTime);
61
62         Log(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
63                 TTimeStamp(startTime).AsString(),
64                 TTimeStamp(endTime).AsString()));
65
66         fRun = run;
67         fStartTime = startTime;
68         fEndTime = endTime;
69
70         fData = new AliZDCDataDCS(fRun, fStartTime, fEndTime);
71 }
72
73 //______________________________________________________________________________________________
74 UInt_t AliZDCPreprocessor::ProcessChMap(TString runType)
75
76   // Reading the file for mapping from FXS
77   TList* daqSource = GetFileSources(kDAQ, runType.Data());
78   if(!daqSource){
79     AliError(Form("No sources run %d for run type %s!", fRun, runType.Data()));
80     return 1;
81   }
82   Log("\t List of sources "); daqSource->Print();
83   //
84   TIter iter(daqSource);
85   TObjString* source = 0;
86   Int_t isou=0;
87   Int_t res=999;
88   Int_t readMap[48][6]; 
89   //
90   while((source = dynamic_cast<TObjString*> (iter.Next()))){
91      Log(Form("\n\t Getting file #%d\n",++isou));
92      TString fileName = "ZDCChMapping.dat";
93
94      if(fileName.Length() <= 0){
95        Log(Form("No file from source %s!", source->GetName()));
96        return 1;
97      }
98      // --- Reading file with calibration data
99      //const char* fname = fileName.Data();
100      if(fileName){
101        FILE *file;
102        if((file = fopen(fileName,"r")) == NULL){
103          printf("Cannot open file %s \n",fileName.Data());
104          return 1;
105        }
106        Log(Form("File %s connected to process data for ADC mapping", fileName.Data()));
107        //
108        for(Int_t j=0; j<48; j++){         
109            for(Int_t k=0; k<6; k++){
110              fscanf(file,"%d",&readMap[j][k]);
111            }
112        }
113        fclose(file);
114      }
115      else{
116        Log(Form("File %s not found", fileName.Data()));
117        return 1;
118      }
119   }
120   delete daqSource; daqSource=0;
121   
122   // Store the currently read map ONLY IF it is different
123   // from the entry in the OCDB
124   Bool_t updateOCDB = kFALSE;
125   
126   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","ChMap");
127   if(!cdbEntry){
128     Log("\t AliZDCPreprocessor -> WARNING! No CDB entry for ch. mapping\n");
129     updateOCDB = kTRUE;
130   }
131   else{
132     AliZDCChMap *chMap = (AliZDCChMap*) cdbEntry->GetObject();
133     for(Int_t i=0; i<48; i++){
134       if(  (readMap[i][1] == chMap->GetADCModule(i)) 
135         && (readMap[i][2] == chMap->GetADCChannel(i)) 
136         && (readMap[i][4] == chMap->GetDetector(i)) 
137         && (readMap[i][5] == chMap->GetSector(i))){
138          updateOCDB = kFALSE;
139       }
140       else updateOCDB = kTRUE;
141     }
142   }
143   //
144   if(updateOCDB==kTRUE){
145     Log("\t AliZDCPreprocessor -> A new entry ZDC/Calib/ChMap will be created");
146     //
147     // --- Initializing mapping calibration object
148     AliZDCChMap *mapCalib = new AliZDCChMap("ZDC");
149     // Writing channel map in the OCDB
150     for(Int_t k=0; k<48; k++){
151       mapCalib->SetADCModule(k,readMap[k][1]);
152       mapCalib->SetADCChannel(k,readMap[k][2]);
153       mapCalib->SetDetector(k,readMap[k][4]);
154       mapCalib->SetSector(k,readMap[k][5]);
155     }
156     //mapCalib->Print("");
157     // 
158     AliCDBMetaData metaData;
159     metaData.SetBeamPeriod(0);
160     metaData.SetResponsible("Chiara Oppedisano");
161     metaData.SetComment("Filling AliZDCChMap object");  
162     //
163     res = Store("Calib","ChMap",mapCalib, &metaData, 0, 1);
164   }
165   else{
166     Log("\t AliZDCPreprocessor -> ZDC/Calib/ChMap entry in OCDB is valid and won't be updated\n");
167     res = kTRUE;
168   }
169
170   
171   return res;
172
173 }
174
175 //______________________________________________________________________________________________
176 UInt_t AliZDCPreprocessor::Process(TMap* dcsAliasMap)
177 {
178   // *************** From DCS ******************
179   // Fills data into a AliZDCDataDCS object
180   if(!dcsAliasMap) return 1;
181
182   // The processing of the DCS input data is forwarded to AliZDCDataDCS
183   Float_t dcsValues[28]; // DCSAliases=28
184   fData->SetCalibData(dcsValues);
185   fData->ProcessData(*dcsAliasMap);
186   // Store DCS data for reference
187   AliCDBMetaData metadata;
188   metadata.SetResponsible("Chiara Oppedisano");
189   metadata.SetComment("DCS data for ZDC");
190   Bool_t resDCSRef = kTRUE;
191   resDCSRef = StoreReferenceData("DCS","Data",fData,&metadata);
192   //dcsAliasMap->Print("");
193   //
194   // --- Writing ZDC table positions into alignment object
195   TClonesArray *array = new TClonesArray("AliAlignObjParams",10);
196   TClonesArray &alobj = *array;
197   AliAlignObjParams a;
198   Double_t dx=0., dz=0., dpsi=0., dtheta=0., dphi=0.;
199   // Vertical table position in mm from DCS
200   Double_t dyZN1 = (Double_t) (dcsValues[0]/10.);
201   Double_t dyZP1 = (Double_t) (dcsValues[1]/10.);
202   Double_t dyZN2 = (Double_t) (dcsValues[2]/10.);
203   Double_t dyZP2 = (Double_t) (dcsValues[3]/10.);
204   //
205   const char *n1ZDC="ZDC/NeutronZDC_C";
206   const char *p1ZDC="ZDC/ProtonZDC_C";
207   const char *n2ZDC="ZDC/NeutronZDC_A";
208   const char *p2ZDC="ZDC/ProtonZDC_A";
209   //
210   UShort_t iIndex=0;
211   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
212   UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
213   //
214   new(alobj[0]) AliAlignObjParams(n1ZDC, volid, dx, dyZN1, dz, dpsi, dtheta, dphi, kTRUE);
215   new(alobj[1]) AliAlignObjParams(p1ZDC, volid, dx, dyZP1, dz, dpsi, dtheta, dphi, kTRUE);
216   new(alobj[2]) AliAlignObjParams(n2ZDC, volid, dx, dyZN2, dz, dpsi, dtheta, dphi, kTRUE);
217   new(alobj[3]) AliAlignObjParams(p2ZDC, volid, dx, dyZP2, dz, dpsi, dtheta, dphi, kTRUE);
218   // save in CDB storage
219   AliCDBMetaData md;
220   md.SetResponsible("Chiara Oppedisano");
221   md.SetComment("Alignment object for ZDC");
222   Bool_t resultAl = kTRUE;
223   resultAl = Store("Align","Data", array, &md, 0, 0);
224   
225 // *************** From DAQ ******************
226 Bool_t resChMap=kTRUE, resPedCal=kTRUE, resLaserCal=kTRUE, resECal=kTRUE;
227 // 
228 const char* beamType = GetRunParameter("beamType");
229 TString runType = GetRunType();
230 printf("\n\t AliZDCPreprocessor -> beamType %s\n",beamType);
231 printf("\t AliZDCPreprocessor -> runType  %s\n\n",runType.Data());
232
233 if(strcmp(beamType,"p-p")==0){
234    
235    // --- Cheking if there is already the entry in the OCDB
236    AliCDBEntry *cdbEntry = GetFromOCDB("Calib", "EMDCalib");
237    if(!cdbEntry){   
238      printf("\t AliZDCPreprocessor -> ZDC/Calib/EMDCalib entry will be created\n");
239      // --- Initializing calibration object
240      AliZDCCalib *eCalib = new AliZDCCalib("ZDC");
241      //
242      for(Int_t j=0; j<6; j++) eCalib->SetEnCalib(j,1.);
243      for(Int_t j=0; j<5; j++){  
244         eCalib->SetZN1EqualCoeff(j, 1.);
245         eCalib->SetZP1EqualCoeff(j, 1.);
246         eCalib->SetZN2EqualCoeff(j, 1.);
247         eCalib->SetZP2EqualCoeff(j, 1.);  
248      }
249      //eCalib->Print("");
250      // 
251      AliCDBMetaData metaData;
252      metaData.SetBeamPeriod(0);
253      metaData.SetResponsible("Chiara Oppedisano");
254      metaData.SetComment("AliZDCCalib object");  
255      //
256      resECal = Store("Calib","EMDCalib",eCalib, &metaData, 0, 1);
257    }
258    else{
259      printf("\t AliZDCPreprocessor -> ZDC/Calib/EMDCalib object already existing in OCDB!!!\n");
260      resECal = kTRUE;
261    }
262 }
263 // ******************************************
264 //   ZDC ADC channel mapping
265 // ******************************************
266 resChMap = ProcessChMap(runType);
267 // 
268 // *****************************************************
269 // [a] PEDESTALS -> Pedestal subtraction
270 // *****************************************************
271 // 
272 if(runType=="STANDALONE_PEDESTAL"){
273   TList* daqSources = GetFileSources(kDAQ, "PEDESTALS");
274   if(!daqSources){
275     Log(Form("No source for STANDALONE_PEDESTAL run %d !", fRun));
276     return 1;
277   }
278   Log("\t List of sources for STANDALONE_PEDESTAL");
279   daqSources->Print();
280   //
281   TIter iter(daqSources);
282   TObjString* source = 0;
283   Int_t i=0;
284   while((source = dynamic_cast<TObjString*> (iter.Next()))){
285        Log(Form("\n\t Getting file #%d\n",++i));
286        TString stringPedFileName = GetFile(kDAQ, "PEDESTALS", source->GetName());
287        if(stringPedFileName.Length() <= 0){
288           Log(Form("No PEDESTAL file from source %s!", source->GetName()));
289           return 1;
290        }
291        // --- Initializing pedestal calibration object
292        AliZDCPedestals *pedCalib = new AliZDCPedestals("ZDC");
293        // --- Reading file with pedestal calibration data
294        const char* pedFileName = stringPedFileName.Data();
295        // no. ADCch = (22 signal ch. + 2 reference PMs) * 2 gain chain = 48
296        const Int_t knZDCch = 48;
297        if(pedFileName){
298          FILE *file;
299          if((file = fopen(pedFileName,"r")) == NULL){
300            printf("Cannot open file %s \n",pedFileName);
301            return 1;
302          }
303          Log(Form("File %s connected to process pedestal data", pedFileName));
304          Float_t pedVal[(2*knZDCch)][2];
305          for(Int_t k=0; k<(2*knZDCch); k++){
306             for(Int_t j=0; j<2; j++){
307                fscanf(file,"%f",&pedVal[k][j]);
308                //if(j==1) printf("pedVal[%d] -> %f, %f \n",k,pedVal[k][0],pedVal[k][1]);
309             }
310             if(k<knZDCch){
311               pedCalib->SetMeanPed(k,pedVal[k][0]);
312               pedCalib->SetMeanPedWidth(i,pedVal[k][1]);
313             }
314             else if(k>=knZDCch && k<(2*knZDCch)){
315               pedCalib->SetOOTPed(k-knZDCch,pedVal[k][0]);
316               pedCalib->SetOOTPedWidth(k-knZDCch,pedVal[k][1]);
317             }
318             else if(k>=(2*knZDCch) && k<(3*knZDCch)){
319               pedCalib->SetPedCorrCoeff(k-(2*knZDCch),pedVal[k][0],pedVal[k][1]);
320             }
321          }
322          fclose(file);
323        }
324        else{
325           Log(Form("File %s not found", pedFileName));
326           return 1;
327        }
328        //
329       //pedCalib->Print("");
330       // 
331       AliCDBMetaData metaData;
332       metaData.SetBeamPeriod(0);
333       metaData.SetResponsible("Chiara Oppedisano");
334       metaData.SetComment("Filling AliZDCPedestals object");  
335       //
336       resPedCal = Store("Calib","Pedestals",pedCalib, &metaData, 0, 1);
337   }
338   delete daqSources; daqSources = 0;
339 }
340 // *****************************************************
341 // [b] STANDALONE_LASER EVENTS -> Signal stability
342 // *****************************************************
343 else if(runType=="STANDALONE_LASER"){
344   TList* daqSources = GetFileSources(kDAQ, "LASER");
345   if(!daqSources){
346     AliError(Form("No sources for STANDALONE_LASER run %d !", fRun));
347     return 1;
348   }
349   Log("\t List of sources for STANDALONE_LASER");
350   daqSources->Print();
351   //
352   TIter iter2(daqSources);
353   TObjString* source = 0;
354   Int_t i=0;
355   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
356        Log(Form("\n\t Getting file #%d\n",++i));
357        TString stringLASERFileName = GetFile(kDAQ, "LASER", source->GetName());
358        if(stringLASERFileName.Length() <= 0){
359          Log(Form("No LASER file from source %s!", source->GetName()));
360          return 1;
361        }
362        // --- Initializing pedestal calibration object
363        AliZDCLaserCalib *lCalib = new AliZDCLaserCalib("ZDC");
364        // --- Reading file with pedestal calibration data
365        const char* laserFileName = stringLASERFileName.Data();
366        if(laserFileName){
367          FILE *file;
368          if((file = fopen(laserFileName,"r")) == NULL){
369            printf("Cannot open file %s \n",laserFileName);
370            return 1;
371          }
372          Log(Form("File %s connected to process data from LASER events", laserFileName));
373          //
374          Float_t ivalRead[4][2]; 
375          for(Int_t j=0; j<4; j++){
376             for(Int_t k=0; k<2; k++){
377               fscanf(file,"%f",&ivalRead[j][k]);
378               //printf(" %d %1.0f  ",k, ivalRead[j][k]);
379             }
380             if(j==0 || j==1){
381               lCalib->SetGain(j, 0);
382               if(j==0) lCalib->SetSector(j, 1);
383               else lCalib->SetSector(j, 4);
384             }
385             else if(j==2 || j==3){
386               lCalib->SetGain(j, 1);
387               if(j==2) lCalib->SetSector(j, 1);
388               else lCalib->SetSector(j, 4);
389             }
390             lCalib->SetfPMRefValue(j, ivalRead[j][0]);
391             lCalib->SetfPMRefWidth(j, ivalRead[j][1]);
392          }
393          fclose(file);
394        }
395        else{
396          Log(Form("File %s not found", laserFileName));
397          return 1;
398        }
399        //lCalib->Print("");
400        // 
401        AliCDBMetaData metaData;
402        metaData.SetBeamPeriod(0);
403        metaData.SetResponsible("Chiara Oppedisano");
404        metaData.SetComment("Filling AliZDCLaserCalib object");  
405        //
406        resLaserCal = Store("Calib","LaserCalib",lCalib, &metaData, 0, 1);
407   }
408   
409 }
410 // *****************************************************
411 // [c] EMD EVENTS -> Energy calibration and equalization
412 // *****************************************************
413 else if(runType=="STANDALONE_EMD" && strcmp(beamType,"Pb-Pb")==0){
414   TList* daqSources = GetFileSources(kDAQ, "EMDCALIB");
415   if(!daqSources){
416     AliError(Form("No sources for STANDALONE_EMD run %d !", fRun));
417     return 1;
418   }
419   Log("\t List of sources for STANDALONE_EMD");
420   daqSources->Print();
421   //
422   TIter iter2(daqSources);
423   TObjString* source = 0;
424   Int_t i=0;
425   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
426       Log(Form("\n\t Getting file #%d\n",++i));
427       TString stringEMDFileName = GetFile(kDAQ, "EMDCALIB", source->GetName());
428       if(stringEMDFileName.Length() <= 0){
429         Log(Form("No EMDCALIB file from source %s!", source->GetName()));
430         return 1;
431       }
432       // --- Initializing pedestal calibration object
433       AliZDCCalib *eCalib = new AliZDCCalib("ZDC");
434       // --- Reading file with pedestal calibration data
435       const char* emdFileName = stringEMDFileName.Data();
436       if(emdFileName){
437         FILE *file;
438         if((file = fopen(emdFileName,"r")) == NULL){
439           printf("Cannot open file %s \n",emdFileName);
440           return 1;
441         }
442         Log(Form("File %s connected to process data from EM dissociation events", emdFileName));
443         //
444         Float_t fitValEMD[6]; Float_t equalCoeff[5][4];
445         Float_t calibVal[4];
446         for(Int_t j=0; j<10; j++){         
447           if(j<6){
448             fscanf(file,"%f",&fitValEMD[j]);
449             if(j<4){
450               calibVal[j] = 2.76/fitValEMD[j];
451               eCalib->SetEnCalib(j,calibVal[j]);
452             }
453             else eCalib->SetEnCalib(j,fitValEMD[j]);
454           }
455           else{
456             for(Int_t k=0; k<5; k++){
457                fscanf(file,"%f",&equalCoeff[j][k]);
458                if(j==6)      eCalib->SetZN1EqualCoeff(k, equalCoeff[j][k]);
459                else if(j==7) eCalib->SetZP1EqualCoeff(k, equalCoeff[j][k]);
460                else if(j==8) eCalib->SetZN2EqualCoeff(k, equalCoeff[j][k]);
461                else if(j==9) eCalib->SetZP2EqualCoeff(k, equalCoeff[j][k]);  
462             }
463           }
464         }
465         fclose(file);
466       }
467       else{
468         Log(Form("File %s not found", emdFileName));
469         return 1;
470       }
471       //eCalib->Print("");
472       // 
473       AliCDBMetaData metaData;
474       metaData.SetBeamPeriod(0);
475       metaData.SetResponsible("Chiara Oppedisano");
476       metaData.SetComment("Filling AliZDCCalib object");  
477       //
478       resECal = Store("Calib","EMDCalib",eCalib, &metaData, 0, 1);
479   }
480 }
481
482
483   // note that the parameters are returned as character strings!
484   const char* nEvents = GetRunParameter("totalEvents");
485   if(nEvents) Log(Form("Number of events for run %d: %s",fRun, nEvents));
486   else Log(Form("Number of events not put in logbook!"));
487  
488   UInt_t result = 0;
489   if(resDCSRef==kFALSE || resultAl==kFALSE || resPedCal==kFALSE ||
490      resLaserCal==kFALSE || resECal==kFALSE || resChMap==kFALSE){
491     if(resDCSRef == kFALSE)        result = 1;
492     else if(resultAl == kFALSE)    result = 2;
493     else if(resChMap == kFALSE)    result = 3;
494     else if(resPedCal == kFALSE)   result = 4;
495     else if(resLaserCal == kFALSE) result = 5;
496     else if(resECal == kFALSE)     result = 6;
497   }
498   
499   return result;
500   
501 }