1 |
// Geometry.cpp: implementation of the Geometry class.
|
2 |
//
|
3 |
//////////////////////////////////////////////////////////////////////
|
4 |
|
5 |
#include "../interface/Geometry.h"
|
6 |
#include <algorithm>
|
7 |
|
8 |
Geometry::Geometry(void)
|
9 |
{
|
10 |
}
|
11 |
|
12 |
Geometry::~Geometry(void)
|
13 |
{
|
14 |
}
|
15 |
|
16 |
void Geometry::Load (char* path)
|
17 |
{
|
18 |
FILE* pFile = fopen(path,"rb" );
|
19 |
if(pFile==NULL){
|
20 |
printf("The file %s can not be open !\n",path);
|
21 |
return;
|
22 |
}
|
23 |
|
24 |
stChunk* PrimaryChunk = new stChunk;
|
25 |
|
26 |
PrimaryChunk->read =fread(&PrimaryChunk->type,1,2,pFile);
|
27 |
PrimaryChunk->read +=fread(&PrimaryChunk->size,1,4,pFile);
|
28 |
|
29 |
if(PrimaryChunk->type!=C_PRIMARY){
|
30 |
printf("PrimaryChunk has not a good Id (%i!=%i)\n",PrimaryChunk->type,C_PRIMARY);
|
31 |
printf("Exiting...\n");
|
32 |
exit(0);
|
33 |
}
|
34 |
|
35 |
Read(pFile,PrimaryChunk);
|
36 |
fclose(pFile);
|
37 |
}
|
38 |
|
39 |
void Geometry::Read (FILE* pFile, stChunk* ParentChunk)
|
40 |
{
|
41 |
// unsigned short temp;
|
42 |
// fread(&temp,1,2,pFile);
|
43 |
// fseek(pFile,-2,SEEK_CUR);
|
44 |
// printf("Parent = %i --> Current ChunkId = %i to read = %i\n",ParentChunk->type,temp,0);
|
45 |
|
46 |
stChunk* CurrentChunk = new stChunk;
|
47 |
ReadChunk(pFile, CurrentChunk);
|
48 |
|
49 |
switch(CurrentChunk->type)
|
50 |
{
|
51 |
case C_GEOMETRY: // Geometry
|
52 |
while(CurrentChunk->read<CurrentChunk->size){
|
53 |
Read(pFile,CurrentChunk);
|
54 |
}
|
55 |
break;
|
56 |
|
57 |
|
58 |
////////////////////////TRACKER DATA//////////////////////////
|
59 |
case C_TRACKER: // TRACKER
|
60 |
while(CurrentChunk->read<CurrentChunk->size){
|
61 |
Read(pFile,CurrentChunk);
|
62 |
}
|
63 |
break;
|
64 |
|
65 |
case C_TRACKER_MOD:{ // Module
|
66 |
while(CurrentChunk->read<CurrentChunk->size){
|
67 |
TrackerDet* Temp = new TrackerDet;
|
68 |
CurrentChunk->read += fread(Temp,sizeof(TrackerDet),1,pFile) * sizeof(TrackerDet);
|
69 |
Det_Tracker_ALL[Temp->DetId] = Temp;
|
70 |
}
|
71 |
}break;
|
72 |
|
73 |
////////////////////////ECAL DATA//////////////////////////
|
74 |
case C_ECAL: // ECAL
|
75 |
while(CurrentChunk->read<CurrentChunk->size){
|
76 |
Read(pFile,CurrentChunk);
|
77 |
}
|
78 |
break;
|
79 |
|
80 |
|
81 |
case C_ECAL_MOD:{ // Module
|
82 |
while(CurrentChunk->read<CurrentChunk->size){
|
83 |
CaloDet* Temp = new CaloDet;
|
84 |
CurrentChunk->read += fread(Temp,sizeof(CaloDet),1,pFile) * sizeof(CaloDet);
|
85 |
Det_ECAL_ALL[Temp->DetId] = Temp;
|
86 |
}
|
87 |
}break;
|
88 |
|
89 |
////////////////////////HCAL DATA//////////////////////////
|
90 |
case C_HCAL: // HCAL
|
91 |
while(CurrentChunk->read<CurrentChunk->size){
|
92 |
Read(pFile,CurrentChunk);
|
93 |
}
|
94 |
break;
|
95 |
|
96 |
case C_HCAL_MOD:{ // Module
|
97 |
while(CurrentChunk->read<CurrentChunk->size){
|
98 |
CaloDet* Temp = new CaloDet;
|
99 |
CurrentChunk->read += fread(Temp,sizeof(CaloDet),1,pFile) * sizeof(CaloDet);
|
100 |
Det_HCAL_ALL[Temp->DetId] = Temp;
|
101 |
}
|
102 |
}break;
|
103 |
|
104 |
////////////////////////MUON DATA//////////////////////////
|
105 |
case C_MUON: // MUON
|
106 |
while(CurrentChunk->read<CurrentChunk->size){
|
107 |
Read(pFile,CurrentChunk);
|
108 |
}
|
109 |
break;
|
110 |
|
111 |
case C_MUON_MOD:{ // Module
|
112 |
while(CurrentChunk->read<CurrentChunk->size){
|
113 |
TrackerDet* Temp = new TrackerDet;
|
114 |
CurrentChunk->read += fread(Temp,sizeof(TrackerDet),1,pFile) * sizeof(TrackerDet);
|
115 |
Det_Muon_ALL[Temp->DetId] = Temp;
|
116 |
}
|
117 |
}break;
|
118 |
|
119 |
////////////////////////UNKOWN CHUNK ////////////////////////
|
120 |
default:
|
121 |
printf("Unknown ChunkID (%i)\n",CurrentChunk->type);
|
122 |
printf("Chunk will be skipped\n");
|
123 |
printf("The program can not run properly\n");
|
124 |
fseek (pFile,CurrentChunk->size-CurrentChunk->read,SEEK_CUR);
|
125 |
CurrentChunk->read += CurrentChunk->size-CurrentChunk->read;
|
126 |
break;
|
127 |
}
|
128 |
ParentChunk->read += CurrentChunk->read;
|
129 |
}
|
130 |
|
131 |
void Geometry::Save (char* path)
|
132 |
{
|
133 |
stChunkToSave* Primary_chunk = new stChunkToSave;
|
134 |
|
135 |
Primary_chunk->type = C_PRIMARY;
|
136 |
Primary_chunk->size = 6;
|
137 |
|
138 |
Write(Primary_chunk);
|
139 |
/*
|
140 |
printf("%i (%i)\n",Primary_chunk->type,Primary_chunk->size);
|
141 |
for(unsigned int i=0;i<Primary_chunk->daughters.size();i++){
|
142 |
stChunkToSave* tmp = Primary_chunk->daughters[i];
|
143 |
printf(" %i (%i)\n",tmp->type,tmp->size);
|
144 |
for(unsigned int j=0;j<tmp->daughters.size();j++){
|
145 |
stChunkToSave* Tmp = tmp->daughters[j];
|
146 |
printf(" %i (%i)\n",Tmp->type,Tmp->size);
|
147 |
for(unsigned int k=0;k<Tmp->daughters.size();k++){
|
148 |
stChunkToSave* TMp = Tmp->daughters[k];
|
149 |
printf(" %i (%i)\n",TMp->type,TMp->size);
|
150 |
for(unsigned int l=0;l<TMp->daughters.size();l++){
|
151 |
stChunkToSave* TMP = TMp->daughters[l];
|
152 |
printf(" %i (%i)\n",TMP->type,TMP->size);
|
153 |
}
|
154 |
}
|
155 |
}
|
156 |
}
|
157 |
*/
|
158 |
|
159 |
FILE* pFile = fopen(path,"wb" );
|
160 |
WriteChunk(pFile, Primary_chunk);
|
161 |
fclose(pFile);
|
162 |
}
|
163 |
|
164 |
void Geometry::Write(stChunkToSave* ParentChunk)
|
165 |
{
|
166 |
unsigned int i;
|
167 |
std::map<unsigned int,TrackerDet*, std::less<unsigned int> >::iterator it;
|
168 |
std::map<unsigned int,CaloDet* , std::less<unsigned int> >::iterator it2;
|
169 |
stChunkToSave* CurrentChunk;
|
170 |
void* data_buffer;
|
171 |
|
172 |
switch(ParentChunk->type)
|
173 |
{
|
174 |
case C_PRIMARY: // PrimaryChunk
|
175 |
CurrentChunk = new stChunkToSave;
|
176 |
CurrentChunk->type = C_GEOMETRY;
|
177 |
CurrentChunk->size = 6;
|
178 |
ParentChunk->daughters.push_back(CurrentChunk);
|
179 |
break;
|
180 |
|
181 |
case C_GEOMETRY: // Geometry
|
182 |
if(Det_Tracker_ALL.size()>0){
|
183 |
CurrentChunk = new stChunkToSave;
|
184 |
CurrentChunk->type = C_TRACKER;
|
185 |
CurrentChunk->size = 6;
|
186 |
ParentChunk->daughters.push_back(CurrentChunk);
|
187 |
}
|
188 |
|
189 |
if(Det_ECAL_ALL.size()>0){
|
190 |
CurrentChunk = new stChunkToSave;
|
191 |
CurrentChunk->type = C_ECAL;
|
192 |
CurrentChunk->size = 6;
|
193 |
ParentChunk->daughters.push_back(CurrentChunk);
|
194 |
}
|
195 |
|
196 |
if(Det_HCAL_ALL.size()>0){
|
197 |
CurrentChunk = new stChunkToSave;
|
198 |
CurrentChunk->type = C_HCAL;
|
199 |
CurrentChunk->size = 6;
|
200 |
ParentChunk->daughters.push_back(CurrentChunk);
|
201 |
}
|
202 |
|
203 |
if(Det_Muon_ALL.size()>0){
|
204 |
CurrentChunk = new stChunkToSave;
|
205 |
CurrentChunk->type = C_MUON;
|
206 |
CurrentChunk->size = 6;
|
207 |
ParentChunk->daughters.push_back(CurrentChunk);
|
208 |
}
|
209 |
break;
|
210 |
|
211 |
case C_TRACKER: // Tracker
|
212 |
CurrentChunk = new stChunkToSave;
|
213 |
CurrentChunk->type = C_TRACKER_MOD;
|
214 |
CurrentChunk->size = 6 + sizeof(TrackerDet)*Det_Tracker_ALL.size();
|
215 |
data_buffer = new void*[CurrentChunk->size-6];
|
216 |
CurrentChunk->data = data_buffer;
|
217 |
for(it=Det_Tracker_ALL.begin();it!=Det_Tracker_ALL.end();it++){
|
218 |
memcpy( data_buffer, it->second, sizeof(TrackerDet) );
|
219 |
data_buffer = (void*)((unsigned long)data_buffer + sizeof(TrackerDet));
|
220 |
}
|
221 |
ParentChunk->daughters.push_back(CurrentChunk);
|
222 |
break;
|
223 |
|
224 |
case C_ECAL: // ECAL
|
225 |
CurrentChunk = new stChunkToSave;
|
226 |
CurrentChunk->type = C_ECAL_MOD;
|
227 |
CurrentChunk->size = 6 + sizeof(CaloDet)*Det_ECAL_ALL.size();
|
228 |
data_buffer = new void*[CurrentChunk->size-6];
|
229 |
CurrentChunk->data = data_buffer;
|
230 |
for(it2=Det_ECAL_ALL.begin();it2!=Det_ECAL_ALL.end();it2++){
|
231 |
memcpy( data_buffer, it2->second, sizeof(CaloDet) );
|
232 |
data_buffer = (void*)((unsigned long)data_buffer + sizeof(CaloDet));
|
233 |
}
|
234 |
ParentChunk->daughters.push_back(CurrentChunk);
|
235 |
break;
|
236 |
|
237 |
case C_HCAL: // HCAL
|
238 |
CurrentChunk = new stChunkToSave;
|
239 |
CurrentChunk->type = C_HCAL_MOD;
|
240 |
CurrentChunk->size = 6 + sizeof(CaloDet)*Det_HCAL_ALL.size();
|
241 |
data_buffer = new void*[CurrentChunk->size-6];
|
242 |
CurrentChunk->data = data_buffer;
|
243 |
for(it2=Det_HCAL_ALL.begin();it2!=Det_HCAL_ALL.end();it2++){
|
244 |
memcpy( data_buffer, it2->second, sizeof(CaloDet) );
|
245 |
data_buffer = (void*)((unsigned long)data_buffer + sizeof(CaloDet));
|
246 |
}
|
247 |
ParentChunk->daughters.push_back(CurrentChunk);
|
248 |
break;
|
249 |
|
250 |
case C_MUON: // Tracker
|
251 |
CurrentChunk = new stChunkToSave;
|
252 |
CurrentChunk->type = C_MUON_MOD;
|
253 |
CurrentChunk->size = 6 + sizeof(TrackerDet)*Det_Muon_ALL.size();
|
254 |
data_buffer = new void*[CurrentChunk->size-6];
|
255 |
CurrentChunk->data = data_buffer;
|
256 |
for(it=Det_Muon_ALL.begin();it!=Det_Muon_ALL.end();it++){
|
257 |
memcpy( data_buffer, it->second, sizeof(TrackerDet) );
|
258 |
data_buffer = (void*)((unsigned long)data_buffer + sizeof(TrackerDet));
|
259 |
}
|
260 |
ParentChunk->daughters.push_back(CurrentChunk);
|
261 |
break;
|
262 |
default:
|
263 |
break;
|
264 |
}
|
265 |
|
266 |
|
267 |
for(i=0; i<ParentChunk->daughters.size();i++)
|
268 |
{
|
269 |
// if( !(ParentChunk->type == C_PRIMARY && i!=ParentChunk->daughters.size()-1))
|
270 |
Write(ParentChunk->daughters[i]);
|
271 |
ParentChunk->size += ParentChunk->daughters[i]->size;
|
272 |
}
|
273 |
}
|
274 |
|
275 |
void Geometry::Add_TrackerDet(unsigned int DetId, float TrapezoidalParam,
|
276 |
float PosX, float PosY, float PosZ,
|
277 |
float WidthX, float WidthY, float WidthZ,
|
278 |
float LengthX, float LengthY, float LengthZ,
|
279 |
float ThickX, float ThickY, float ThickZ){
|
280 |
|
281 |
int Det = (DetId>>28)&0xF;
|
282 |
//int SubDet = (DetId>>25)&0x7;
|
283 |
|
284 |
TrackerDet* temp = new TrackerDet();
|
285 |
temp->DetId = DetId;
|
286 |
temp->PosX = PosX;
|
287 |
temp->PosY = PosY;
|
288 |
temp->PosZ = PosZ;
|
289 |
temp->WidthX = WidthX;
|
290 |
temp->WidthY = WidthY;
|
291 |
temp->WidthZ = WidthZ;
|
292 |
temp->LengthX = LengthX;
|
293 |
temp->LengthY = LengthY;
|
294 |
temp->LengthZ = LengthZ;
|
295 |
temp->ThickX = ThickX;
|
296 |
temp->ThickY = ThickY;
|
297 |
temp->ThickZ = ThickZ;
|
298 |
temp->TrapezoidalParam = TrapezoidalParam;
|
299 |
|
300 |
if(Det==1){ Det_Tracker_ALL[temp->DetId] = temp; return;}
|
301 |
if(Det==2){ Det_Muon_ALL [temp->DetId] = temp; return;}
|
302 |
|
303 |
printf("Unknown Det %i\n",Det);
|
304 |
delete temp;
|
305 |
return;
|
306 |
}
|
307 |
|
308 |
void Geometry::Add_CaloDet (unsigned int DetId,
|
309 |
float PosX, float PosY, float PosZ,
|
310 |
float wX, float wY, float wZ,
|
311 |
float hX, float hY, float hZ, float F){
|
312 |
|
313 |
CaloDet* temp = new CaloDet();
|
314 |
temp->DetId = DetId;
|
315 |
temp->PosX = PosX;
|
316 |
temp->PosY = PosY;
|
317 |
temp->PosZ = PosZ;
|
318 |
temp->wX = wX; temp->wY = wY; temp->wZ = wZ;
|
319 |
temp->hX = hX; temp->hY = hY; temp->hZ = hZ;
|
320 |
temp->F = F;
|
321 |
|
322 |
|
323 |
int Det = (DetId>>28)&0xF;
|
324 |
//int SubDet = (DetId>>25)&0x7;
|
325 |
//DET1 --> SubDet 1=EB 2=EE 3=PS 4=EcalTriggerTower 5=EcalLaserPnDiode
|
326 |
//DET2 --> SubDet 1=HB 2=HE 3=HO 4=HF 5=HcalTriggerTower 6=HcalOther (HcalOtherEmpty or HcalCalibration)
|
327 |
|
328 |
if(Det==3){ Det_ECAL_ALL[temp->DetId] = temp; return;}
|
329 |
if(Det==4){ Det_HCAL_ALL[temp->DetId] = temp; return;}
|
330 |
|
331 |
printf("Unknown Det %i\n",Det);
|
332 |
delete temp;
|
333 |
return;
|
334 |
}
|
335 |
|
336 |
|
337 |
TrackerDet* Geometry::Find_TrackerDet (unsigned int DetId){
|
338 |
int Det = (DetId>>28)&0xF;
|
339 |
//int SubDet = (DetId>>25)&0x7;
|
340 |
std::map<unsigned int,TrackerDet*, std::less<unsigned int> >::iterator det;
|
341 |
|
342 |
if(Det==1){ // Tracker
|
343 |
det = Det_Tracker_ALL.find(DetId);
|
344 |
if(det!=Det_Tracker_ALL.end())return det->second;
|
345 |
}else if(Det==2){ // MUON
|
346 |
det = Det_Muon_ALL.find(DetId);
|
347 |
if(det!=Det_Muon_ALL.end())return det->second;
|
348 |
}
|
349 |
return NULL;
|
350 |
}
|
351 |
|
352 |
CaloDet* Geometry::Find_CaloDet (unsigned int DetId){
|
353 |
|
354 |
int Det = (DetId>>28)&0xF;
|
355 |
//int SubDet = (DetId>>25)&0x7;
|
356 |
std::map<unsigned int,CaloDet*, std::less<unsigned int> >::iterator det;
|
357 |
|
358 |
if(Det==3){ //ECAL
|
359 |
det = Det_ECAL_ALL.find(DetId);
|
360 |
if(det!=Det_ECAL_ALL.end())return det->second;
|
361 |
}else if(Det==4){ //HCAL
|
362 |
det = Det_HCAL_ALL.find(DetId);
|
363 |
if(det!=Det_HCAL_ALL.end())return det->second;
|
364 |
}
|
365 |
return NULL;
|
366 |
}
|