Update for Ds
[u/mrichter/AliRoot.git] / ITS / ITSSDDGAINda.cxx
1 /*
2 - Contact: - prino@to.infn.it
3 - Link: - alien:///alice/data/2009/LHC09c_SDD/000079095/raw/09000079095024.10.root
4 - Run Type: - PULSER_RUN
5 - DA Type: - LDC
6 - Number of events needed: >15
7 - Input Files: - SDDbase_step1_ddl*c*_sid*.data (output of previous PEDESTAL run)
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 #include <TObjArray.h>
48 #include <TObjString.h>
49 #include <TDatime.h>
50
51 // AliRoot includes
52 #include "AliRawReaderDate.h"
53 #include "AliITSOnlineSDDTP.h"
54 #include "AliITSRawStreamSDD.h"
55 #include "AliITSRawStreamSDDCompressed.h"
56
57 #ifdef ALI_AMORE
58 #include <AmoreDA.h>
59 #endif
60 /* Main routine
61       Arguments: list of DATE raw data files
62 */
63 int main(int argc, char **argv) {
64
65   int status = 0;
66
67   // line added to solve IO problems
68   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
69                                         "*",
70                                         "TStreamerInfo",
71                                         "RIO",
72                                         "TStreamerInfo()");
73
74   /* log start of process */
75   printf("ITS SDD TEST-PULSE algorithm program started\n");  
76
77
78   /* check that we got some arguments = list of files */
79   if (argc<2) {
80     printf("Wrong number of arguments\n");
81     return -1;
82   }
83
84
85   Int_t maxNEvents=15; // maximum number of events to be analyzed
86   const Int_t kTotDDL=24;
87   const Int_t kModPerDDL=12;
88   const Int_t kSides=2;
89   UInt_t amSamplFreq=40;
90   UChar_t cdhAttr=0;
91
92   gSystem->Exec("rm -f  SDDbase_LDC.tar");
93
94   AliITSOnlineSDDTP **tpan=new AliITSOnlineSDDTP*[kTotDDL*kModPerDDL*kSides];
95   TH2F **histo=new TH2F*[kTotDDL*kModPerDDL*kSides];
96   Bool_t isFilled[kTotDDL*kModPerDDL*kSides];
97   Bool_t writtenoutput=kFALSE;
98   Char_t hisnam[20];
99   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
100     for(Int_t imod=0; imod<kModPerDDL;imod++){
101       for(Int_t isid=0;isid<kSides;isid++){
102         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
103         tpan[index]=new AliITSOnlineSDDTP(iddl,imod,isid,100.);
104         sprintf(hisnam,"h%02dc%02ds%d",iddl,imod,isid);
105         histo[index]=new TH2F(hisnam,"",256,-0.5,255.5,256,-0.5,255.5);
106         isFilled[index]=0;
107       }
108     }
109   }
110   
111   /* report progress */
112   daqDA_progressReport(10);
113   Int_t iev=0,iAnalyzedEv=0;
114   /* read the data files */
115   int n;
116   for (n=1;n<argc;n++) {
117    
118     status=monitorSetDataSource( argv[n] );
119     if (status!=0) {
120       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
121       return -1;
122     }
123
124     /* report progress */
125     /* in this example, indexed on the number of files */
126     // Progress report inside the event loop as well?
127     daqDA_progressReport(10+80*n/argc);
128
129     /* read the file */
130     for(;;) {
131       struct eventHeaderStruct *event;
132       eventTypeType eventT;
133
134       /* get next event */
135       status=monitorGetEventDynamic((void **)&event);
136       if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
137       if (status!=0) {
138         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
139         return -1;
140       }
141
142       /* retry if got no event */
143       if (event==NULL) {
144         break;
145       }
146
147       if(iAnalyzedEv>=maxNEvents) break;
148       iev++; 
149       
150       /* use event - here, just write event id to result file */
151       eventT=event->eventType;
152       switch (event->eventType){
153         
154         /* START OF RUN */
155       case START_OF_RUN:
156         break;
157         
158         /* END OF RUN */
159       case END_OF_RUN:
160         break;
161         
162         //      case PHYSICS_EVENT:  // comment this line for test raw data
163         //      break;               // comment this line for test raw data
164
165       case CALIBRATION_EVENT:
166         break;  // uncomment this line for test raw data
167       case PHYSICS_EVENT: // uncomment this line for test raw data
168         printf(" event number = %i \n",iev);
169         AliRawReader *rawReader = new AliRawReaderDate((void*)event);
170         rawReader->Reset();
171         cdhAttr=AliITSRawStreamSDD::ReadBlockAttributes(rawReader);
172         amSamplFreq=AliITSRawStreamSDD::ReadAMSamplFreqFromCDH(cdhAttr);
173         AliITSRawStream* s=AliITSRawStreamSDD::CreateRawStreamSDD(rawReader,cdhAttr);
174         if(!writtenoutput){
175           printf("Use %s raw stream, sampling frequency %d MHz\n",s->ClassName(),amSamplFreq);
176           writtenoutput=kTRUE;
177         }
178
179         for(Int_t iddl=0; iddl<kTotDDL;iddl++){
180           for(Int_t imod=0; imod<kModPerDDL;imod++){
181             for(Int_t isid=0;isid<kSides;isid++){
182               Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
183               histo[index]->Reset();
184             }
185           }
186         }
187
188         while(s->Next()){
189           Int_t iDDL=rawReader->GetDDLID();
190           Int_t iCarlos=s->GetCarlosId();
191           if(s->IsCompletedModule()) continue;
192           if(s->IsCompletedDDL()) continue;
193           if(iDDL>=0 && iDDL<kTotDDL){ 
194             Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s->GetChannel(); 
195             histo[index]->Fill(s->GetCoord2(),s->GetCoord1(),s->GetSignal());
196             isFilled[index]=1;
197           }
198         }
199         delete s;
200         delete rawReader;
201         for(Int_t iddl=0; iddl<kTotDDL;iddl++){
202           for(Int_t imod=0; imod<kModPerDDL;imod++){
203             for(Int_t isid=0;isid<kSides;isid++){
204               Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
205               if(amSamplFreq==20) tpan[index]->SetLastGoodTB(126);
206               else tpan[index]->SetLastGoodTB(254);
207               if(isFilled[index]) tpan[index]->AddEvent(histo[index]);    
208             }
209           }
210         }
211         
212         /* free resources */
213         iAnalyzedEv++;
214         free(event);
215       }
216     }
217     
218   }
219     
220   /* write report */
221   TDatime time;
222   TObjString timeinfo(Form("%02d%02d%02d%02d%02d%02d",time.GetYear()-2000,time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond()));
223   printf("Run #%s, received %d calibration events, time %s\n",getenv("DATE_RUN_NUMBER"),iAnalyzedEv,timeinfo.GetString().Data());
224
225   /* report progress */
226   daqDA_progressReport(90);
227
228   TObjArray* gainHistos=new TObjArray();
229   TObjArray* statusHistos=new TObjArray();
230   
231   Char_t filnam[100],command[120];
232   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
233     for(Int_t imod=0; imod<kModPerDDL;imod++){
234       for(Int_t isid=0;isid<kSides;isid++){
235         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
236         if(isFilled[index]){
237           tpan[index]->ValidateAnodes();
238           tpan[index]->WriteToASCII();
239           gainHistos->AddLast(tpan[index]->GetGainAnodeHisto());
240           statusHistos->AddLast(tpan[index]->GetStatusAnodeHisto());
241           sprintf(filnam,"SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
242           sprintf(command,"tar -rf SDDbase_LDC.tar %s",filnam);
243           gSystem->Exec(command);
244         }
245       }
246     }  
247   }
248
249
250   FILE *conffil=fopen("fee.conf","w");
251   fprintf(conffil,"%d\n",amSamplFreq);
252   fprintf(conffil,"%02X\n",cdhAttr);
253   fclose(conffil);
254   gSystem->Exec("tar -rf SDDbase_LDC.tar fee.conf");
255   status=daqDA_FES_storeFile("./SDDbase_LDC.tar","SDD_Calib");
256
257
258 #ifdef ALI_AMORE
259   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
260   Int_t statusamore =0;
261   statusamore += amoreDA.Send("TimeInfoPulser",&timeinfo);
262   statusamore += amoreDA.Send("Gain",gainHistos);
263   if ( statusamore )
264     printf("Warning: Failed to write Arrays in the AMORE database\n");
265   else 
266     printf("amoreDA.Send() OK\n");
267 #else
268   printf("Warning: SDDGAIN DA not compiled with AMORE support\n");
269 #endif
270     
271  
272   TFile *fh=new TFile("SDDgainHistos.root","RECREATE");
273   gainHistos->Write();
274   statusHistos->Write();
275   fh->Close();
276
277
278   /* report progress */
279   daqDA_progressReport(100);
280
281
282
283   return status;
284 }