Updates in SDD DA: new event counting scheme, protections against missing configurati...
[u/mrichter/AliRoot.git] / ITS / ITSSDDGAINda.cxx
1 /*
2 - Contact: - prino@to.infn.it
3 - Link: - http://www.to.infn.it/~prino/alice/RawData/run11173.date
4 - Run Type: - PULSER_RUN
5 - DA Type: - LDC
6 - Number of events needed: 100
7 - Input Files: - SDDbase_step1_ddl*c*_sid*.data
8 - Output Files: - SDDbase_ddl*c*_sid*.data
9 - Trigger types used: 
10 */
11
12 //////////////////////////////////////////////////////////////////////////////
13 // Detector Algorithm for analysis of SDD test pulse runs.                  //
14 //                                                                          //
15 // Produces ASCII and ROOT output files with:                               //
16 // 1. anode quality bit                                                     //
17 // 1. Baseline values                                                       //
18 // 2. Raw noise                                                             //
19 // 3. Common mode coefficients                                              //
20 // 4. Noise corrected for common mode                                       //
21 // 5. Gain                                                                  //
22 // Files are written to FXS                                                 //
23 //                                                                          //
24 // Author: F. Prino (prino@to.infn.it)                                      //
25 //                                                                          //
26 //////////////////////////////////////////////////////////////////////////////
27
28
29
30 extern "C" {
31 #include "daqDA.h"
32 }
33
34 #include "event.h"
35 #include "monitor.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 // ROOT includes
41 #include <TFile.h>
42 #include <TH1F.h>
43 #include <TH2F.h>
44 #include <TSystem.h>
45 #include <TROOT.h>
46 #include <TPluginManager.h>
47
48 // AliRoot includes
49 #include "AliRawReaderDate.h"
50 #include "AliITSOnlineSDDTP.h"
51 #include "AliITSRawStreamSDD.h"
52 /* Main routine
53       Arguments: list of DATE raw data files
54 */
55 int main(int argc, char **argv) {
56
57   int status = 0;
58
59   // line added to solve IO problems
60   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
61                                         "*",
62                                         "TStreamerInfo",
63                                         "RIO",
64                                         "TStreamerInfo()");
65
66   /* log start of process */
67   printf("ITS SDD TEST-PULSE algorithm program started\n");  
68
69
70   /* check that we got some arguments = list of files */
71   if (argc<2) {
72     printf("Wrong number of arguments\n");
73     return -1;
74   }
75
76
77   Int_t maxNEvents=15; // maximum number of events to be analyzed
78   const Int_t kTotDDL=24;
79   const Int_t kModPerDDL=12;
80   const Int_t kSides=2;
81   Int_t adcSamplFreq=40;
82   Bool_t readfeeconf=kFALSE;
83   gSystem->Exec("rm -f  SDDbase_LDC.tar");
84    if(gSystem->Getenv("DAQ_DETDB_LOCAL")!=NULL){
85     const char* dir=gSystem->Getenv("DAQ_DETDB_LOCAL");    
86     TString filnam=Form("%s/fee.conf",dir); 
87     FILE* feefil=fopen(filnam.Data(),"r"); 
88     if(feefil){
89       fscanf(feefil,"%d \n",&adcSamplFreq);
90       TString shcomm=Form("tar -rf SDDbase_LDC.tar -C %s fee.conf",dir); 
91       gSystem->Exec(shcomm.Data());
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   AliITSOnlineSDDTP **tpan=new AliITSOnlineSDDTP*[kTotDDL*kModPerDDL*kSides];
100   TH2F **histo=new TH2F*[kTotDDL*kModPerDDL*kSides];
101   Bool_t isFilled[kTotDDL*kModPerDDL*kSides];
102   Char_t hisnam[20];
103   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
104     for(Int_t imod=0; imod<kModPerDDL;imod++){
105       for(Int_t isid=0;isid<kSides;isid++){
106         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
107         tpan[index]=new AliITSOnlineSDDTP(iddl,imod,isid,100.);
108         if(adcSamplFreq==20) tpan[index]->SetLastGoodTB(126);
109         else tpan[index]->SetLastGoodTB(254);
110         sprintf(hisnam,"h%02dc%02ds%d",iddl,imod,isid);
111         histo[index]=new TH2F(hisnam,"",256,-0.5,255.5,256,-0.5,255.5);
112         isFilled[index]=0;
113       }
114     }
115   }
116   
117   /* report progress */
118   daqDA_progressReport(10);
119   Int_t iev=0,iAnalyzedEv=0;
120   /* read the data files */
121   int n;
122   for (n=1;n<argc;n++) {
123    
124     status=monitorSetDataSource( argv[n] );
125     if (status!=0) {
126       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
127       return -1;
128     }
129
130     /* report progress */
131     /* in this example, indexed on the number of files */
132     // Progress report inside the event loop as well?
133     daqDA_progressReport(10+80*n/argc);
134
135     /* read the file */
136     for(;;) {
137       struct eventHeaderStruct *event;
138       eventTypeType eventT;
139
140       /* get next event */
141       status=monitorGetEventDynamic((void **)&event);
142       if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
143       if (status!=0) {
144         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
145         return -1;
146       }
147
148       /* retry if got no event */
149       if (event==NULL) {
150         break;
151       }
152
153       if(iAnalyzedEv>=maxNEvents) break;
154       iev++; 
155       
156       /* use event - here, just write event id to result file */
157       eventT=event->eventType;
158       switch (event->eventType){
159         
160         /* START OF RUN */
161       case START_OF_RUN:
162         break;
163         
164         /* END OF RUN */
165       case END_OF_RUN:
166         break;
167         
168         //      case PHYSICS_EVENT:  // comment this line for test raw data
169         //      break;               // comment this line for test raw data
170
171       case CALIBRATION_EVENT:
172         break;  // uncomment this line for test raw data
173       case PHYSICS_EVENT: // uncomment this line for test raw data
174         printf(" event number = %i \n",iev);
175         AliRawReader *rawReader = new AliRawReaderDate((void*)event);
176
177         Int_t evtyp=0;
178         while(rawReader->ReadHeader()){
179           const UInt_t *subev = rawReader->GetSubEventAttributes();
180           if(subev[0]==0 && subev[1]==0 && subev[2]==0) evtyp=1; 
181         }
182
183         rawReader->Reset();
184         for(Int_t iddl=0; iddl<kTotDDL;iddl++){
185           for(Int_t imod=0; imod<kModPerDDL;imod++){
186             for(Int_t isid=0;isid<kSides;isid++){
187               Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
188               histo[index]->Reset();
189             }
190           }
191         }
192         AliITSRawStreamSDD s(rawReader);
193         
194         while(s.Next()){
195           Int_t iDDL=rawReader->GetDDLID();
196           Int_t iCarlos=s.GetCarlosId();
197           if(s.IsCompletedModule()) continue;
198           if(s.IsCompletedDDL()) continue;
199           if(iDDL>=0 && iDDL<kTotDDL){ 
200             Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s.GetChannel(); 
201             histo[index]->Fill(s.GetCoord2(),s.GetCoord1(),s.GetSignal());
202             isFilled[index]=1;
203           }
204         }
205         delete rawReader;
206         for(Int_t iddl=0; iddl<kTotDDL;iddl++){
207           for(Int_t imod=0; imod<kModPerDDL;imod++){
208             for(Int_t isid=0;isid<kSides;isid++){
209               Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
210               if(isFilled[index]) tpan[index]->AddEvent(histo[index]);    
211             }
212           }
213         }
214         
215         /* free resources */
216         iAnalyzedEv++;
217         free(event);
218       }
219     }
220     
221   }
222     
223   TFile *fh=new TFile("SDDgainHistos.root","RECREATE");
224   Char_t filnam[100],command[120];
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(isFilled[index]){
230           tpan[index]->ValidateAnodes();
231           tpan[index]->WriteToASCII();
232           tpan[index]->WriteToROOT(fh);
233           sprintf(filnam,"SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
234           sprintf(command,"tar -rf SDDbase_LDC.tar %s",filnam);
235           gSystem->Exec(command);
236         }
237       }
238     }  
239   }
240   fh->Close();
241
242   /* write report */
243   printf("Run #%s, received %d calibration events\n",getenv("DATE_RUN_NUMBER"),iAnalyzedEv);
244
245   /* report progress */
246   daqDA_progressReport(90);
247
248
249
250   fh->Close();
251
252
253
254   status=daqDA_FES_storeFile("./SDDbase_LDC.tar","SDD_Calib");
255
256   /* report progress */
257   daqDA_progressReport(100);
258
259
260
261   return status;
262 }