Fixing a little overlap. Adding flags to perform material budget studies (Mario)
[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   // main - Arguments: list of DATE raw data files
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
119   Int_t iev;
120   Int_t ievPed;
121   Int_t ievUsed;
122   Int_t nEvToBeSkipped=5;
123
124   for(Int_t iStep=0;iStep<2;iStep++){
125     printf("Start Analysis Step %d\n",iStep);
126     iev=0;
127     ievPed=0;
128     ievUsed=0;
129     if(iStep==1){
130       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
131         for(Int_t imod=0; imod<kModPerDDL;imod++){
132           for(Int_t isid=0;isid<kSides;isid++){
133             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
134             corr[index]=new AliITSOnlineSDDCMN(iddl,imod,isid);
135             isFilled[index]=0;
136           }
137         }
138       }
139     }
140
141     /* read the data files */
142     int n;
143     for (n=1;n<argc;n++) {
144    
145       status=monitorSetDataSource( argv[n] );
146       if (status!=0) {
147         printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
148         return -1;
149       }
150
151       /* report progress */
152       /* in this example, indexed on the number of files */
153       // Progress report inside the event loop as well?
154       daqDA_progressReport(10+40*iStep*n/argc);
155
156       /* read the file */
157       for(;;) {
158         struct eventHeaderStruct *event;
159         eventTypeType eventT;
160
161         /* get next event */
162         status=monitorGetEventDynamic((void **)&event);
163         if (status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
164         if (status!=0) {
165           printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
166           return -1;
167         }
168
169         /* retry if got no event */
170         if (event==NULL) {
171           break;
172         }
173
174         iev++; 
175
176         /* use event - here, just write event id to result file */
177         eventT=event->eventType;
178         switch (event->eventType){
179       
180           /* START OF RUN */
181         case START_OF_RUN:
182           break;
183       
184           /* END OF RUN */
185         case END_OF_RUN:
186           break;
187
188           //      case PHYSICS_EVENT:  // comment this line for test raw data
189           //    break;               // comment this line for test raw data
190
191
192         case CALIBRATION_EVENT:
193           break;  // uncomment this line for test raw data
194         case PHYSICS_EVENT: // uncomment this line for test raw data
195           printf(" Event number = %i ",iev);
196           ievPed++;
197           if(ievPed<=nEvToBeSkipped){
198             printf(" -> SKIP\n");
199             break;
200           }
201           printf("  -> Analyze\n");
202           ievUsed++;
203           AliRawReader *rawReader = new AliRawReaderDate((void*)event);
204           rawReader->Reset();
205           UChar_t cdhAttr=AliITSRawStreamSDD::ReadBlockAttributes(rawReader);
206           amSamplFreq=AliITSRawStreamSDD::ReadAMSamplFreqFromCDH(cdhAttr);
207           AliITSRawStream* s=AliITSRawStreamSDD::CreateRawStreamSDD(rawReader,cdhAttr);
208           if(!writtenoutput){
209             printf("Use %s raw stream, sampling frequency %d MHz\n",s->ClassName(),amSamplFreq);
210             writtenoutput=kTRUE;
211           }
212
213
214           for(Int_t iddl=0; iddl<kTotDDL;iddl++){
215             for(Int_t imod=0; imod<kModPerDDL;imod++){
216               for(Int_t isid=0;isid<kSides;isid++){
217                 Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
218                 histo[index]->Reset();
219               }
220             }
221           }
222
223           while(s->Next()){
224             Int_t iDDL=rawReader->GetDDLID();
225             Int_t iCarlos=s->GetCarlosId();
226             if(s->IsCompletedModule()) continue;
227             if(s->IsCompletedDDL()) continue;
228             if(iDDL>=0 && iDDL<kTotDDL){ 
229               Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s->GetChannel(); 
230               histo[index]->Fill(s->GetCoord2(),s->GetCoord1(),s->GetSignal());
231               isFilled[index]=1;
232             }
233           }
234           delete s;
235           delete rawReader;
236           for(Int_t iddl=0; iddl<kTotDDL;iddl++){
237             for(Int_t imod=0; imod<kModPerDDL;imod++){
238               for(Int_t isid=0;isid<kSides;isid++){
239                 Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
240                 if(iStep==0){
241                   if(amSamplFreq==20) base[index]->SetLastGoodTB(126);
242                   else base[index]->SetLastGoodTB(254);
243                   base[index]->AddEvent(histo[index]);
244                 }
245                 if(iStep==1){
246                   if(amSamplFreq==20) corr[index]->SetLastGoodTB(126);
247                   else corr[index]->SetLastGoodTB(254);
248                   corr[index]->AddEvent(histo[index]);
249                 }
250               }
251             }
252           }
253
254           /* free resources */
255           free(event);
256         }
257         if(ievUsed>=maxNEvents) break;
258       }
259     }
260
261     if(iStep==0){
262       for(Int_t iddl=0; iddl<kTotDDL;iddl++){
263         for(Int_t imod=0; imod<kModPerDDL;imod++){
264           for(Int_t isid=0;isid<kSides;isid++){
265             Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
266             base[index]->ValidateAnodes();
267             base[index]->WriteToASCII();
268           }
269         }
270       }
271     }
272   }
273
274   /* write report */
275   TDatime time;
276   TObjString timeinfo(Form("%02d%02d%02d%02d%02d%02d",time.GetYear()-2000,time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond()));
277   printf("Run #%s, received %d calibration events, time %s\n",getenv("DATE_RUN_NUMBER"),ievUsed,timeinfo.GetString().Data());
278
279   /* report progress */
280   daqDA_progressReport(90);
281
282   TObjArray* basHistos=new TObjArray();
283   TObjArray* noiseHistos=new TObjArray();
284   TObjArray* cmnHistos=new TObjArray();
285   TObjArray* corrnHistos=new TObjArray();
286   TObjArray* statusHistos=new TObjArray();
287
288
289   Char_t filnam[100],command[150];
290   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
291     for(Int_t imod=0; imod<kModPerDDL;imod++){
292       for(Int_t isid=0;isid<kSides;isid++){
293         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
294         corr[index]->ValidateAnodes();
295         corr[index]->WriteToASCII();
296         if(isFilled[index]){
297           basHistos->AddLast(corr[index]->GetBaselineAnodeHisto());
298           noiseHistos->AddLast(corr[index]->GetRawNoiseAnodeHisto());
299           cmnHistos->AddLast(corr[index]->GetCMNCoefAnodeHisto());
300           corrnHistos->AddLast(corr[index]->GetCorrNoiseAnodeHisto());
301           statusHistos->AddLast(corr[index]->GetStatusAnodeHisto());
302           sprintf(filnam,"SDDbase_step2_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
303           sprintf(command,"tar -rf SDDbase_step2_LDC.tar %s",filnam);
304           gSystem->Exec(command);
305         }
306       }
307     }
308   }
309
310 #ifdef ALI_AMORE
311   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
312   Int_t statusamore =0;
313   statusamore += amoreDA.Send("TimeInfoPedestal",&timeinfo);
314   statusamore += amoreDA.Send("Baselines",basHistos);
315   statusamore += amoreDA.Send("RawNoise",noiseHistos);
316   statusamore += amoreDA.Send("CommonMode",cmnHistos);
317   statusamore += amoreDA.Send("CorrectedNoise",corrnHistos);
318   statusamore += amoreDA.Send("NoisyChannels",statusHistos);
319   if ( statusamore )
320     printf("Warning: Failed to write Arrays in the AMORE database\n");
321   else 
322     printf("amoreDA.Send() OK\n");
323 #else
324   printf("Warning: SDDBAS DA not compiled with AMORE support\n");
325 #endif
326     
327     
328   TFile *fh=new TFile("SDDbaseHistos.root","RECREATE");
329   basHistos->Write();
330   noiseHistos->Write();
331   cmnHistos->Write();
332   corrnHistos->Write();
333   statusHistos->Write();
334   fh->Close();
335
336   /* report progress */
337   daqDA_progressReport(100);
338
339
340
341   return status;
342 }