]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtrapConfigHandler.cxx
- make AliTRDtrapConfig streamable
[u/mrichter/AliRoot.git] / TRD / AliTRDtrapConfigHandler.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 ////////////////////////////////////////////////////////////////////////////
17 //                                                                        //
18 //  MCM configuraton handler                                              //
19 //                                                                        //
20 //  Author: U. Westerhoff                                                 //
21 //                                                                        //
22 ////////////////////////////////////////////////////////////////////////////
23
24 #include "AliTRDtrapConfigHandler.h"
25
26 #include <iostream>
27 #include <sstream>
28 #include <iomanip>
29
30 #include "AliLog.h"
31
32 #include "AliTRDfeeParam.h"
33 #include "AliTRDtrapConfig.h"
34 #include "AliTRDmcmSim.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDcalibDB.h"
37
38 #include "TMath.h"
39 #include "TGeoMatrix.h"
40 #include "TGraph.h"
41
42 #include "Cal/AliTRDCalOnlineGainTable.h"
43 #include "Cal/AliTRDCalOnlineGainTableROC.h"
44 #include "Cal/AliTRDCalOnlineGainTableMCM.h"
45
46 using namespace std;
47
48 AliTRDtrapConfig* AliTRDtrapConfigHandler::fgActiveTrapConfig = 0x0;
49
50 AliTRDtrapConfigHandler::AliTRDtrapConfigHandler(AliTRDtrapConfig *cfg) :
51      ltuParam()
52      , fRestrictiveMask((0x3ffff << 11) | (0x1f << 6) | 0x3f)
53      , fTrapConfig(cfg)
54      , fGtbl()
55 {
56
57 }
58
59
60 AliTRDtrapConfigHandler::~AliTRDtrapConfigHandler()
61 {
62
63 }
64
65 void AliTRDtrapConfigHandler::Init()
66 {
67   if (!fTrapConfig) {
68     AliError("No TRAPconfig given");
69     return;
70   }
71
72   // setup of register allocation
73   // I/O configuration which we don't care about
74   fTrapConfig->SetTrapRegAlloc(AliTRDtrapConfig::kSEBDOU, AliTRDtrapConfig::kAllocNone);
75   // position look-up table by layer
76   for (Int_t iBin = 0; iBin < 128; iBin++)
77     fTrapConfig->SetTrapRegAlloc((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + iBin), AliTRDtrapConfig::kAllocByLayer);
78   // ... individual
79   fTrapConfig->SetTrapRegAlloc(AliTRDtrapConfig::kC14CPUA, AliTRDtrapConfig::kAllocByMCM);
80   fTrapConfig->SetTrapRegAlloc(AliTRDtrapConfig::kC15CPUA, AliTRDtrapConfig::kAllocByMCM);
81
82   // setup of DMEM allocation
83   for(Int_t iAddr = AliTRDtrapConfig::fgkDmemStartAddress;
84             iAddr < (AliTRDtrapConfig::fgkDmemWords + AliTRDtrapConfig::fgkDmemStartAddress); iAddr++) {
85
86     if(iAddr == AliTRDmcmSim::fgkDmemAddrDeflCorr)
87       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByMCMinSM);
88
89     else if(iAddr == AliTRDmcmSim::fgkDmemAddrNdrift)
90       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByDetector);
91
92     else if((iAddr >= AliTRDmcmSim::fgkDmemAddrDeflCutStart) && (iAddr <= AliTRDmcmSim::fgkDmemAddrDeflCutEnd))
93       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByMCMinSM);
94
95     else if((iAddr >= AliTRDmcmSim::fgkDmemAddrTrackletStart) && (iAddr <= AliTRDmcmSim::fgkDmemAddrTrackletEnd))
96       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByMCM);
97
98     else if((iAddr >= AliTRDmcmSim::fgkDmemAddrLUTStart) && (iAddr <= AliTRDmcmSim::fgkDmemAddrLUTEnd))
99       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocGlobal);
100
101     else if(iAddr == AliTRDmcmSim::fgkDmemAddrLUTcor0)
102       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByMCMinSM);
103
104     else if(iAddr == AliTRDmcmSim::fgkDmemAddrLUTcor1)
105       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocByMCMinSM);
106
107     else if(iAddr == AliTRDmcmSim::fgkDmemAddrLUTnbins)
108       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocGlobal);
109
110     else if(iAddr == AliTRDmcmSim::fgkDmemAddrLUTLength)
111       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocGlobal);
112
113     else
114       fTrapConfig->SetDmemAlloc(iAddr, AliTRDtrapConfig::kAllocGlobal);
115   }
116 }
117
118 void AliTRDtrapConfigHandler::ResetMCMs()
119 {
120    //
121    // Reset all MCM registers and DMEM
122    //
123
124   if (!fTrapConfig) {
125     AliError("No TRAPconfig given");
126     return;
127   }
128
129    fTrapConfig->ResetRegs();
130    fTrapConfig->ResetDmem();
131 }
132
133
134 Int_t AliTRDtrapConfigHandler::LoadConfig()
135 {
136   // load a default configuration which is suitable for simulation
137   // for a detailed description of the registers see the TRAP manual
138   // if you want to resimulate tracklets on real data use the appropriate config instead
139
140   if (!fTrapConfig) {
141     AliError("No TRAPconfig given");
142     return -1;
143   }
144
145   // prepare ltuParam
146   // ndrift (+ 5 binary digits)
147   ltuParam.SetNtimebins(20 << 5);
148   // deflection + tilt correction
149   ltuParam.SetRawOmegaTau(0.16133);
150   // deflection range table
151   ltuParam.SetRawPtMin(0.1);
152   // magnetic field
153   ltuParam.SetRawMagField(0.0);
154   // scaling factors for q0, q1
155   ltuParam.SetRawScaleQ0(0);
156   ltuParam.SetRawScaleQ1(0);
157   // disable length correction and tilting correction
158   ltuParam.SetRawLengthCorrectionEnable(kFALSE);
159   ltuParam.SetRawTiltCorrectionEnable(kFALSE);
160
161   for (Int_t iDet = 0; iDet < 540; iDet++) {
162     // HC header configuration bits
163     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC15CPUA, 0x2102, iDet); // zs, deh
164
165     // no. of timebins
166     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC13CPUA, 24, iDet);
167
168     // pedestal filter
169     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFPNP, 4*10, iDet);
170     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFPTC, 0, iDet);
171     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFPBY, 0, iDet); // bypassed!
172
173     // gain filter
174     for (Int_t adc = 0; adc < 20; adc++) {
175       fTrapConfig->SetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0+adc), 40, iDet);
176       fTrapConfig->SetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0+adc), 15, iDet);
177     }
178     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFGTA, 20, iDet);
179     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFGTB, 2060, iDet);
180     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFGBY, 0, iDet);  // bypassed!
181
182     // tail cancellation
183     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFTAL, 200, iDet);
184     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFTLL, 0, iDet);
185     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFTLS, 200, iDet);
186     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kFTBY, 0, iDet);
187
188     // tracklet calculation
189     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPQS0, 5, iDet);
190     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPQE0, 10, iDet);
191     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPQS1, 11, iDet);
192     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPQE1, 20, iDet);
193     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPFS, 5, iDet);
194     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPFE, 20, iDet);
195     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPVBY, 0, iDet);
196     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPVT, 10, iDet);
197     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPHT, 150, iDet);
198     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPFP, 40, iDet);
199     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPCL, 1, iDet);
200     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kTPCT, 10, iDet);
201
202     // apply ltuParams
203     ConfigureDyCorr(iDet);
204     ConfigureDRange(iDet); // deflection range
205     ConfigureNTimebins(iDet);  // timebins in the drift region
206     ConfigurePIDcorr(iDet);  // scaling parameters for the PID
207
208     // event buffer
209     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kEBSF, 1, iDet);  // 0: store filtered; 1: store unfiltered
210
211     // zs applied to data stored in event buffer (sel. by EBSF)
212     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kEBIS, 15 << 2, iDet); // single indicator threshold (plus two digits)
213     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kEBIT, 30 << 2, iDet); // sum indicator threshold (plus two digits)
214     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kEBIL, 0xf0, iDet);   // lookup table
215     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kEBIN, 0, iDet);      // neighbour sensitivity
216
217     // raw data
218     fTrapConfig->SetTrapReg(AliTRDtrapConfig::kNES, (0x0000 << 16) | 0x1000, iDet);
219   }
220
221   // ****** hit position LUT
222
223   // now calculate it from PRF
224   AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
225
226   Double_t padResponse[3]; // pad response left, central, right
227   Double_t padResponseR[3]; // pad response left, central, right
228   Double_t padResponseL[3]; // pad response left, central, right
229
230   for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
231     TGraph gr(128);
232     for (Int_t iBin = 0; iBin < 256*0.5; iBin++) {
233       cal->PadResponse(1., iBin*1./256.,    iLayer, padResponse);
234       cal->PadResponse(1., iBin*1./256.-1., iLayer, padResponseR);
235       cal->PadResponse(1., iBin*1./256.+1., iLayer, padResponseL);
236       gr.SetPoint(iBin, (0.5 * (padResponseR[1] - padResponseL[1])/padResponse[1] * 256), iBin);
237     }
238     for (Int_t iBin = 0; iBin < 128; iBin++) {
239       Int_t corr = (Int_t) (gr.Eval(iBin)) - iBin;
240       if (corr < 0)
241         corr = 0;
242       else if (corr > 31)
243         corr = 31;
244       for (Int_t iStack = 0; iStack < 540/6; iStack++) {
245         fTrapConfig->SetTrapReg((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + iBin), corr, 6*iStack + iLayer);
246       }
247     }
248   }
249   // ****** hit position LUT configuration end
250
251   return 0;
252 }
253
254
255 Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename)
256 {
257    //
258   // load a TRAP configuration from a file
259    // The file format is the format created by the standalone
260    // command coder scc / show_cfdat but without the running number
261    // scc /show_cfdat adds as the first column
262    // which are two tools to inspect/export configurations from wingDB
263    //
264
265   if (!fTrapConfig) {
266     AliError("No TRAPconfig given");
267     return -1;
268   }
269
270    Int_t ignoredLines=0;
271    Int_t ignoredCmds=0;
272    Int_t readLines=0;
273
274
275    AliDebug(5, Form("Processing file %s", filename.Data()));
276    std::ifstream infile;
277    infile.open(filename.Data(), std::ifstream::in);
278    if (!infile.is_open()) {
279     AliError(Form("Can not open MCM configuration file %s", filename.Data()));
280     return kFALSE;
281    }
282
283    UInt_t cmd;
284    Int_t extali, addr, data;
285
286    // reset restrictive mask
287    fRestrictiveMask = (0x3ffff << 11) | (0x1f << 6) | 0x3f;
288    char linebuffer[512];
289    istringstream line;
290
291    while(infile.getline(linebuffer, 512) && infile.good()) {
292       line.clear();
293       line.str(linebuffer);
294       cmd=999;
295       extali=-1;
296       addr=-1;
297       data=-1;
298       line >> std::skipws >> cmd >> addr >> data >> extali;  // the lines read from config file can contain additional columns.
299       // Therefore the detour via istringstream
300
301       if(cmd!=999 && addr != -1 && data!= -1 && extali!=-1) {
302
303          if(cmd==fgkScsnCmdWrite) {
304             for(Int_t det=0; det<AliTRDgeometry::Ndet(); det++) {
305                UInt_t rocpos = (1 << (AliTRDgeometry::GetSector(det)+11)) | (1 << (AliTRDgeometry::GetStack(det)+6)) | (1 << AliTRDgeometry::GetLayer(det));
306                AliDebug(1, Form("checking restriction: mask=0x%08x, rocpos=0x%08x", fRestrictiveMask, rocpos));
307                if ((fRestrictiveMask & rocpos) == rocpos) {
308                  AliDebug(1, Form("match: %i %i %i %i", cmd, extali, addr, data));
309                   AddValues(det, cmd, extali, addr, data);
310                }
311             }
312          }
313
314          else if(cmd == fgkScsnLTUparam) {
315             ProcessLTUparam(extali, addr, data);
316          }
317
318          else if(cmd == fgkScsnCmdRestr) {
319             fRestrictiveMask = data;
320            AliDebug(1, Form("updated restrictive mask to 0x%08x", fRestrictiveMask));
321          }
322
323          else if((cmd == fgkScsnCmdReset) ||
324                  (cmd == fgkScsnCmdRobReset)) {
325            fTrapConfig->ResetRegs();
326          }
327
328          else if (cmd == fgkScsnCmdSetHC) {
329            Int_t fullVersion = ((data & 0x7F00) >> 1) | (data & 0x7f);
330
331            for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
332              Int_t smls = (AliTRDgeometry::GetSector(iDet) << 6) | (AliTRDgeometry::GetLayer(iDet) << 3) | AliTRDgeometry::GetStack(iDet);
333
334              for (Int_t iRob = 0; iRob < 8; iRob++) {
335                // HC mergers
336                fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC14CPUA, 0xc << 16, iDet, iRob, 17);
337                fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, 17);
338
339                // board mergers
340                fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC14CPUA, 0, iDet, iRob, 16);
341                fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, 16);
342
343                // and now for the others
344                for (Int_t iMcm = 0; iMcm < 16; iMcm++) {
345                  fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC14CPUA, iMcm | (iRob << 4) | (3 << 16), iDet, iRob, iMcm);
346                  fTrapConfig->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, iMcm);
347                }
348              }
349            }
350          }
351
352          else if((cmd == fgkScsnCmdRead) ||
353                  (cmd == fgkScsnCmdPause) ||
354                  (cmd == fgkScsnCmdPtrg) ||
355                  (cmd == fgkScsnCmdHwPtrg) ||
356                  (cmd == fgkScsnCmdRobPower) ||
357                  (cmd == fgkScsnCmdTtcRx) ||
358                  (cmd == fgkScsnCmdMcmTemp) ||
359                  (cmd == fgkScsnCmdOri) ||
360                  (cmd == fgkScsnCmdPM) ) {
361            AliDebug(2, Form("ignored SCSN command: %i %i %i %i", cmd, addr, data, extali));
362          }
363
364          else {
365             AliWarning(Form("unknown SCSN command: %i %i %i %i", cmd, addr, data, extali));
366             ignoredCmds++;
367          }
368
369          readLines++;
370       }
371
372       else if(!infile.eof() && !infile.good()) {
373          infile.clear();
374          infile.ignore(256, '\n');
375          ignoredLines++;
376       }
377
378       if(!infile.eof())
379          infile.clear();
380    }
381
382    infile.close();
383
384    AliDebug(5, Form("Ignored lines: %i, ignored cmds: %i", ignoredLines, ignoredCmds));
385
386
387    if(ignoredLines>readLines)
388       AliError(Form("More than 50 %% of the input file could not be processed. Perhaps you should check the input file %s", filename.Data()));
389
390
391    return kTRUE;
392 }
393
394
395
396 Int_t AliTRDtrapConfigHandler::SetGaintable(AliTRDCalOnlineGainTable const &gtbl)
397 {
398    fGtbl=gtbl;
399    return 0;
400 }
401
402
403 void AliTRDtrapConfigHandler::ProcessLTUparam(Int_t dest, Int_t addr, UInt_t data)
404 {
405    //
406    // Process the LTU parameters and stores them in internal class variables
407    // or transfer the stored values to AliTRDtrapConfig, depending on the dest parameter
408    //
409
410    switch (dest) {
411
412    case 0: // set the parameters in AliTRDtrapConfig
413       for(Int_t det=0; det<AliTRDgeometry::Ndet(); det++) {
414          ConfigureDyCorr(det);
415          ConfigureDRange(det); // deflection range
416          ConfigureNTimebins(det);  // timebins in the drift region
417          ConfigurePIDcorr(det);  // scaling parameters for the PID
418       }
419       break;
420
421    case 1: // set variables
422       switch (addr) {
423
424       case 0: ltuParam.SetPtMin(data); break; // pt_min in GeV/c (*1000)
425       case 1: ltuParam.SetMagField(data); break; // B in T (*1000)
426       case 2: ltuParam.SetOmegaTau(data); break; // omega*tau
427       case 3: ltuParam.SetNtimebins(data); break;
428         // ntimbins: drift time (for 3 cm) in timebins (5 add. bin. digits)
429       case 4: ltuParam.SetScaleQ0(data); break;
430       case 5: ltuParam.SetScaleQ1(data); break;
431       case 6: ltuParam.SetLengthCorrectionEnable(data); break;
432       case 7: ltuParam.SetTiltCorrectionEnable(data); break;
433       }
434       break;
435
436    default:
437       AliError(Form("dest %i not implemented", dest));
438    }
439
440 }
441
442
443 void AliTRDtrapConfigHandler::ConfigureNTimebins(Int_t det)
444 {
445    //
446    // Set timebins in the drift region
447    //
448
449   if (!fTrapConfig) {
450     AliError("No TRAPconfig given");
451     return;
452   }
453
454   AddValues(det, fgkScsnCmdWrite, 127, AliTRDmcmSim::fgkDmemAddrNdrift, ltuParam.GetNtimebins());
455 }
456
457
458
459 void AliTRDtrapConfigHandler::ConfigureDyCorr(Int_t det)
460 {
461    //
462    //  Deflection length correction
463    //  due to Lorentz angle and tilted pad correction
464    //  This correction is in units of padwidth / (256*32)
465    //
466
467   if (!fTrapConfig) {
468     AliError("No TRAPconfig given");
469     return;
470   }
471
472    Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
473
474   for (Int_t r = 0; r < nRobs; r++) {
475     for (Int_t m = 0; m < 16; m++) {
476       Int_t dest =  1<<10 | r<<7 | m;
477       Int_t dyCorrInt = ltuParam.GetDyCorrection(det, r, m);
478       AddValues(det, fgkScsnCmdWrite, dest, AliTRDmcmSim::fgkDmemAddrDeflCorr, dyCorrInt);
479     }
480   }
481 }
482
483
484
485
486
487 void AliTRDtrapConfigHandler::ConfigureDRange(Int_t det)
488 {
489    //
490    // deflection range LUT
491    // range calculated according to B-field (in T) and pt_min (in GeV/c)
492    // if pt_min < 0.1 GeV/c the maximal allowed range for the tracklet
493    // deflection (-64..63) is used
494    //
495
496   if (!fTrapConfig) {
497     AliError("No TRAPconfig given");
498     return;
499   }
500
501   Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
502
503   Int_t dyMinInt;
504   Int_t dyMaxInt;
505
506    for (Int_t r = 0; r < nRobs; r++) {
507       for (Int_t m = 0; m < 16; m++) {
508          for (Int_t c = 0; c < 18; c++) {
509
510            // cout << "maxdefl: " << maxDeflAngle << ", localPhi " << localPhi << endl;
511            // cout << "r " << r << ", m" << m << ", c " << c << ", min angle: " << localPhi-maxDeflAngle << ", max: " << localPhi+maxDeflAngle
512            //   << ", min int: " << dyMinInt << ", max int: " << dyMaxInt << endl;
513            Int_t dest =  1<<10 | r<<7 | m;
514            Int_t lutAddr = AliTRDmcmSim::fgkDmemAddrDeflCutStart + 2*c;
515            ltuParam.GetDyRange(det, r, m, c, dyMinInt, dyMaxInt);
516            AddValues(det, fgkScsnCmdWrite, dest, lutAddr+0, dyMinInt);
517            AddValues(det, fgkScsnCmdWrite, dest, lutAddr+1, dyMaxInt);
518          }
519       }
520    }
521 }
522
523 void AliTRDtrapConfigHandler::PrintGeoTest()
524 {
525    //
526    // Prints some information about the geometry. Only for debugging
527    //
528
529    int sm=0;
530    //   for(int sm=0; sm<6; sm++) {
531    for(int stack=0; stack<5; stack++) {
532       for(int layer=0; layer<6; layer++) {
533
534          Int_t det = sm*30+stack*6+layer;
535          for (Int_t r = 0; r < 6; r++) {
536             for (Int_t m = 0; m < 16; m++) {
537                for (Int_t c = 7; c < 8; c++) {
538                  cout << stack << ";" << layer << ";" << r << ";" << m
539                       << ";" << ltuParam.GetX(det, r, m)
540                       << ";" << ltuParam.GetLocalY(det, r, m, c)
541                       << ";" << ltuParam.GetLocalZ(det, r, m) << endl;
542                }
543             }
544          }
545       }
546    }
547    // }
548 }
549
550
551 void AliTRDtrapConfigHandler::ConfigurePIDcorr(Int_t det)
552 {
553    //
554    // Calculate the MCM individual correction factors for the PID
555    // and transfer them to AliTRDtrapConfig
556    //
557
558   if (!fTrapConfig) {
559     AliError("No TRAPconfig given");
560     return;
561   }
562
563    static const Int_t addrLUTcor0 = AliTRDmcmSim::fgkDmemAddrLUTcor0;
564    static const Int_t addrLUTcor1 = AliTRDmcmSim::fgkDmemAddrLUTcor1;
565
566    UInt_t cor0;
567    UInt_t cor1;
568
569    Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
570
571    for (Int_t r=0; r<nRobs; r++) {
572       for(Int_t m=0; m<16; m++) {
573          Int_t dest =  1<<10 | r<<7 | m;
574          if(fGtbl.GetGainTableROC(det) && fGtbl.GetGainTableROC(det)->GetGainTableMCM(r, m))
575             ltuParam.GetCorrectionFactors(det, r, m, 9, cor0, cor1, fGtbl.GetGainTableROC(det)->GetGainTableMCM(r, m)->GetMCMGain());
576          else
577             ltuParam.GetCorrectionFactors(det, r, m, 9, cor0, cor1);
578          AddValues(det, fgkScsnCmdWrite, dest, addrLUTcor0, cor0);
579          AddValues(det, fgkScsnCmdWrite, dest, addrLUTcor1, cor1);
580     }
581   }
582 }
583
584
585 Bool_t AliTRDtrapConfigHandler::AddValues(UInt_t det, UInt_t cmd, UInt_t extali, Int_t addr, UInt_t data)
586 {
587    // transfer the informations provided by LoadConfig to the internal class variables
588
589   if (!fTrapConfig) {
590     AliError("No TRAPconfig given");
591     return kFALSE;
592   }
593
594   if(cmd != fgkScsnCmdWrite) {
595     AliError(Form("Invalid command received: %i", cmd));
596     return kFALSE;
597   }
598
599   AliTRDtrapConfig::TrapReg_t mcmReg = fTrapConfig->GetRegByAddress(addr);
600   Int_t rocType = AliTRDgeometry::GetStack(det) == 2 ? 0 : 1;
601
602   static const int mcmListSize=40;  // 40 is more or less arbitrary
603   Int_t mcmList[mcmListSize];
604
605   // configuration registers
606   if(mcmReg >= 0 && mcmReg < AliTRDtrapConfig::kLastReg) {
607
608     for(Int_t linkPair=0; linkPair<fgkMaxLinkPairs; linkPair++) {
609       if(AliTRDfeeParam::ExtAliToAli(extali, linkPair, rocType, mcmList, mcmListSize)!=0) {
610         Int_t i=0;
611         while(mcmList[i] != -1 && i<mcmListSize) {
612           if(mcmList[i]==127) {
613             AliDebug(1, Form("broadcast write to %s: 0x%08x",
614                              fTrapConfig->GetRegName((AliTRDtrapConfig::TrapReg_t) mcmReg), data));
615             fTrapConfig->SetTrapReg( (AliTRDtrapConfig::TrapReg_t) mcmReg, data, det);
616           }
617           else {
618             AliDebug(1, Form("individual write to %s (%i, %i): 0x%08x",
619                              fTrapConfig->GetRegName((AliTRDtrapConfig::TrapReg_t) mcmReg), (mcmList[i]>>7), (mcmList[i]&0x7F), data));
620             fTrapConfig->SetTrapReg( (AliTRDtrapConfig::TrapReg_t) mcmReg, data, det, (mcmList[i]>>7)&0x7, (mcmList[i]&0x7F));
621           }
622           i++;
623         }
624       }
625     }
626     return kTRUE;
627   }
628   // DMEM
629   else if ( (addr >= AliTRDtrapConfig::fgkDmemStartAddress) &&
630             (addr < (AliTRDtrapConfig::fgkDmemStartAddress + AliTRDtrapConfig::fgkDmemWords))) {
631     for(Int_t linkPair=0; linkPair<fgkMaxLinkPairs; linkPair++) {
632       if(AliTRDfeeParam::ExtAliToAli(extali, linkPair, rocType, mcmList, mcmListSize)!=0) {
633         Int_t i=0;
634         while(mcmList[i] != -1 && i < mcmListSize) {
635           if(mcmList[i] == 127)
636              fTrapConfig->SetDmem(addr, data, det, 0, 127);
637           else
638              fTrapConfig->SetDmem(addr, data, det, mcmList[i] >> 7, mcmList[i] & 0x7f);
639           i++;
640         }
641       }
642     }
643     return kTRUE;
644   }
645   else if ( (addr >= AliTRDtrapConfig::fgkImemStartAddress) &&
646             (addr < (AliTRDtrapConfig::fgkImemStartAddress + AliTRDtrapConfig::fgkImemWords))) {
647     // IMEM is ignored for now
648     ;
649   }
650   else if ( (addr >= AliTRDtrapConfig::fgkDbankStartAddress) &&
651             (addr < (AliTRDtrapConfig::fgkDbankStartAddress + AliTRDtrapConfig::fgkImemWords))) {
652     // DBANK is ignored for now
653     ;
654   }
655   else {
656     AliError(Form("Writing to unhandled address 0x%04x", addr));
657     return kFALSE;
658   }
659 }