]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/ITSSSDPEDda.cxx
Changes to the Improve method (J. Belikov) Savannah bug 76361
[u/mrichter/AliRoot.git] / ITS / ITSSSDPEDda.cxx
1 /**************************************************************************
2 - Contact: Oleksandr_Borysov oborysov@cern.ch
3 - Link: /afs/cern.ch/user/o/oborysov/public/ssdda/run75491.raw, ssddaconfig.txt, ssdddlmap_v09.txt, ssdbcmap_1258498704.root
4 - Run Type: PEDESTAL
5 - DA Type: LDC
6 - Number of events needed: ~200
7 - Input Files: raw_data_file_on_LDC, in the daqDetDB: ssddaconfig.txt, ssdddlmap_v09.txt, ssdbcmap_1258498704.root
8 - Output Files: ./<EqId_Slot> ./ssddaldc_<LDCID>.root, FXS_name=ITSSSDda_<LDCID>.root 
9                 local files are persistent over runs: data source
10 - Trigger types used:
11  **************************************************************************/
12
13  
14 #include <iostream>
15 #include <fstream>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 #include <ctype.h>
20 #include "TString.h"
21 #include "TFile.h"
22 #include "daqDA.h"
23 #include "AliITSHandleDaSSD.h" 
24
25 #include "TROOT.h"
26 #include "TPluginManager.h"
27
28 using namespace std;
29
30
31 struct ConfigStruct {
32   Int_t    fNModuleProcess;
33   string   fSsdDdlMap;
34   string   fBadChannels;
35   Bool_t   fCheckChipsOff, fUseWelford;
36   ConfigStruct() : fNModuleProcess(108), fSsdDdlMap(""), fBadChannels(""), fCheckChipsOff(kFALSE), fUseWelford(kTRUE) {}
37 };
38
39
40 Int_t SaveEquipmentCalibrationData(const AliITSHandleDaSSD  *ssddaldc, const Char_t  *fprefix = NULL);
41 Bool_t ReadDAConfigurationFile(const Char_t *configfname, AliITSHandleDaSSD *const ssddaldc, ConfigStruct& cfg);
42
43
44 int main( int argc, char** argv )
45 {
46   const Char_t       *configfname = "ssddaconfig.txt";
47   const Char_t       *bcfname = "badchannels.root"; 
48   const Char_t       *ddlmfname = "ssdddlmap.txt";
49   AliITSHandleDaSSD  *ssddaldc;
50   TString             feefname, fcdbsave, lfname;
51   Int_t               status;
52   Char_t             *dafname = NULL;
53   ConfigStruct        cfg; 
54
55   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
56                                         "*",
57                                         "TStreamerInfo",
58                                         "RIO",
59                                         "TStreamerInfo()");
60
61
62   /* check that we got some arguments = list of files */
63   if (argc<2) {
64     fprintf(stderr, "Wrong number of arguments\n");
65     return -1;
66   }
67
68   char *datafilename = argv[1];
69   ssddaldc = new AliITSHandleDaSSD(datafilename);
70   if (ssddaldc->IsZombie()) { 
71    cerr << "Failed to process raw data file " << datafilename << "! Exit DA!\n";
72    return -1;
73   }
74
75   lfname.Form("./%s", configfname);
76   if (!(status = daqDA_DB_getFile(configfname, lfname.Data()))) {
77     if (!ReadDAConfigurationFile(lfname.Data(), ssddaldc, cfg)) cerr << "Error reading configuration file "  << lfname.Data() << " !\n";
78   } else fprintf(stderr, "Failed to import DA configuration file %s from the detector db: %d, %s \n", configfname, status, lfname.Data());
79     
80   if (cfg.fBadChannels.size() > 0) bcfname = cfg.fBadChannels.c_str();
81   lfname.Form("./%s", bcfname);
82   if ((status = daqDA_DB_getFile(bcfname, lfname.Data()))) {
83     fprintf(stderr, "Failed to import the file %s from the detector db: %d, %s! Exit DA!\n", bcfname, status, lfname.Data());
84     delete ssddaldc;
85     return -3;
86   }  
87   if (!ssddaldc->ReadStaticBadChannelsMap(lfname.Data())) {
88     cerr << "Error reading static bad channels map " << lfname.Data() << "! Exit DA!\n";
89     delete ssddaldc;
90     return -4;
91   }       
92   
93   if (cfg.fSsdDdlMap.size() > 0) ddlmfname = cfg.fSsdDdlMap.c_str();
94   lfname.Form("./%s", ddlmfname);
95   if (!(status = daqDA_DB_getFile(ddlmfname, lfname.Data()))) {
96     if (!ssddaldc->ReadDDLModuleMap(lfname.Data())) cerr << "Error reading DDL map from file " << lfname.Data() << " !\n"; 
97   } else {
98     fprintf(stderr, "Failed to import file %s from the detector db: %d, %s \n", ddlmfname, status, lfname.Data());
99     if (!ssddaldc->ReadDDLModuleMap()) cerr << "Failed to load the DDL map from AliITSRawStreamSSD!\n"; 
100   }    
101   
102   if (!ssddaldc->ProcessRawData(cfg.fNModuleProcess, cfg.fUseWelford)) {
103      cerr << "Error !ssddaldc->ProcessRawData()" << endl;
104      delete ssddaldc;
105      return -1;
106   }
107   daqDA_progressReport(90);
108
109   dafname = ".";
110   if (ssddaldc->SaveCalibrationSSDLDC(dafname)) {
111     cout << "SSDDA data are saved in " << dafname << endl;
112     status = daqDA_FES_storeFile(dafname, "CALIBRATION");
113     if (status) fprintf(stderr, "Failed to export file : %d\n", status);
114   } else cerr << "Error saving DA data to the file!\n";
115
116   feefname.Form("%s/ssddaldc_%i.root", ".", ssddaldc->GetLdcId());
117   cout << "Saving feessdda data in " << feefname << endl;
118   TFile *fileRun = new TFile (feefname.Data(),"RECREATE");
119   if (fileRun->IsZombie()) {
120     cerr << "Error open file " << feefname << endl;
121     delete ssddaldc;
122     delete fileRun;
123     return -2;
124   }  
125   ssddaldc->Write();
126   fileRun->Close();
127   delete fileRun;
128
129   fcdbsave.Form("ssddaldc_%i.root", ssddaldc->GetLdcId());
130   status = daqDA_DB_storeFile(feefname.Data(), fcdbsave.Data());
131   if (status) fprintf(stderr, "Failed to export file %s to the detector db: %d, %s \n", feefname.Data(), status, fcdbsave.Data());
132   cout << SaveEquipmentCalibrationData(ssddaldc) << " files were uploaded to DetDB!\n";
133   if(cfg.fCheckChipsOff) ssddaldc->CheckOffChips();
134   delete ssddaldc;
135   daqDA_progressReport(100);
136   return 0;
137 }
138
139
140
141 //__________________________________________________________________________________________
142 Int_t SaveEquipmentCalibrationData(const AliITSHandleDaSSD  *ssddaldc, const Char_t  *fprefix)
143 {
144   TString feefilename;
145   Int_t count = 0, status;
146   for (Int_t ddli = 0; ddli < 16; ddli++) {
147     for(Int_t adi = 1; adi <= 9; adi++) {
148           if (!ssddaldc->AdDataPresent(ddli, adi)) continue;  
149           if (fprefix) feefilename.Form("%s%i_%i", fprefix, ssddaldc->DdlToEquipmentId(ddli), adi);
150           else feefilename.Form("%i_%i", ssddaldc->DdlToEquipmentId(ddli), adi);
151       if (ssddaldc->SaveEqSlotCalibrationData(ddli, adi, feefilename)) {
152         status = daqDA_DB_storeFile(feefilename, feefilename);
153         if (status) fprintf(stderr, "Error %i, failed to export file %s\n", status, feefilename.Data());
154         else count++;
155       }
156     }
157   }
158   return count;
159 }
160
161
162 //__________________________________________________________________________________________
163 Bool_t ReadDAConfigurationFile(const Char_t *configfname, AliITSHandleDaSSD *const ssddaldc, ConfigStruct& cfg) 
164 {
165 // Dowload configuration parameters from configuration file or database
166   const int nkwords = 13;
167   char const *keywords[nkwords] = {"ZsDefault", "OffsetDefault", "ZsFactor", "PedestalThresholdFactor", "CmThresholdFactor",
168                              "NModulesToProcess", "DDLMapFile", "BadChannelsFile", "ZSMinValue", "MergeBCFlag", 
169                              "CheckChipsOff", "UseWelford", "OffLadder"};
170   Int_t tmpint, laddern;
171   Float_t tmpflt;
172   fstream dfile;
173   vector<short> allist(0), cllist(0);
174   if (!configfname) {
175     cerr << "No DA configuration file name is specified, Return!\n";
176     return kFALSE;
177   }
178   if (!ssddaldc) {
179     cerr << "ssddaldc == 0, DA configuration file will not be read! Return!\n";
180     return kFALSE;
181   }
182   dfile.open(configfname, ios::in);
183   if (!dfile.is_open()) {
184     cerr << "Error open DA configuration file " << configfname << " defaul value are used!\n";
185     return kFALSE;
186   }
187   while (!dfile.eof()) {
188     string str, keystr, tmpstr;
189     getline(dfile, str);
190     stringstream strline(str);
191     strline >> keystr;
192     if (keystr.size() == 0) continue;
193     if ((keystr.at(0) == '#') ) continue;
194     int ind = 0;
195     while (keystr.compare(keywords[ind])) if (++ind == nkwords) break;
196     switch (ind) {
197           case 0: 
198               strline >> tmpint;
199               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
200               else {
201                     ssddaldc->SetZsDefaul(tmpint);   
202                     cout << "Default value for ZS thereshold " << keystr << ": " << ssddaldc->GetZsDefault() << endl;
203               } break;   
204           case 1: 
205                   strline >> tmpint;
206               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
207               else {
208                     ssddaldc->SetOffsetDefault(tmpint);   
209                     cout << "Default value for offset correction " << keystr << ": " << ssddaldc->GetOffsetDefault() << endl;
210               } break;   
211           case 2: 
212                   strline >> tmpflt;
213               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
214               else {
215                     ssddaldc->SetZsFactor(tmpflt);   
216                 cout << keystr << ": " << ssddaldc->GetZsFactor() << endl;
217               } break;
218           case 3: 
219                   strline >> tmpflt;
220               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
221               else {
222                     ssddaldc->SetPedestalThresholdFactor(tmpflt);
223                 cout << keystr << ": " << ssddaldc->GetPedestalThresholdFactor() << endl;
224               } break;
225           case 4: 
226                   strline >> tmpflt;
227               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
228               else {
229                     ssddaldc->SetCmThresholdFactor(tmpflt);
230                 cout << keystr << ": " << ssddaldc->GetCmThresholdFactor() << endl;
231               } break;
232           case 5: 
233                   strline >> tmpint;
234               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
235               else {
236                     cfg.fNModuleProcess = tmpint;
237                 cout << keystr << ": " << cfg.fNModuleProcess << endl;
238               } break;
239           case 6: 
240                   strline >> tmpstr;
241               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
242               else {
243                 if (tmpstr.size() == 0) continue;
244                 if (tmpstr.at(0) == '#') continue;
245                     cfg.fSsdDdlMap = tmpstr;
246                 cout << keystr << ": " << cfg.fSsdDdlMap.c_str() << endl;
247               } break;
248           case 7: 
249                   strline >> tmpstr;
250               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
251               else {
252                 if (tmpstr.size() == 0) continue;
253                 if (tmpstr.at(0) == '#') continue;
254                     cfg.fBadChannels = tmpstr;
255                 cout << keystr << ": " << cfg.fBadChannels.c_str() << endl;
256               } break;
257           case 8: 
258                   strline >> tmpint;
259               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
260               else {
261                     ssddaldc->SetZsMinimum(tmpint);
262                 cout << keystr << ": " << ssddaldc->GetZsMinimum() << endl;
263               } break;
264           case 9: 
265                   strline >> tmpint;
266               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
267               else {
268                     ssddaldc->SetMergeBCFlag(static_cast<Byte_t>(tmpint));
269                 cout << keystr << ": " << ssddaldc->GetMergeBCFlag() << endl;
270               } break;
271           case 10: 
272                   strline >> tmpint;
273               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
274               else {
275                     cfg.fCheckChipsOff = static_cast<Bool_t>(tmpint);
276                 cout << keystr << ": " << cfg.fCheckChipsOff << endl;
277               } break;
278           case 11: 
279                   strline >> tmpint;
280               if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
281               else {
282                     cfg.fUseWelford = static_cast<Bool_t>(tmpint);
283                 cout << keystr << ": " << cfg.fUseWelford << endl;
284               } break;
285           case 12: 
286                   char dside;
287               while (!strline.eof()) {
288                 strline >> tmpstr;
289                 if (strline.fail()) cerr << "Failed to read " << keystr << " value from DA configuration file!\n";
290                 if (tmpstr.size() == 0) break;
291                 if ((tmpstr.at(0) == '#') ) break;
292                 if ( sscanf(tmpstr.c_str(), "%c%u", &dside, &laddern) < 2 ) {
293                   cerr << "Error reading side and ladder number form the config file: " << tmpstr << "! Continue!\n";
294                   continue;
295                 }
296                 if ( toupper(dside) == 'A') allist.push_back(laddern);
297                 else if ( toupper(dside) == 'C') cllist.push_back(laddern);
298                 else cerr << "Error! " << dside << " SSD ladder side can be either A or C! Continue!\n";
299               }
300                break;
301           default: 
302                   cerr << keystr << " is not a key word, no assignment were made!\n"; 
303     }
304   }
305   if (allist.size() == 0 && cllist.size() == 0) return kTRUE;
306   short *tmparr = 0, si;
307   vector<short>::iterator it;
308   if (allist.size() > 0) {
309         tmparr = new short [allist.size()];
310         cout << "Following A side " << (allist.size()>1?"ladders are":"ladder is") << " supposed to be off and will be suppressed :";
311     for ( it = allist.begin(), si = 0; it < allist.end(); it++, si++ ) {
312       tmparr[si] = *it;
313       cout << " " << tmparr[si];
314     }
315     cout << ";" << endl;
316     ssddaldc->SetALaddersOff(allist.size(), tmparr);
317   }
318   if (tmparr) delete [] tmparr;
319   if (cllist.size() > 0) {
320         tmparr = new short [cllist.size()];
321         cout << "Following C side " << (cllist.size() > 1 ? "ladders are" : "ladder is") << " supposed to be off and will be suppressed :";
322     for ( it = cllist.begin(), si = 0; it < cllist.end(); it++, si++ ) {
323       tmparr[si] = *it;
324       cout << " " << tmparr[si];
325     }
326     cout << ";" << endl;
327     ssddaldc->SetCLaddersOff(cllist.size(), tmparr);
328   }
329   if (tmparr) delete [] tmparr;
330   return kTRUE;
331 }