]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCPEDESTALda.cxx
d77a374e026dd5373399cfd03905411ec9262398
[u/mrichter/AliRoot.git] / TPC / TPCPEDESTALda.cxx
1 /*
2
3 TPCda_pedestal.cxx - calibration algorithm for TPC pedestal runs
4
5 10/06/2007  sylvain.chapeland@cern.ch :  first version - clean skeleton based on DAQ DA case1
6
7 contact: marian.ivanov@cern.ch
8
9
10 This process reads RAW data from the files provided as command line arguments
11 and save results in a file (named from RESULT_FILE define - see below).
12
13 */
14
15 #define RESULT_FILE "tpcPedestal.root"
16
17
18 extern "C" {
19 #include <daqDA.h>
20 }
21 #include "event.h"
22 #include "monitor.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fstream.h>
26
27 //
28 //Root includes
29 //
30 #include <TFile.h>
31 #include <TArrayF.h>
32 #include "TROOT.h"
33 #include "TPluginManager.h"
34
35 //
36 //AliRoot includes
37 //
38 #include "AliRawReader.h"
39 #include "AliRawReaderDate.h"
40 #include "AliTPCmapper.h"
41 #include "AliTPCRawStream.h"
42 #include "AliTPCROC.h"
43 #include "AliTPCCalROC.h"
44 #include "AliTPCCalPad.h"
45 #include "AliMathBase.h"
46 #include "TTreeStream.h"
47
48 //
49 // TPC calibration algorithm includes
50 //
51 #include "AliTPCCalibPedestal.h"
52
53 /*
54   Main routine, TPC pedestal detector algorithm to be run on TPC LDC
55   Arguments: list of DATE raw data files
56 */
57
58 int main(int argc, char **argv) {
59   //
60   // Main for TPC pedestal detector algorithm
61   //
62
63   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
64                                         "*",
65                                         "TStreamerInfo",
66                                         "RIO",
67                                         "TStreamerInfo()"); 
68   Bool_t timeAnalysis = kTRUE;
69
70   int i,status;
71   AliTPCCalibPedestal calibPedestal;           // pedestal and noise calibration
72   calibPedestal.SetTimeAnalysis(timeAnalysis); // pedestal(t) calibration
73
74   if (argc<2) {
75     printf("Wrong number of arguments\n");
76     return -1;
77   }
78
79   /* log start of process */
80   printf("TPC DA started - %s\n",__FILE__);
81
82   /* declare monitoring program */
83   status=monitorDeclareMp( __FILE__ );
84   if (status!=0) {
85     printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
86     return -1;
87   }
88
89   /* loop over RAW data files */
90   int nevents=0;
91   for ( i=1; i<argc; i++ ) {
92
93     /* define data source : this is argument i */
94     printf("Processing file %s\n", argv[i]);
95     status=monitorSetDataSource( argv[i] );
96     if (status!=0) {
97       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
98       return -1;
99     }
100
101     /* read until EOF */
102     for ( ; ; ) {
103       struct eventHeaderStruct *event;
104
105       /* check shutdown condition */
106       if (daqDA_checkShutdown()) {break;}
107
108       /* get next event (blocking call until timeout) */
109       status=monitorGetEventDynamic((void **)&event);
110       if (status==MON_ERR_EOF) {
111         printf ("End of File %d (%s) detected\n", i, argv[i]);
112         break; /* end of monitoring file has been reached */
113       }
114       if (status!=0) {
115         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
116         break;
117       }
118
119       /* skip start/end of run events */
120       if ( (event->eventType != physicsEvent) && (event->eventType != calibrationEvent) )
121         continue;
122
123       /* retry if got no event */
124       if (event==NULL) {
125         continue;
126       }
127
128       nevents++;
129
130       //  Pedestal calibration
131       AliRawReader *rawReader = new AliRawReaderDate((void*)event);
132       calibPedestal.ProcessEvent(rawReader);
133       delete rawReader;
134
135       /* free resources */
136       free(event);
137     }
138   }
139
140   calibPedestal.Analyse(); 
141   calibPedestal.AnalyseTime(nevents); 
142   printf ("%d physics/calibration events processed\n",nevents);
143
144   TFile *fileTPC = new TFile(RESULT_FILE, "recreate");
145   calibPedestal.Write("calibPedestal");
146   delete fileTPC;
147   printf("Wrote %s\n",RESULT_FILE);
148
149   //
150   // Now prepare ASCII files for local ALTRO configuration through DDL
151   //
152
153   ofstream pedfile;
154   ofstream noisefile;
155   ofstream pedmemfile;
156   char filename[255];
157   sprintf(filename,"tpcPedestals.data");
158   pedfile.open(filename);
159   sprintf(filename,"tpcNoise.data");
160   noisefile.open(filename);
161   sprintf(filename,"tpcPedestalMem.data");
162   pedmemfile.open(filename);
163
164   TArrayF **timePed = calibPedestal.GetTimePedestals();
165   AliTPCmapper mapping;
166
167   Int_t ctr_channel = 0;
168   Int_t ctr_altro = 0;
169   Int_t ctr_pattern = 0;
170
171   pedfile    << 10 << std::endl; // PEDESTALS per channel
172   noisefile  << 11 << std::endl; // NOISE per altro
173   pedmemfile << 12 << std::endl; // PEDESTALs per time bin
174
175   for ( Int_t roc = 0; roc < 72; roc++ ) {
176     if ( !calibPedestal.GetCalRocPedestal(roc) ) continue;
177     Int_t side   = mapping.GetSideFromRoc(roc);
178     Int_t sector = mapping.GetSectorFromRoc(roc);
179     //printf("Analysing ROC %d (side %d, sector %d) ...\n", roc, side, sector);
180     Int_t nru = mapping.IsIROC(roc) ? 2 : 4;
181     for ( int rcu = 0; rcu < nru; rcu++ ) {
182       Int_t patch = mapping.IsIROC(roc) ? rcu : rcu+2;
183       for ( int branch = 0; branch < 2; branch++ ) {
184         for ( int fec = 0; fec < mapping.GetNfec(patch, branch); fec++ ) {
185           for ( int altro = 0; altro < 8; altro++ ) {
186             Float_t rms = 0.;
187             Float_t ctr = 0.;
188             for ( int channel = 0; channel < 16; channel++ ) {
189               Int_t hwadd = mapping.CodeHWAddress(branch, fec, altro, channel);
190               Int_t row   = mapping.GetPadRow(patch, hwadd);
191               Int_t pad   = mapping.GetPad(patch, hwadd);
192               Float_t ped = calibPedestal.GetCalRocPedestal(roc)->GetValue(row,pad);
193               // fixed pedestal
194               if ( ped > 1.e-10 ) {
195                 pedfile << ctr_channel << "\t" << side << "\t" << sector << "\t" << patch << "\t" << hwadd << "\t" << ped << std::endl;
196                 ctr_channel++;
197               }
198               // pedestal(t)
199               if ( timePed && fabs(timePed[row][pad].GetSum()) > 1e-10 ) {
200                 pedmemfile << ctr_pattern << "\t" << side << "\t" << sector << "\t" << patch << "\t" << hwadd;
201                 for ( Int_t timebin = 0; timebin < 1024; timebin++ )
202                   pedmemfile << "\t" << timePed[row][pad].At(timebin);
203                 pedmemfile << std::endl;
204                 ctr_pattern++;
205               }
206               // rms=noise
207               Float_t rms2 = calibPedestal.GetCalRocRMS(roc)->GetValue(row,pad);
208               if ( rms2 > 1.e-10 ) { rms += rms2; ctr += 1.; }
209             } // end channel for loop
210             // noise data (rms) averaged over all channels in this ALTRO.
211             Int_t hwadd = mapping.CodeHWAddress(branch, fec, altro, 0);
212             if ( ctr > 1.e-10 ) {
213               noisefile << ctr_altro << "\t" << side << "\t" << sector << "\t" << patch << "\t" << hwadd << "\t" << rms/ctr << std::endl;
214               ctr_altro++;
215             }
216           } // end altro for loop
217         } // end fec for loop
218       } // end branch for loop
219     } // end rcu for loop
220   } // end roc loop
221
222   pedfile.close();
223   noisefile.close();
224   pedmemfile.close();
225
226   printf("Wrote ASCII files\n");
227
228   return status;
229 }