In the calculation of the jacobian use the transposed matrix instead of its inverse...
[u/mrichter/AliRoot.git] / HMPID / HMPIDda.cxx
CommitLineData
6f14ad73 1/*
a225d309 2
3HMPID DA for online calibration
4
5Contact: Levente.Molnar@ba.infn.it, Giacomo.Volpe@ba.infn.it
6Link: http://richpc1.ba.infn.it/~levente/Files4Public/ValidateHmpidDA/
7Run Type: PEDESTAL -- but we select on the PHYSICS_EVENTS in th HMPIDda.cxx
8DA Type: LDC
9Number of events needed: 1000 events
3c8e86a0 10Input Files: Raw pedestal file, EXTERNAL config file: HmpidSigmaCut.txt on both HMPID LDCs
a225d309 11Output Files: 14 txt files including pedestal values
12Trigger types used: PEDESTAL RUN (selecting on PHYSICS_EVENT)
13
6f14ad73 14*/
a225d309 15
6f14ad73 16extern "C" {
17#include <daqDA.h>
18}
19
20#include "event.h"
21#include "monitor.h"
22
23#include <Riostream.h>
913dd690 24#include "fstream.h"
6f14ad73 25#include <stdio.h>
26#include <stdlib.h>
27
28//AliRoot
ef7257c6 29#include "AliHMPIDRawStream.h"
21f61e25 30#include "AliHMPIDCalib.h"
6f14ad73 31#include "AliRawReaderDate.h"
32#include "AliBitPacking.h"
6f14ad73 33
34//ROOT
3c8e86a0 35#include "TROOT.h"
cdd8604a 36#include "TFile.h"
37#include "TSystem.h"
38#include "TString.h"
6f14ad73 39#include "TObject.h"
77e40af5 40#include "TPluginManager.h"
21f61e25 41
6f14ad73 42
43int main(int argc, char **argv){
44
45 int status;
4969a7fa 46 const Char_t *hmpConfigFile = "HmpDaqDaConfig.txt";
47 const Int_t ddlOffset = 1536;
48 TString hmpIn;
49 TString feeIn;
6f14ad73 50
4969a7fa 51
1e7f6589 52 /* log start of process */
53 printf("HMPID DA program started\n");
77e40af5 54
4969a7fa 55 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo","*","TStreamerInfo","RIO","TStreamerInfo()");
77e40af5 56
6f14ad73 57 /* check that we got some arguments = list of files */
58 if (argc<2) {
59 printf("Wrong number of arguments\n");
60 return -1;
61 }
62
4969a7fa 63 /* copy locally a file from daq detector config db */
64 hmpIn=Form("./%s",hmpConfigFile);
65 status=daqDA_DB_getFile(hmpConfigFile,hmpIn.Data());
66 if (status) { printf("Failed to get HMPID config file status: %d\n",status); return -1; }
6f14ad73 67
68 /* report progress */
69 daqDA_progressReport(10);
70
a225d309 71
72
73 /* define wait event timeout - 1s max */
74 monitorSetNowait();
75 monitorSetNoWaitNetworkTimeout(1000);
6f14ad73 76
cdd8604a 77 /* set local storage of ped files for Fe2C */
78
79
21f61e25 80 /* init the pedestal calculation */
81 AliHMPIDCalib *pCal=new AliHMPIDCalib();
3c8e86a0 82 /* Set the number of sigma cuts inside the file HmpidSigmaCut.txt on BOTH LDCs! */
83 /* If the file is NOT present then the default cut 3 will be used!*/
4969a7fa 84 pCal->SetSigCutFromFile(hmpIn);
cdd8604a 85
3c8e86a0 86 /* ONLY set this option to kTRUE if you want to create the ADC dsitributions for all 161280 pads!!!!*/
87 /* kTRUE is not suggested for production mode b/c of the memory consumption! */
913dd690 88 pCal->SetWriteHistoPads(kFALSE); //use this option for default production useage!!!
89 //pCal->SetWriteHistoPads(kTRUE); //only for expert debug
90 //pCal->SetWriteHistoPads(kTRUE,kTRUE,13); //DO NOT USE THIS OPTION!
21f61e25 91
77e40af5 92
1e7f6589 93 /* init event counter */
913dd690 94 Int_t firstEvt=0;
95 Int_t iEvtNcal=firstEvt; //Start from 1 not 0!
a225d309 96 ULong_t runNum=0;
3c8e86a0 97 ULong_t ldcId=0;
98
1e7f6589 99 int n;
100 for (n=1;n<argc;n++) {
6f14ad73 101
102 status=monitorSetDataSource( argv[n] );
103 if (status!=0) {
104 printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
105 return -1;
106 }
107
108 /* report progress */
109 /* in this example, indexed on the number of files */
110 daqDA_progressReport(10+80*n/argc);
111
1e7f6589 112 for(;;) { // infinite loop
a225d309 113
114 /* check shutdown condition */
115 if (daqDA_checkShutdown()) {break;}
116
6f14ad73 117 struct eventHeaderStruct *event;
118 eventTypeType eventT;
119
120 /* get next event */
121 status=monitorGetEventDynamic((void **)&event);
3e60bb92 122 if (status==MON_ERR_EOF) /* end of monitoring file has been reached */
123 {
124 printf("End of monitoring file has been reached! \n");
125 break;
126 }
127
128
6f14ad73 129 if (status!=0) {
130 printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
ef7257c6 131 return -1;
6f14ad73 132 }
133
134 /* retry if got no event */
135 if (event==NULL) {
3e60bb92 136 //break;
137 continue;
6f14ad73 138 }
139
140 /* use event - here, just write event id to result file */
141 eventT=event->eventType;
142
77e40af5 143 if (eventT==PHYSICS_EVENT || eventT==CALIBRATION_EVENT ) { //updated: 18/02/2008 based on http://alice-ecs.web.cern.ch/alice-ecs/runtypes_3.16.html
a225d309 144 runNum=(unsigned long)event->eventRunNb; //assuming that only one run is processed at a time
3c8e86a0 145 ldcId=(unsigned long)event->eventLdcId;
77e40af5 146 if(iEvtNcal==firstEvt && pCal->GetWritePads()==kTRUE)
147 {
cb9b108e 148 if(pCal->GetLargePads()==kFALSE) pCal->InitFile((Int_t)ldcId); //The number for iEvtNcal should be the same as for the first value
149 else pCal->InitFile((Int_t)runNum); //The number for iEvtNcal should be the same as for the first value
77e40af5 150 }
151
913dd690 152 iEvtNcal++;
ef7257c6 153 AliRawReader *reader = new AliRawReaderDate((void*)event);
cb9b108e 154 AliHMPIDRawStream stream(reader);stream.SetTurbo(kTRUE); //raw data decoding without error checks SetTurbo(kTRUE)
a225d309 155 while(stream.Next())
156 {
157 for(Int_t iPad=0;iPad<stream.GetNPads();iPad++) {
158 pCal->FillPedestal(stream.GetPadArray()[iPad],stream.GetChargeArray()[iPad]);
3c8e86a0 159 } //pads
913dd690 160 }//while -- loop on det load in one event
161
162 for(Int_t iddl=0;iddl<stream.GetNDDL();iddl++){
163 pCal->FillDDLCnt(iddl,stream.GetnDDLInStream()[iddl],stream.GetnDDLOutStream()[iddl]);
3c8e86a0 164 for(Int_t ierr=0; ierr < stream.GetNErrors(); ierr++) {
165 pCal->FillErrors(iddl,ierr,stream.GetErrors(iddl,ierr));
a225d309 166 }
3c8e86a0 167 }//err
a225d309 168
3c8e86a0 169 pCal->SetRunParams(runNum,stream.GetTimeStamp(),stream.GetLDCNumber()); //Get the last TimeStam read and the LDC ID
a225d309 170 stream.Delete();
6f14ad73 171 }// if CALIBRATION_EVENT
172
173 /* exit when last event received, no need to wait for TERM signal */
174 if (eventT==END_OF_RUN) {
175 printf("EOR event detected\n");
176 break;
177
178 } // events loop
179
1e7f6589 180 free(event);
181 }
6f14ad73 182
3e60bb92 183 }//arg
6f14ad73 184
1e7f6589 185 /* write report */
3c8e86a0 186 printf("HMPID DA processed RUN #%s on LDC#%d, with %d calibration events\n",getenv("DATE_RUN_NUMBER"),ldcId,iEvtNcal);
1e7f6589 187
188 if (!iEvtNcal) {
189 printf("No calibration events have been read. Exiting\n");
6f14ad73 190 return -1;
191 }
192
193 /* report progress */
1e7f6589 194 daqDA_progressReport(90);
cdd8604a 195
4969a7fa 196 for(Int_t nDDL=0; nDDL < AliHMPIDRawStream::kNDDL; nDDL++) {
197 feeIn=Form("thr%d.dat",ddlOffset+nDDL);
21f61e25 198 /* Calculate pedestal for the given ddl, if there is no ddl go t next */
4969a7fa 199 if(pCal->CalcPedestal(nDDL,Form("HmpidPedDdl%02i.txt",nDDL),Form("%s",feeIn.Data()),iEvtNcal)) {
200 status=daqDA_DB_storeFile(feeIn.Data(),feeIn.Data()); //store a single threshold file for a DDL in DAQ DB
201 if (status) { printf("Failed to store file %s in DAQ DB, status: %d\n",feeIn.Data(),status); }
202 status=daqDA_FES_storeFile(Form("HmpidPedDdl%02i.txt",nDDL),Form("HmpidPedDdl%02i.txt",nDDL)); //store a single pedestal file for a DDL
203 if (status) { printf("Failed to export file on FES: %d\n",status); }
204 }//pedestals and thresholds
205 if(pCal->WriteErrors(nDDL,Form("HmpidErrorsDdl%02i.txt",nDDL),iEvtNcal)) {
206 status=daqDA_FES_storeFile(Form("HmpidErrorsDdl%02i.txt",nDDL),Form("HmpidErrorsDdl%02i.txt",nDDL));
207 if (status) { printf("Failed to export file : %d\n",status); }
208 }//errors
cdd8604a 209 /* to create pedestal file as Paolo uncomment the line */
4969a7fa 210 //if(!pCal->CalcPedestalPaolo(nDDL,Form("%sHmpidPedDdl%02i.txt",sDaOut.Data(),nDDL),iEvtNcal)) continue;
3e60bb92 211 }//nDDL
3c8e86a0 212
77e40af5 213 if(pCal->GetWritePads()==kTRUE) pCal->CloseFile();
3c8e86a0 214 delete pCal;
a225d309 215 if (status) return -1;
3e60bb92 216
1e7f6589 217 /* report progress */
218 daqDA_progressReport(100);
a225d309 219
3c8e86a0 220
1e7f6589 221 return status;
6f14ad73 222}
4969a7fa 223