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