795d1c579f8940ecc0782377fd8030c30ef6d887
[u/mrichter/AliRoot.git] / PMD / AliPMDPreprocessor.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 //                                                     //
17 //  Source File : AliPMDPreprocessor.cxx               //
18 //                                                     //
19 //                                                     //
20 //-----------------------------------------------------//
21
22 // --- ROOT system
23 #include <TFile.h>
24 #include <TTimeStamp.h>
25 #include <TObjString.h>
26 #include <TTree.h>
27
28 #include "AliLog.h"
29 #include "AliShuttleInterface.h"
30 #include "AliCDBMetaData.h"
31 #include "AliPMDCalibData.h"
32 #include "AliPMDHotData.h"
33 #include "AliPMDMeanSm.h"
34 #include "AliPMDPedestal.h"
35 #include "AliPMDPreprocessor.h"
36
37
38 ClassImp(AliPMDPreprocessor)
39   
40 //______________________________________________________________________________________________
41 AliPMDPreprocessor::AliPMDPreprocessor(AliShuttleInterface* shuttle) :
42   AliPreprocessor("PMD", shuttle)
43 {
44   // constructor
45   AddRunType("PHYSICS");
46   AddRunType("PEDESTAL");
47 }
48
49 //______________________________________________________________________________________________
50 AliPMDPreprocessor::~AliPMDPreprocessor()
51 {
52   // destructor
53 }
54
55 //______________________________________________________________________________________________
56 void AliPMDPreprocessor::Initialize(Int_t run, UInt_t startTime,
57         UInt_t endTime)
58 {
59   // Creates AliPMDDataDAQ object
60
61   AliPreprocessor::Initialize(run, startTime, endTime);
62   
63   AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
64                TTimeStamp(startTime).AsString(),
65                TTimeStamp(endTime).AsString()));
66
67   fRun = run;
68   fStartTime = startTime;
69   fEndTime = endTime;
70
71 }
72
73 //-----------------------------------------
74 Bool_t AliPMDPreprocessor::ProcessDAQ()
75 {
76   TString RunType = GetRunType();
77   Log(Form("RunType %s",RunType.Data()));
78   if (RunType !="PHYSICS" || RunType != "PEDESTAL") {
79     return kFALSE;
80   }
81
82   return kTRUE;
83 }
84
85
86 //_____________________________________________________________________
87 UInt_t AliPMDPreprocessor::Process(TMap* pdaqAliasMap)
88 {
89   
90   if(!pdaqAliasMap) return 1;
91   TString runType = GetRunType();
92   if(runType == "PEDESTAL"){
93     AliPMDPedestal *pedestal = new AliPMDPedestal();
94     
95     TList* filesources = GetFileSources(kDAQ, "PMD_PED.root");
96     
97     if(!filesources) {
98       Log(Form("No sources found for PMD_PED.root!"));
99       return 1;
100     }
101     
102     AliInfo("Here's the list of sources for PMD_PED.root");
103     filesources->Print();
104     
105     TIter iter(filesources);
106     TObjString* source;
107     UInt_t result = 0;
108     TString filename;
109     while((source=dynamic_cast<TObjString*> (iter.Next()))){
110       filename = GetFile(kDAQ, "PMD_PED.root", source->GetName());
111       if(filename.Length() == 0) {
112         Log(Form("Error retrieving file from source %s failed!", source->GetName()));
113         delete filesources;
114         return 1;
115       }
116       
117       Log(Form("File with id PMD_PED.root got from %s", source->GetName()));
118       
119       Int_t det, sm, row, col;
120       Float_t mean, rms;
121       
122       TFile *f= new TFile(filename.Data());
123       if(!f || !f->IsOpen()) 
124         {
125           Log(Form("Error opening file with Id PMD_PED.root from source %s!", source->GetName()));
126           return 1;
127         } 
128       TTree *tree = dynamic_cast<TTree *> (f->Get("ped"));
129       if (!tree) 
130         {
131           Log("Could not find object \"ped\" in PED file!");
132           return 1;
133         }
134             
135       tree->SetBranchAddress("det",  &det);
136       tree->SetBranchAddress("sm",   &sm);
137       tree->SetBranchAddress("row",  &row);
138       tree->SetBranchAddress("col",  &col);
139       tree->SetBranchAddress("mean", &mean);
140       tree->SetBranchAddress("rms",  &rms);
141       
142       Int_t nEntries = (Int_t) tree->GetEntries();
143       for(Int_t i = 0; i < nEntries; i++)
144         {
145           tree->GetEntry(i);
146           pedestal->SetPedMeanRms(det,sm,row,col,mean,rms);
147         }
148       f->Close();
149       delete f;
150     }
151     AliCDBMetaData metaData;
152     metaData.SetBeamPeriod(0);
153     metaData.SetComment("test PMD preprocessor");
154     
155     result = Store("Calib","Ped", pedestal, &metaData,0,kTRUE);
156     delete pedestal;
157     if(result==0)
158       {
159         Log("Error storing");                        
160         return 1;
161       }
162     else
163       {
164         return 0;
165       }
166     
167   }else if (runType == "PHYSICS"){
168     
169     AliPMDCalibData *calibda = new AliPMDCalibData();
170     
171     TList* filesources = GetFileSources(kDAQ, "PMDGAINS.root");
172     
173     if(!filesources) {
174       Log(Form("No sources found for PMDGAINS.root!"));
175       return 1;
176     }
177     
178     AliInfo("Here's the list of sources for PMDGAINS.root");
179     filesources->Print();
180     
181     TIter iter(filesources);
182     TObjString* source;
183     UInt_t result = 0;
184     TString filename;
185     while((source=dynamic_cast<TObjString*> (iter.Next()))){
186       filename = GetFile(kDAQ, "PMDGAINS.root", source->GetName());
187       if(filename.Length() == 0) {
188         Log(Form("Error retrieving file from source %s failed!", source->GetName()));
189         delete filesources;
190         return 1;
191       }
192       
193       Log(Form("File with id PMDGAINS.root got from %s", source->GetName()));
194       
195       Int_t det, sm, row, col;
196       Float_t gain;
197
198       TFile *f1= new TFile(filename.Data());
199       if(!f1 || !f1->IsOpen()) 
200         {
201           Log(Form("Error opening file with Id PMDGAINS.root from source %s!", source->GetName()));
202           return 1;
203         } 
204       TTree *tree = dynamic_cast<TTree *> (f1->Get("ic"));
205       if (!tree) 
206         {
207           Log("Could not find object \"ic\" in DAQ file!");
208           return 1;
209         }
210       
211       tree->SetBranchAddress("det",  &det);
212       tree->SetBranchAddress("sm",   &sm);
213       tree->SetBranchAddress("row",  &row);
214       tree->SetBranchAddress("col",  &col);
215       tree->SetBranchAddress("gain", &gain);
216       
217
218       
219       Int_t nEntries = (Int_t) tree->GetEntries();
220       for(Int_t i = 0; i < nEntries; i++)
221         {
222           tree->GetEntry(i);
223           
224           //if(DET>1 || SM>23 || ROW>95 || COL>95) {
225           //  printf("Error! gain[%d,%d,%d,%d] = %f\n",
226           //   DET,SM,ROW,COL,GAIN);
227           //  continue;
228           //}
229           
230           calibda->SetGainFact(det,sm,row,col,gain);
231         }
232       f1->Close();
233       delete f1;
234     }
235     AliCDBMetaData metaData;
236     metaData.SetBeamPeriod(0);
237     metaData.SetComment("test PMD preprocessor");
238     result = Store("Calib","Gain", calibda, &metaData);
239     delete calibda;
240     if(result==0)
241       {
242         Log("Error storing");                        
243         return 1;
244       }
245     //------------------For Storing HOT Data-------------------------//
246     AliPMDHotData *hotda = new AliPMDHotData();
247     TList* filesource = GetFileSources(kDAQ, "PMD_HOT.root");
248         
249     if(!filesource) {
250       Log(Form("No sources found for PMD_HOT.root!"));
251       return 1;
252     }
253     
254     AliInfo("Here's the list of sources for PMD_HOT.root");
255     filesource->Print();
256     
257     TIter iter2(filesource);
258     TObjString* sources;
259     UInt_t hotresult = 0;
260     TString filenames;
261     while((sources=dynamic_cast<TObjString*> (iter2.Next()))){
262       filenames = GetFile(kDAQ, "PMD_HOT.root", sources->GetName());
263       if(filenames.Length() == 0) {
264         Log(Form("Error retrieving file from source %s failed!", sources->GetName()));
265         delete filesource;
266         return 1;
267       }
268       
269       Log(Form("File with id PMD_HOT.root got from %s", sources->GetName()));
270       
271       Int_t det, sm, row, col;
272       Float_t flag;
273       
274       TFile *f2= new TFile(filenames.Data());
275       if(!f2 || !f2->IsOpen()) 
276         {
277           Log(Form("Error opening file with Id PMD_HOT.root from source %s!", sources->GetName()));
278           return 1;
279         } 
280       TTree *tree1 = dynamic_cast<TTree *> (f2->Get("hot"));
281       if (!tree1) 
282         {
283           Log("Could not find object \"hot\" in DAQ file!");
284           return 1;
285         }
286       
287       tree1->SetBranchAddress("det",  &det);
288       tree1->SetBranchAddress("sm",   &sm);
289       tree1->SetBranchAddress("row",  &row);
290       tree1->SetBranchAddress("col",  &col);
291       tree1->SetBranchAddress("flag", &flag);
292       
293       
294       
295       Int_t nEntries = (Int_t) tree1->GetEntries();
296       for(Int_t j = 0; j < nEntries; j++)
297         {
298           tree1->GetEntry(j);
299           
300           //if(det>1 || sm>23 || row>95 || col>95) {
301           // printf("Error! gain[%d,%d,%d,%d] = %f\n",
302           // det,sm,row,col,flag);
303           //continue;
304           //}
305           
306           hotda->SetHotChannel(det,sm,row,col,flag);
307         }
308       f2->Close();
309       delete f2;
310     }
311     hotresult = Store("Calib","Hot", hotda, &metaData);
312     delete hotda;
313     if(hotresult==0)
314       {
315         Log("Error storing");                        
316         return 1;
317       }
318     //-------------------------------------------------------------------  
319 //-----------------------------------for storing SM MEAN--------------//
320         
321     //------------------For Storing HOT Data-------------------------//
322     AliPMDMeanSm *smmeanda = new AliPMDMeanSm();
323     TList* filesourc = GetFileSources(kDAQ, "PMD_MEAN_SM.root");
324         
325     if(!filesourc) {
326       Log(Form("No sources found for PMD_MEAN_SM.root!"));
327       return 1;
328     }
329     
330     AliInfo("Here's the list of sources for PMD_MEAN_SM.root");
331     filesourc->Print();
332     
333     TIter iter3(filesourc);
334     TObjString* sourc;
335     UInt_t meanresult = 0;
336     TString filenam;
337     while((sourc=dynamic_cast<TObjString*> (iter3.Next()))){
338       filenam = GetFile(kDAQ, "PMD_MEAN_SM.root", sourc->GetName());
339       if(filenam.Length() == 0) {
340         Log(Form("Error retrieving file from source %s failed!", sourc->GetName()));
341         delete filesourc;
342         return 1;
343       }
344       
345       Log(Form("File with id PMD_MEAN_SM.root got from %s", sourc->GetName()));
346       
347       Int_t det, sm ;
348       Float_t smmean;
349       
350       TFile *f3= new TFile(filenam.Data());
351       if(!f3 || !f3->IsOpen()) 
352         {
353           Log(Form("Error opening file with Id PMD_MEAN_SM.root from source %s!", sourc->GetName()));
354           return 1;
355         } 
356       TTree *tree2 = dynamic_cast<TTree *> (f3->Get("mean"));
357       if (!tree2) 
358         {
359           Log("Could not find object \"hot\" in DAQ file!");
360           return 1;
361         }
362       
363       tree2->SetBranchAddress("det",  &det);
364       tree2->SetBranchAddress("sm",   &sm);
365       tree2->SetBranchAddress("smmean", &smmean);
366       
367       Int_t nEntries = (Int_t) tree2->GetEntries();
368       for(Int_t j = 0; j < nEntries; j++)
369         {
370           tree2->GetEntry(j);
371           smmeanda->SetMeanSm(det,sm,smmean);
372         }
373       f3->Close();
374       delete f3;
375     }
376     meanresult = Store("Calib","SMMEAN", smmeanda, &metaData);
377     delete smmeanda;
378     if(meanresult==0)
379       {
380         Log("Error storing");                        
381         return 1;
382       }
383     // -------------Store DCS data for reference------------//
384     AliCDBMetaData metadata;
385     metadata.SetComment("DCS data for PMD");
386     Bool_t resStore = kFALSE;
387     resStore = StoreReferenceData("DCS","Data",pdaqAliasMap,&metadata);
388     if(resStore==0)
389       {
390         Log("Error storing");                        
391         return 1;
392       }
393     else
394       {
395         return 0;
396       }
397     
398   }
399     
400   return 2;
401 }
402
403