]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/PHOSGAINda.cxx
Hardcoded module number removed; ZS parameters reads from the file (if any).
[u/mrichter/AliRoot.git] / PHOS / PHOSGAINda.cxx
1 /*
2 contact: Boris.Polishchuk@cern.ch
3 link: see comments in the $ALICE_ROOT/PHOS/AliPHOSRcuDA1.cxx
4 reference run: /castor/cern.ch/alice/phos/2007/10/04/18/07000008249001.1000.root
5 run type: PHYSICS
6 DA type: MON 
7 number of events needed: 1000
8 input files: RCU0.data  RCU1.data  RCU2.data  RCU3.data
9 Output files: PHOS_Module2_Calib.root
10 Trigger types used: PHYSICS
11 */
12
13
14 #include "event.h"
15 #include "monitor.h"
16 extern "C" {
17 #include "daqDA.h"
18 }
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <TSystem.h>
24 #include <TROOT.h>
25 #include <TPluginManager.h>
26
27 #include "AliRawReader.h"
28 #include "AliRawReaderDate.h"
29 #include "AliPHOSRcuDA1.h"
30 #include "AliPHOSRawFitterv0.h"
31 #include "AliCaloAltroMapping.h"
32 #include "AliCaloRawStreamV3.h"
33
34
35 /* Main routine
36       Arguments: 
37       1- monitoring data source
38 */
39 int main(int argc, char **argv) {
40
41   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
42                                         "*",
43                                         "TStreamerInfo",
44                                         "RIO",
45                                         "TStreamerInfo()");
46
47   int status;
48   
49   if (argc!=2) {
50     printf("Wrong number of arguments\n");
51     return -1;
52   }
53   
54   /* Retrieve ZS parameters from DAQ DB */
55   const char* zsfile = "zs.txt";
56   int failZS = daqDA_DB_getFile(zsfile, zsfile);
57   
58   Int_t offset,threshold;
59   
60   if(!failZS) {
61     FILE *f = fopen(zsfile,"r");
62     int scan = fscanf(f,"%d %d",&offset,&threshold);
63   }
64   
65   /* Retrieve mapping files from DAQ DB */ 
66   const char* mapFiles[4] = {"RCU0.data","RCU1.data","RCU2.data","RCU3.data"};
67   
68   for(Int_t iFile=0; iFile<4; iFile++) {
69     int failed = daqDA_DB_getFile(mapFiles[iFile], mapFiles[iFile]);
70     if(failed) { 
71       printf("Cannot retrieve file %s from DAQ DB. Exit.\n",mapFiles[iFile]);
72       return -1;
73     }
74   }
75   
76   /* Open mapping files */
77   AliAltroMapping *mapping[4];
78   TString path = "./";
79   path += "RCU";
80   TString path2;
81   for(Int_t i = 0; i < 4; i++) {
82     path2 = path;
83     path2 += i;
84     path2 += ".data";
85     mapping[i] = new AliCaloAltroMapping(path2.Data());
86   }
87   
88   /* define data source : this is argument 1 */  
89   status=monitorSetDataSource( argv[1] );
90   if (status!=0) {
91     printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
92     return -1;
93   }
94
95
96   /* declare monitoring program */
97   status=monitorDeclareMp( __FILE__ );
98   if (status!=0) {
99     printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
100     return -1;
101   }
102
103
104   /* define wait event timeout - 1s max */
105   monitorSetNowait();
106   monitorSetNoWaitNetworkTimeout(1000);
107   
108    /* init some counters */
109   int nevents_physics=0;
110   int nevents_total=0;
111
112   AliRawReader *rawReader = NULL;
113   AliPHOSRcuDA1* dAs[5];
114
115   for(Int_t iMod=0; iMod<5; iMod++) {
116     dAs[iMod] = 0;
117   }
118   
119   Float_t e[64][56][2];
120   Float_t t[64][56][2];
121
122   for(Int_t iX=0; iX<64; iX++) {
123     for(Int_t iZ=0; iZ<56; iZ++) {
124       for(Int_t iGain=0; iGain<2; iGain++) {
125         e[iX][iZ][iGain] = 0.;
126         t[iX][iZ][iGain] = 0.;
127       }
128     }
129   }
130   
131   Int_t cellX    = -1;
132   Int_t cellZ    = -1;
133   Int_t nBunches =  0;
134   Int_t sigStart, sigLength;
135   Int_t caloFlag;
136
137   /* main loop (infinite) */
138   for(;;) {
139     struct eventHeaderStruct *event;
140     eventTypeType eventT;
141   
142     /* check shutdown condition */
143     if (daqDA_checkShutdown()) {break;}
144     
145     /* get next event (blocking call until timeout) */
146     status=monitorGetEventDynamic((void **)&event);
147     if (status==MON_ERR_EOF) {
148       printf ("End of File detected\n");
149       break; /* end of monitoring file has been reached */
150     }
151     
152     if (status!=0) {
153       printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
154       break;
155     }
156
157     /* retry if got no event */
158     if (event==NULL) {
159       continue;
160     }
161
162
163     /* use event - here, just write event id to result file */
164     eventT=event->eventType;
165     
166     if (eventT==PHYSICS_EVENT) {
167       
168       rawReader = new AliRawReaderDate((void*)event);
169       AliCaloRawStreamV3 stream(rawReader,"PHOS",mapping);
170       AliPHOSRawFitterv0 fitter;
171       fitter.SubtractPedestals(kTRUE); // assume that data is non-ZS
172       
173       if(!failZS) {
174         fitter.SubtractPedestals(kFALSE);
175         fitter.SetAmpOffset(offset);
176         fitter.SetAmpThreshold(threshold);
177       }
178       
179       while (stream.NextDDL()) {
180         while (stream.NextChannel()) {
181
182           cellX    = stream.GetCellX();
183           cellZ    = stream.GetCellZ();
184           caloFlag = stream.GetCaloFlag();  // 0=LG, 1=HG, 2=TRU
185
186           // In case of oscillating signals with ZS, 
187           //a channel can have several bunches.
188           
189           nBunches = 0;
190           while (stream.NextBunch()) {
191             nBunches++;
192             if (nBunches > 1) continue;
193             sigStart  = stream.GetStartTimeBin();
194             sigLength = stream.GetBunchLength();
195             fitter.SetSamples(stream.GetSignals(),sigStart,sigLength);
196           } // End of NextBunch()
197           
198           fitter.SetNBunches(nBunches);
199           fitter.SetChannelGeo(stream.GetModule(),cellX,cellZ,caloFlag);
200           fitter.Eval();
201           
202           if (nBunches>1) continue;
203           
204           e[cellX][cellZ][caloFlag] = fitter.GetEnergy();
205           t[cellX][cellZ][caloFlag] = fitter.GetTime();
206         }
207
208         if(dAs[stream.GetModule()])
209           dAs[stream.GetModule()]->FillHistograms(e,t);
210         else
211           dAs[stream.GetModule()] = new AliPHOSRcuDA1(stream.GetModule(),-1);
212         
213         for(Int_t iX=0; iX<64; iX++) {
214           for(Int_t iZ=0; iZ<56; iZ++) {
215             for(Int_t iGain=0; iGain<2; iGain++) {
216               e[iX][iZ][iGain] = 0.;
217               t[iX][iZ][iGain] = 0.;
218             }
219           }
220         }
221
222       }
223
224 //       da1.FillHistograms(e,t);
225 //     //da1.UpdateHistoFile();
226       
227         delete rawReader;     
228         nevents_physics++;
229     }
230     
231     nevents_total++;
232     
233     /* free resources */
234     free(event);
235     
236     /* exit when last event received, no need to wait for TERM signal */
237     if (eventT==END_OF_RUN) {
238       printf("EOR event detected\n");
239       break;
240     }
241   }
242   
243   for(Int_t i = 0; i < 4; i++) delete mapping[i];  
244   
245   /* Be sure that all histograms are saved */
246   
247   char localfile[128];
248   
249   for(Int_t iMod=0; iMod<5; iMod++) {
250     if(!dAs[iMod]) continue;
251     
252     dAs[iMod]->UpdateHistoFile();
253     dAs[iMod]->SetWriteToFile(kFALSE);    
254     
255     /* Store output files to the File Exchange Server */
256     sprintf(localfile,"PHOS_Module%d_Calib.root",iMod);
257     daqDA_FES_storeFile(localfile,"AMPLITUDES");
258   }
259   
260   return status;
261 }