Updates in SDD DA: new event counting scheme, protections against missing configurati...
[u/mrichter/AliRoot.git] / ITS / ITSSDDBASda.cxx
1 /*
2 - Contact: - prino@to.infn.it
3 - Link: - http://www.to.infn.it/~prino/alice/RawData/run11161.date
4 - Run Type: - PEDESTAL_RUN
5 - DA Type: - LDC
6 - Number of events needed: 100
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
49 // AliRoot includes
50 #include "AliRawReaderDate.h"
51 #include "AliITSOnlineSDDBase.h"
52 #include "AliITSOnlineSDDCMN.h"
53 #include "AliITSRawStreamSDD.h"
54 /* Main routine
55       Arguments: list of DATE raw data files
56 */
57 int main(int argc, char **argv) {
58
59   int status = 0;
60
61   // line added to solve IO problems
62   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
63                                         "*",
64                                         "TStreamerInfo",
65                                         "RIO",
66                                         "TStreamerInfo()");
67
68   /* log start of process */
69   printf("ITS SDD BASELINE+NOISE algorithm program started\n");  
70
71
72   /* check that we got some arguments = list of files */
73   if (argc<2) {
74     printf("Wrong number of arguments\n");
75     return -1;
76   }
77
78
79   Int_t maxNEvents=10; // maximum number of events to be analyzed
80   const Int_t kTotDDL=24;
81   const Int_t kModPerDDL=12;
82   const Int_t kSides=2;
83   Int_t adcSamplFreq=40;
84   Bool_t readfeeconf=kFALSE;
85   gSystem->Exec("rm -f SDDbase_*.data");
86   if(gSystem->Getenv("DAQ_DETDB_LOCAL")!=NULL){
87     const char* dir=gSystem->Getenv("DAQ_DETDB_LOCAL");    
88     TString filnam=Form("%s/fee.conf",dir); 
89     FILE* feefil=fopen(filnam.Data(),"r"); 
90     if(feefil){
91       fscanf(feefil,"%d \n",&adcSamplFreq);
92       fclose(feefil);
93       readfeeconf=kTRUE;
94       printf("ADC sampling frequency = %d MHz\n",adcSamplFreq);
95     }
96   }
97   if(!readfeeconf) printf("File fee.conf not found, sampling frequency set to 40 MHz\n");
98   
99   AliITSOnlineSDDBase **base=new AliITSOnlineSDDBase*[kTotDDL*kModPerDDL*kSides];
100   AliITSOnlineSDDCMN **corr=new AliITSOnlineSDDCMN*[kTotDDL*kModPerDDL*kSides];
101   TH2F **histo=new TH2F*[kTotDDL*kModPerDDL*kSides];
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         if(adcSamplFreq==20) base[index]->SetLastGoodTB(126);
110         else base[index]->SetLastGoodTB(254);
111         sprintf(hisnam,"h%02dc%02ds%d",iddl,imod,isid);
112         histo[index]=new TH2F(hisnam,"",256,-0.5,255.5,256,-0.5,255.5);
113       }
114     }
115   }
116   
117   /* report progress */
118   daqDA_progressReport(8);
119   Int_t iev;
120   Int_t ievPed;
121   for(Int_t iStep=0;iStep<2;iStep++){
122     printf("Start Analysis Step %d\n",iStep);
123     iev=0;
124     ievPed=0;
125     if(iStep==1){
126       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
127         for(Int_t imod=0; imod<kModPerDDL;imod++){
128           for(Int_t isid=0;isid<kSides;isid++){
129             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
130             corr[index]=new AliITSOnlineSDDCMN(iddl,imod,isid);
131             if(adcSamplFreq==20) corr[index]->SetLastGoodTB(126);
132             else corr[index]->SetLastGoodTB(254);
133           }
134         }
135       }
136     }
137
138     /* read the data files */
139     int n;
140     for (n=1;n<argc;n++) {
141    
142       status=monitorSetDataSource( argv[n] );
143       if (status!=0) {
144         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
145         return -1;
146       }
147
148       /* report progress */
149       /* in this example, indexed on the number of files */
150       // Progress report inside the event loop as well?
151       daqDA_progressReport(10+40*iStep*n/argc);
152
153       /* read the file */
154       for(;;) {
155         struct eventHeaderStruct *event;
156         eventTypeType eventT;
157
158         /* get next event */
159         status=monitorGetEventDynamic((void **)&event);
160         if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
161         if (status!=0) {
162           printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
163           return -1;
164         }
165
166         /* retry if got no event */
167         if (event==NULL) {
168           break;
169         }
170
171         iev++; 
172
173         /* use event - here, just write event id to result file */
174         eventT=event->eventType;
175         switch (event->eventType){
176       
177           /* START OF RUN */
178         case START_OF_RUN:
179           break;
180       
181           /* END OF RUN */
182         case END_OF_RUN:
183           break;
184
185           //      case PHYSICS_EVENT:  // comment this line for test raw data
186           //    break;               // comment this line for test raw data
187
188
189         case CALIBRATION_EVENT:
190           break;  // uncomment this line for test raw data
191         case PHYSICS_EVENT: // uncomment this line for test raw data
192           printf(" event number = %i \n",iev);
193           ievPed++; 
194           AliRawReader *rawReader = new AliRawReaderDate((void*)event);
195
196
197           Int_t evtyp=0;
198           while(rawReader->ReadHeader()){
199             const UInt_t *subev = rawReader->GetSubEventAttributes();
200             if(subev[0]==0 && subev[1]==0 && subev[2]==0) evtyp=1; 
201           }
202
203           rawReader->Reset();
204           for(Int_t iddl=0; iddl<kTotDDL;iddl++){
205             for(Int_t imod=0; imod<kModPerDDL;imod++){
206               for(Int_t isid=0;isid<kSides;isid++){
207                 Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
208                 histo[index]->Reset();
209               }
210             }
211           }
212           AliITSRawStreamSDD s(rawReader);
213
214           while(s.Next()){
215             Int_t iDDL=rawReader->GetDDLID();
216             Int_t iCarlos=s.GetCarlosId();
217             if(s.IsCompletedModule()) continue;
218             if(s.IsCompletedDDL()) continue;
219             if(iDDL>=0 && iDDL<kTotDDL){ 
220               Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s.GetChannel(); 
221               histo[index]->Fill(s.GetCoord2(),s.GetCoord1(),s.GetSignal());
222             }
223           }
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) base[index]->AddEvent(histo[index]);
230                 if(iStep==1) corr[index]->AddEvent(histo[index]);
231               }
232             }
233           }
234
235           /* free resources */
236           free(event);
237         }
238         if(ievPed>=maxNEvents) break;
239       }
240     }
241
242     if(iStep==0){
243       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
244         for(Int_t imod=0; imod<kModPerDDL;imod++){
245           for(Int_t isid=0;isid<kSides;isid++){
246             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
247             base[index]->ValidateAnodes();
248             base[index]->WriteToASCII();
249           }
250         }
251       }
252     }
253   }
254
255   /* write report */
256   printf("Run #%s, received %d calibration events\n",getenv("DATE_RUN_NUMBER"),ievPed);
257
258   /* report progress */
259   daqDA_progressReport(90);
260
261
262
263   TFile *fh=new TFile("SDDbaseHistos.root","RECREATE");
264   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
265     for(Int_t imod=0; imod<kModPerDDL;imod++){
266       for(Int_t isid=0;isid<kSides;isid++){
267         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
268         corr[index]->ValidateAnodes();
269         corr[index]->WriteToASCII();
270         corr[index]->WriteToROOT(fh);
271       }
272     }
273   }
274   fh->Close();
275
276
277   /* report progress */
278   daqDA_progressReport(100);
279
280
281
282   return status;
283 }