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