33b3874e18f49a277f0dfef8e916da95ab19c568
[u/mrichter/AliRoot.git] / HLT / MUON / macros / CreateTriggerRecoLookupTables.C
1 /**************************************************************************
2  * This file is property of and copyright by the ALICE HLT Project        * 
3  * All rights reserved.                                                   *
4  *                                                                        *
5  * Primary Authors:                                                       *
6  *   Artur Szostak <artursz@iafrica.com>                                  *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          * 
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /**
18  * This macro is used to generate the lookup tables for the trigger reconstructor
19  * component. All alignment and geometry data is taken from the CDB.
20  */
21
22 #include <iostream>
23 #include <fstream>
24
25 #include "AliCDBManager.h"
26 #include "AliGeomManager.h"
27 #include "AliMUONGeometryTransformer.h"
28 #include "AliMUONGeometryDetElement.h"
29 #include "AliMpCDB.h"
30 #include "AliMpDDLStore.h"
31 #include "AliMpPad.h"
32 #include "AliMpSegmentation.h"
33 #include "AliMpDEIterator.h"
34 #include "AliMpVSegmentation.h"
35 #include "AliMpDEManager.h"
36 #include "AliMpLocalBoard.h"
37 #include "AliMpTriggerCrate.h"
38 #include "AliHLTMUONDataTypes.h"
39
40 using namespace std;
41
42
43 struct TriggerRecoLookupTable
44 {
45         // [regional header index][local board ID][chamber][cathode - X/Y][bit set in bit pattern]
46         AliHLTMUONTriggerRecoLutRow fRow[8][16][4][2][16];
47 };
48
49
50 void CreateTriggerRecoLookupTables(const char* CDBpath = "local://$ALICE_ROOT")
51 {
52         AliCDBManager* cdbManager = AliCDBManager::Instance();
53         cdbManager->SetDefaultStorage(CDBpath);
54         cdbManager->SetRun(0);
55         AliGeomManager::LoadGeometry();
56         
57         AliMUONGeometryTransformer transformer;
58         if (! transformer.LoadGeometryData())
59         {
60                 cerr << "ERROR: Could not load geometry into transformer." << endl;
61                 return;
62         }
63         
64         if (! AliMpCDB::LoadMpSegmentation())
65         {
66                 cerr << "ERROR: Could not load segmentation mapping." << endl;
67                 return;
68         }
69         AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
70         if (segmentation == NULL)
71         {
72                 cerr << "ERROR: AliMpSegmentation::Instance() was NULL." << endl;
73                 return;
74         }
75         if (! AliMpCDB::LoadDDLStore())
76         {
77                 cerr << "ERROR: Could not load DDL mapping." << endl;
78                 return;
79         }
80         AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
81         if (ddlStore == NULL)
82         {
83                 cerr << "ERROR: AliMpDDLStore::Instance() was NULL." << endl;
84                 return;
85         }
86         
87         cout << "Building LUTs..." << endl;
88         
89         TriggerRecoLookupTable* lookupTable21 = new TriggerRecoLookupTable;
90         TriggerRecoLookupTable* lookupTable22 = new TriggerRecoLookupTable;
91         
92         for (Int_t i = 0; i < 8; i++)
93         for (Int_t j = 0; j < 16; j++)
94         for (Int_t k = 0; k < 4; k++)
95         for (Int_t n = 0; n < 2; n++)
96         for (Int_t m = 0; m < 16; m++)
97         {
98                 lookupTable21->fRow[i][j][k][n][m].fX = 0;
99                 lookupTable21->fRow[i][j][k][n][m].fY = 0;
100                 lookupTable21->fRow[i][j][k][n][m].fZ = 0;
101                 lookupTable22->fRow[i][j][k][n][m].fX = 0;
102                 lookupTable22->fRow[i][j][k][n][m].fY = 0;
103                 lookupTable22->fRow[i][j][k][n][m].fZ = 0;
104         }
105         
106         AliMpDEIterator detElemIter;
107         for (Int_t iDDL = 20; iDDL <= 21; iDDL++)
108         {
109                 for (Int_t iReg = 0; iReg < 8; iReg++)
110                 {
111                         AliMpTriggerCrate* crate = ddlStore->GetTriggerCrate(iDDL, iReg);
112                         if (crate == NULL)
113                         {
114                                 cerr << "ERROR: Could not get crate for regional header = " << iReg
115                                         << ", and DDL ID = " << iDDL << endl;
116                                 continue;
117                         }
118                         
119                         for (Int_t iLocBoard = 0; iLocBoard < 16; iLocBoard++)
120                         {
121                                 Int_t boardId = crate->GetLocalBoardId(iLocBoard);
122                                 if (boardId == 0) continue;
123                                 
124                                 AliMpLocalBoard* localBoard = ddlStore->GetLocalBoard(boardId);
125                                 if (localBoard == NULL)
126                                 {
127                                         cerr << "ERROR: Could not get loacl board: " << boardId << endl;
128                                         continue;
129                                 }
130
131                                 // skip copy cards
132                                 if (! localBoard->IsNotified()) continue;
133                         
134                                 for (Int_t iChamber = 0; iChamber < 4; iChamber++)
135                                 {
136                                         Int_t detElemId = ddlStore->GetDEfromLocalBoard(boardId, iChamber);
137                                         
138                                         const AliMUONGeometryDetElement* detElemTransform = transformer.GetDetElement(detElemId);
139                                         if (detElemTransform == NULL)
140                                         {
141                                                 cerr << "ERROR: Got NULL pointer for geometry transformer for detection element ID = "
142                                                         << detElemId << endl;
143                                                 continue;
144                                         }
145                                         
146                                         for (Int_t iCathode = 0; iCathode <= 1; iCathode++)
147                                         {
148                                                 const AliMpVSegmentation* seg = segmentation->GetMpSegmentation(
149                                                                 detElemId, AliMp::GetCathodType(iCathode)
150                                                         );
151                                                 
152                                                 for (Int_t bitxy = 0; bitxy < 16; bitxy++)
153                                                 {
154                                                         Int_t offset = 0;
155                                                         if (iCathode && localBoard->GetSwitch(6)) offset = -8;
156                                                         
157                                                         AliMpPad pad = seg->PadByLocation(AliMpIntPair(boardId, bitxy+offset), kFALSE);
158                                                 
159                                                         if (! pad.IsValid())
160                                                         {
161                                                                 // There is no pad associated with the given local board and bit pattern.
162                                                                 continue;
163                                                         }
164                                                         
165                                                         // Get the global coodinates of the pad.
166                                                         Float_t lx = pad.Position().X();
167                                                         Float_t ly = pad.Position().Y();
168                                                         Float_t gx, gy, gz;
169                                                         detElemTransform->Local2Global(lx, ly, 0, gx, gy, gz);
170                                                         
171                                                         // Fill the LUT
172                                                         if (crate->GetDdlId() == 20)
173                                                         {
174                                                                 lookupTable21->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fX = gx;
175                                                                 lookupTable21->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fY = gy;
176                                                                 lookupTable21->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fZ = gz;
177                                                         }
178                                                         else
179                                                         {
180                                                                 lookupTable22->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fX = gx;
181                                                                 lookupTable22->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fY = gy;
182                                                                 lookupTable22->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fZ = gz;
183                                                         }
184                                                 }
185                                         }
186                                 }
187                         }
188                 }
189         }
190         
191         fstream file;
192         file.open("Lut21.dat", fstream::out | fstream::binary | fstream::trunc);
193         if (file)
194         {
195                 file.write((char*)lookupTable21, sizeof(TriggerRecoLookupTable));
196                 if (! file)
197                 {
198                         cerr << "ERROR: There was a problem writing to file Lut21.dat" << endl;
199                 }
200                 file.close();
201         }
202         else
203         {
204                 cerr << "ERROR: Could not open file Lut21.dat for writing." << endl;
205         }
206         
207         file.open("Lut22.dat", fstream::out | fstream::binary | fstream::trunc);
208         if (file)
209         {
210                 file.write((char*)lookupTable22, sizeof(TriggerRecoLookupTable));
211                 if (! file)
212                 {
213                         cerr << "ERROR: There was a problem writing to file Lut22.dat" << endl;
214                 }
215                 file.close();
216         }
217         else
218         {
219                 cerr << "ERROR: Could not open file Lut22.dat for writing." << endl;
220         }
221         
222         delete lookupTable21;
223         delete lookupTable22;
224 }