- fixing warnings/coverity
[u/mrichter/AliRoot.git] / ITS / ITSSDDBASda.cxx
1 /*
2 - Contact: - prino@to.infn.it
3 - Link: - alien:///alice/data/2009/LHC09c_SDD/000079094/raw/09000079094024.10.root
4 - Run Type: - PEDESTAL_RUN
5 - DA Type: - LDC
6 - Number of events needed: > 10
7 - Input Files: - 
8 - Output Files: - SDDbase_step1_ddl*c*_sid*.data SDDbase_step2_ddl*c*_sid*.data
9 - Trigger types used: 
10 */
11
12
13 //////////////////////////////////////////////////////////////////////////////
14 // Detector Algorithm for analysis of SDD baseline runs.                    //
15 //                                                                          //
16 // Produces ASCII and ROOT output files with:                               //
17 // 1. anode quality bit                                                     //
18 // 1. Baseline values                                                       //
19 // 2. Raw noise                                                             //
20 // 3. Common mode coefficients                                              //
21 // 4. Noise corrected for common mode                                       //
22 // Files are stored locally.                                                //
23 // The next DAQ-DA step on Test Pulse run writes files to FXS               //
24 //                                                                          //
25 // Author: F. Prino (prino@to.infn.it)                                      //
26 //                                                                          //
27 //////////////////////////////////////////////////////////////////////////////
28
29
30
31 extern "C" {
32 #include "daqDA.h"
33 }
34
35 #include "event.h"
36 #include "monitor.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 // ROOT includes
42 #include <TFile.h>
43 #include <TSystem.h>
44 #include <TH1F.h>
45 #include <TH2F.h>
46 #include <TROOT.h>
47 #include <TPluginManager.h>
48 #include <TObjArray.h>
49 #include <TObjString.h>
50 #include <TDatime.h>
51
52 // AliRoot includes
53 #include "AliRawReaderDate.h"
54 #include "AliITSOnlineSDDBase.h"
55 #include "AliITSOnlineSDDCMN.h"
56 #include "AliITSRawStreamSDD.h"
57 #include "AliITSRawStreamSDDCompressed.h"
58
59 #ifdef ALI_AMORE
60 #include <AmoreDA.h>
61 #endif
62
63 /* Main routine
64       Arguments: list of DATE raw data files
65 */
66 int main(int argc, char **argv) {
67
68   int status = 0;
69
70   // line added to solve IO problems
71   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
72                                         "*",
73                                         "TStreamerInfo",
74                                         "RIO",
75                                         "TStreamerInfo()");
76
77   /* log start of process */
78   printf("ITS SDD BASELINE+NOISE algorithm program started\n");  
79
80
81   /* check that we got some arguments = list of files */
82   if (argc<2) {
83     printf("Wrong number of arguments\n");
84     return -1;
85   }
86
87
88   Int_t maxNEvents=10; // maximum number of events to be analyzed
89   const Int_t kTotDDL=24;
90   const Int_t kModPerDDL=12;
91   const Int_t kSides=2;
92   UInt_t amSamplFreq=40;
93
94   gSystem->Exec("rm -f SDDbase_*.data");
95   gSystem->Exec("rm -f  SDDbase_step2_LDC.tar");
96   
97   AliITSOnlineSDDBase **base=new AliITSOnlineSDDBase*[kTotDDL*kModPerDDL*kSides];
98   AliITSOnlineSDDCMN **corr=new AliITSOnlineSDDCMN*[kTotDDL*kModPerDDL*kSides];
99   TH2F **histo=new TH2F*[kTotDDL*kModPerDDL*kSides];
100   Bool_t isFilled[kTotDDL*kModPerDDL*kSides];
101   Bool_t writtenoutput=kFALSE;
102
103   Char_t hisnam[20];
104   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
105     for(Int_t imod=0; imod<kModPerDDL;imod++){
106       for(Int_t isid=0;isid<kSides;isid++){
107         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
108         base[index]=new AliITSOnlineSDDBase(iddl,imod,isid);
109         sprintf(hisnam,"h%02dc%02ds%d",iddl,imod,isid);
110         histo[index]=new TH2F(hisnam,"",256,-0.5,255.5,256,-0.5,255.5);
111         isFilled[index]=0;
112       }
113     }
114   }
115   
116   /* report progress */
117   daqDA_progressReport(8);
118   Int_t iev;
119   Int_t ievPed;
120   for(Int_t iStep=0;iStep<2;iStep++){
121     printf("Start Analysis Step %d\n",iStep);
122     iev=0;
123     ievPed=0;
124     if(iStep==1){
125       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
126         for(Int_t imod=0; imod<kModPerDDL;imod++){
127           for(Int_t isid=0;isid<kSides;isid++){
128             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
129             corr[index]=new AliITSOnlineSDDCMN(iddl,imod,isid);
130             isFilled[index]=0;
131           }
132         }
133       }
134     }
135
136     /* read the data files */
137     int n;
138     for (n=1;n<argc;n++) {
139    
140       status=monitorSetDataSource( argv[n] );
141       if (status!=0) {
142         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
143         return -1;
144       }
145
146       /* report progress */
147       /* in this example, indexed on the number of files */
148       // Progress report inside the event loop as well?
149       daqDA_progressReport(10+40*iStep*n/argc);
150
151       /* read the file */
152       for(;;) {
153         struct eventHeaderStruct *event;
154         eventTypeType eventT;
155
156         /* get next event */
157         status=monitorGetEventDynamic((void **)&event);
158         if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
159         if (status!=0) {
160           printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
161           return -1;
162         }
163
164         /* retry if got no event */
165         if (event==NULL) {
166           break;
167         }
168
169         iev++; 
170
171         /* use event - here, just write event id to result file */
172         eventT=event->eventType;
173         switch (event->eventType){
174       
175           /* START OF RUN */
176         case START_OF_RUN:
177           break;
178       
179           /* END OF RUN */
180         case END_OF_RUN:
181           break;
182
183           //      case PHYSICS_EVENT:  // comment this line for test raw data
184           //    break;               // comment this line for test raw data
185
186
187         case CALIBRATION_EVENT:
188           break;  // uncomment this line for test raw data
189         case PHYSICS_EVENT: // uncomment this line for test raw data
190           printf(" event number = %i \n",iev);
191           ievPed++; 
192           AliRawReader *rawReader = new AliRawReaderDate((void*)event);
193           rawReader->Reset();
194           UChar_t cdhAttr=AliITSRawStreamSDD::ReadBlockAttributes(rawReader);
195           amSamplFreq=AliITSRawStreamSDD::ReadAMSamplFreqFromCDH(cdhAttr);
196           AliITSRawStream* s=AliITSRawStreamSDD::CreateRawStreamSDD(rawReader,cdhAttr);
197           if(!writtenoutput){
198             printf("Use %s raw stream, sampling frequency %d MHz\n",s->ClassName(),amSamplFreq);
199             writtenoutput=kTRUE;
200           }
201
202
203           for(Int_t iddl=0; iddl<kTotDDL;iddl++){
204             for(Int_t imod=0; imod<kModPerDDL;imod++){
205               for(Int_t isid=0;isid<kSides;isid++){
206                 Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
207                 histo[index]->Reset();
208               }
209             }
210           }
211
212           while(s->Next()){
213             Int_t iDDL=rawReader->GetDDLID();
214             Int_t iCarlos=s->GetCarlosId();
215             if(s->IsCompletedModule()) continue;
216             if(s->IsCompletedDDL()) continue;
217             if(iDDL>=0 && iDDL<kTotDDL){ 
218               Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s->GetChannel(); 
219               histo[index]->Fill(s->GetCoord2(),s->GetCoord1(),s->GetSignal());
220               isFilled[index]=1;
221             }
222           }
223           delete s;
224           delete rawReader;
225           for(Int_t iddl=0; iddl<kTotDDL;iddl++){
226             for(Int_t imod=0; imod<kModPerDDL;imod++){
227               for(Int_t isid=0;isid<kSides;isid++){
228                 Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
229                 if(iStep==0){
230                   if(amSamplFreq==20) base[index]->SetLastGoodTB(126);
231                   else base[index]->SetLastGoodTB(254);
232                   base[index]->AddEvent(histo[index]);
233                 }
234                 if(iStep==1){
235                   if(amSamplFreq==20) corr[index]->SetLastGoodTB(126);
236                   else corr[index]->SetLastGoodTB(254);
237                   corr[index]->AddEvent(histo[index]);
238                 }
239               }
240             }
241           }
242
243           /* free resources */
244           free(event);
245         }
246         if(ievPed>=maxNEvents) break;
247       }
248     }
249
250     if(iStep==0){
251       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
252         for(Int_t imod=0; imod<kModPerDDL;imod++){
253           for(Int_t isid=0;isid<kSides;isid++){
254             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
255             base[index]->ValidateAnodes();
256             base[index]->WriteToASCII();
257           }
258         }
259       }
260     }
261   }
262
263   /* write report */
264   TDatime time;
265   TObjString timeinfo(Form("%02d%02d%02d%02d%02d%02d",time.GetYear()-2000,time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond()));
266   printf("Run #%s, received %d calibration events, time %s\n",getenv("DATE_RUN_NUMBER"),ievPed,timeinfo.GetString().Data());
267
268   /* report progress */
269   daqDA_progressReport(90);
270
271   TObjArray* basHistos=new TObjArray();
272   TObjArray* noiseHistos=new TObjArray();
273   TObjArray* cmnHistos=new TObjArray();
274   TObjArray* corrnHistos=new TObjArray();
275   TObjArray* statusHistos=new TObjArray();
276
277
278   Char_t filnam[100],command[150];
279   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
280     for(Int_t imod=0; imod<kModPerDDL;imod++){
281       for(Int_t isid=0;isid<kSides;isid++){
282         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
283         corr[index]->ValidateAnodes();
284         corr[index]->WriteToASCII();
285         if(isFilled[index]){
286           basHistos->AddLast(corr[index]->GetBaselineAnodeHisto());
287           noiseHistos->AddLast(corr[index]->GetRawNoiseAnodeHisto());
288           cmnHistos->AddLast(corr[index]->GetCMNCoefAnodeHisto());
289           corrnHistos->AddLast(corr[index]->GetCorrNoiseAnodeHisto());
290           statusHistos->AddLast(corr[index]->GetStatusAnodeHisto());
291           sprintf(filnam,"SDDbase_step2_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
292           sprintf(command,"tar -rf SDDbase_step2_LDC.tar %s",filnam);
293           gSystem->Exec(command);
294         }
295       }
296     }
297   }
298
299 #ifdef ALI_AMORE
300   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
301   Int_t statusamore =0;
302   statusamore += amoreDA.Send("TimeInfoPedestal",&timeinfo);
303   statusamore += amoreDA.Send("Baselines",basHistos);
304   statusamore += amoreDA.Send("RawNoise",noiseHistos);
305   statusamore += amoreDA.Send("CommonMode",cmnHistos);
306   statusamore += amoreDA.Send("CorrectedNoise",corrnHistos);
307   statusamore += amoreDA.Send("NoisyChannels",statusHistos);
308   if ( statusamore )
309     printf("Warning: Failed to write Arrays in the AMORE database\n");
310   else 
311     printf("amoreDA.Send() OK\n");
312 #else
313   printf("Warning: SDDBAS DA not compiled with AMORE support\n");
314 #endif
315     
316     
317   TFile *fh=new TFile("SDDbaseHistos.root","RECREATE");
318   basHistos->Write();
319   noiseHistos->Write();
320   cmnHistos->Write();
321   corrnHistos->Write();
322   statusHistos->Write();
323   fh->Close();
324
325   /* report progress */
326   daqDA_progressReport(100);
327
328
329
330   return status;
331 }