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