Report problems to ATLAS LXR Team (with time and IP address indicated)

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: linux ]
Version: head ] [ nightly ] [ GaudiDev ]
  Links to LXR source navigation pages for stable releases [ 12.*.* ]   [ 13.*.* ]   [ 14.*.* ] 

001 #include <stdio.h>
002 #include <stdlib.h>             // for exit()
003 //#include <string>             // for strcmp()
004 #include <math.h>
005 #include <iomanip>
006 
007 #include "RPCgeometry/RPCGeometry.h"
008 #include "MuonCablingTools/RPCdecoder.h"
009 
010 
011 static MultiChamber *T1muonGeo[MaxJtype][MaxJff*(2*MaxJzz+1)];
012 
013 
014 RPCGeometry* RPCGeometry::s_instance = 0;
015 bool RPCGeometry::DumpMap = false;
016 bool RPCGeometry::DumpLog = false;
017 bool RPCGeometry::DumpCon = false;
018 bool RPCGeometry::DebuGeo = false;
019 
020 double RPCGeometry::s_ScaleFactor = 1.;
021 float  RPCGeometry::s_Zsign = +1.;
022 double RPCGeometry::s_pi = 3.14159265358979323846;
023 
024 bool RPCGeometry::s_testbeam = false;
025 int  RPCGeometry::s_JobMinForEncodeProj = 9999;
026 
027 std::string RPCGeometry::FileName = "LVL1conf.dump";
028 
029 #ifndef LVL1_STANDALONE
030 Amdcsimrec* RPCGeometry::s_amdc = 0;
031 #endif
032 
033 bool RPCGeometry::s_status      = true;
034 
035 
036 RPCGeometry::RPCGeometry(void)
037 {
038     for (int sect=0;sect<64;++sect) 
039     {
040         for(int stat=First;stat<=Extended;++stat)
041         {
042             for(int i=0;i<2;++i)
043             {
044                 LogicSector[sect][stat][i] = 0;
045             }
046         }
047     }
048     
049     if(s_testbeam) s_Zsign=-1.;
050         
051 //#ifndef LVL1_STANDALONE
052 
053     projMap = 0;
054     
055     buildgeo();
056     //check_projMap();
057 
058 //#else
059 
060     //readgeo();    
061     
062 //#endif
063 
064     setupmap();
065     setuplogic();
066     
067     if(DumpCon)
068     {
069         std::ofstream dump;
070         dump.open(RPCGeometry::FileName.c_str());
071         write_geodata(dump);
072     }
073 }
074 
075 RPCGeometry::~RPCGeometry(void)
076 {
077     // Delete the Chamber Map
078     for (int Jtype=1;Jtype<=MaxJtype;++Jtype)
079     {
080         for (int Jff=1;Jff<=Mff;++Jff)
081         {
082             for (int Jzz=-Mzz;Jzz<=Mzz;++Jzz)
083             {
084                 MultiChamber *cham = 
085                                  T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz];
086                                                 
087                 if (cham)
088                 {
089                     delete cham;
090                 }
091             }
092         }
093     }
094     //Delete Logic Map
095 }
096 
097 //#ifdef LVL1_STANDALONE
098 const RPCGeometry*
099 RPCGeometry::instance(void)
100 {
101     if (! s_instance) {
102         s_instance = new RPCGeometry();
103     }
104 
105     return s_instance;
106 }
107 //#endif
108 
109 //=============================================================================
110 //**************************** Printout facilities ****************************
111 //=============================================================================
112 
113 void 
114 RPCGeometry::myprin(char c,int i) const 
115 {
116   int j;
117   
118   for(j=0;j<i;j++) printf("%c",c);
119 }
120 
121 void
122 RPCGeometry::cham_info(MultiChamber *cham,int stat)
123 {
124     printf("    Chamber %s,  Phyisic sector = %d,  Z index = %d",
125                       cham->name.c_str(),cham->physics_sector+1,
126                       cham->amdb_z_index);
127     if (stat > 3) printf("\n\n");
128     else printf(",  Station = %d\n\n",stat + 1);
129 }
130 
131 void
132 RPCGeometry::pack_info(RPC_pack *pack)
133 {
134     printf("\n      Jobj = %d,  Station = %d, Z index = %d\n",
135                     pack->Jobj, pack->station, pack->lvl1_z_index);
136     printf("      Pack center: X=%8.2f, Y=%8.2f, Z=%8.2f\n",
137                     pack->pack_coo[0],pack->pack_coo[1],pack->pack_coo[2]);
138     printf("      #z strips = %d (pitch=%5.3f cm), #phi strips = %d",
139                     pack->nstrip_z, pack->stripsize_z, pack->nstrip_f);
140     printf(" (pitch=%5.2f cm)\n",pack->stripsize_f);
141     printf("      Z coordinate of first strip: Z = %8.2f\n",pack->z_proj[2]);
142 }
143 
144 void
145 RPCGeometry::list_of_packs(MultiChamber *cham)
146 {
147     cham_info(cham,3);
148     printf("    has %d RPC packs:\n\n",cham->nRPC_packs);
149     RPC_pack *pack = cham->RPC_packs;
150     while(pack)
151     {
152         pack_info(pack);
153         pack = pack->next;
154     }
155 }
156 
157 void
158 RPCGeometry::first_packs_info(RPC_pack *pack1,RPC_pack *pack2,int stat)
159 {
160     if (pack1)
161     {
162         printf("\n    The chamber\n");
163         pack_info(pack1);
164      printf("\n    has been chosen as first RPC(proj 0) of station %d\n",stat);
165     }
166     if (pack2)
167     {
168         printf("\n    The chamber\n");
169         pack_info(pack2);
170      printf("\n    has been chosen as first RPC(proj 1) of station %d\n",stat);
171     }
172 }
173 
174 void
175 RPCGeometry::check_error(MultiChamber *cham,std::string message)
176 {
177     printf("RPCGeometry, check_links: ");
178     printf("%s\n",message.c_str());
179     cham_info(cham,10);
180     exit(1);
181 }
182 
183 void
184 RPCGeometry::axis(__osstream **disp,std::string X, std::string Y)
185 {    
186     *disp[6] << Y;
187     *disp[7] << "^";
188     for(int i=8;i<22;++i) *disp[i] << "|";
189     *disp[22] << "+-------------> " << X;
190 }
191 
192 int
193 RPCGeometry::find_front(__osstream **disp)
194 {
195     unsigned int head = 0;
196     for (int i=0;i<22;++i)
197     {
198         if((disp[i])->COUNT > head) head = (disp[i])->COUNT;
199     }
200     return head;
201 }
202 
203 void
204 RPCGeometry::align_disp(__osstream **disp,int start,int stop,int front)
205 {
206     for(int i=start;i<=stop;++i)
207     {
208         if(i<23)
209         {
210             int head_line = (disp[i])->COUNT;
211             for(int j=0;j<front - head_line;++j) 
212             {
213                 if(i==10) *disp[i] << "-";
214                 else *disp[i] << " ";
215             }
216         }
217     }
218 }
219 
220 
221 //=============================================================================
222 //********************** Single chamber dump routines *************************
223 //=============================================================================
224 
225 void
226 RPCGeometry::dump_chamber(MultiChamber *cham)
227 {
228     __osstream *disp[23];
229     
230     for(int stat=First;stat<=Extended;++stat)      // for all the trigger stations
231     {
232         RPC_pack* pack0 = cham->firstRPC_z[0][stat];
233         RPC_pack* pack1 = cham->firstRPC_z[1][stat];
234         
235         if (pack0 || pack1)
236         {
237 #if (__GNUC__) && (__GNUC__ > 2)   // put your gcc 3.2 specific code here
238             for(int i=0;i<23;++i) disp[i] = new __osstream();
239 #else                              // put your gcc 2.95 specific code here
240             for(int i=0;i<23;++i) disp[i] = new __osstream(display[i],80);
241 #endif
242 
243 //          ============== Start the dumping of the X-Y view <-----------------
244             axis(disp,"Z","Phi");
245             
246             float Phi = (pack0)? pack0->rotation_angle : pack1->rotation_angle;
247             *disp[10] << " Phi=";
248             
249             align_disp(disp,0,22,(disp[10])->COUNT);
250             
251             *disp[10] << std::setw(5) << std::setprecision(3) 
252                       << std::setiosflags(std::ios::fixed)
253                       << Phi << "-";
254         
255 //          =============== Loop for dumping the RPC packs in the X-Y view <---
256             while (pack0||pack1)
257             {
258                 dump_pack_check(disp,pack1,1);
259                 dump_pack_check(disp,pack0,0);
260                  
261                 if(pack0) pack0 = pack0->next_in_z[0];
262                 if(pack1) pack1 = pack1->next_in_z[1];
263                 
264                 if(pack0 || pack1) 
265                 {
266                     align_disp(disp,0,21,find_front(disp));
267                     for(int i=0;i<10;++i) *disp[i] << "  ";
268                     *disp[10] << "--------";
269                     for(int i=11;i<22;++i) *disp[i] << "  ";
270                 }
271             }
272             
273             align_disp(disp,0,22,find_front(disp));
274         
275 //          =============== Start the dumping of the R-Z view <----------------
276             for(int i=0;i<23;++i) *disp[i] << "  ";
277             axis(disp,"Z","R");
278             align_disp(disp,0,21,(disp[10])->COUNT);
279             for(int i=0;i<21;++i) *disp[i] << "  ";
280             
281             pack0 = cham->firstRPC_z[0][stat];
282             pack1 = cham->firstRPC_z[1][stat];
283 
284             int head = (disp[10])->COUNT;
285             int offs = 0;
286             
287 //          =============== Loop for dumping the RPC packs in the R-Z view <---
288             while (pack0||pack1)
289             {
290                 int proj = (pack0)? 0 : 1;
291                 RPC_pack* pack = (pack0)? pack0 : pack1;
292                 
293                 RPC_pack * prev = pack->prev_in_z[proj];
294                 
295                 float radius =sqrt(powf(pack->pack_coo[0],2) +
296                                    powf(pack->pack_coo[1],2));
297                              
298                 float prev_r =(!prev)?  0 : sqrt(powf(prev->pack_coo[0],2) +
299                                                  powf(prev->pack_coo[1],2));
300                                                         
301                 if (prev)
302                 {
303                     if (pack->pack_coo[2] - pack->half_dim[1] < 
304                         prev->pack_coo[2] + prev->half_dim[1]) 
305                     {
306                         align_disp(disp,offs-2,offs+2,head-1);
307                     } else
308                     {
309                         align_disp(disp,offs-2,offs+2,head+1);
310                     }
311                 }
312                 
313                 if(!offs) offs = (radius > cham->Rmid)?  4 : 16;
314                 
315                 if (radius > prev_r) --offs;
316                 else if (radius < prev_r) ++offs;
317                 
318                 if (radius == cham->Rmid) offs = 10;
319                 
320                 *disp[offs] << "=========";
321                 align_disp(disp,offs-1,offs+1,head);
322                 *disp[offs-1] << " " << std::setw(6) << std::setprecision(1) 
323                               << std::setiosflags(std::ios::fixed) 
324                               << pack->gas_layer_radius[1];
325                 *disp[offs+1] << " " << std::setw(6) << std::setprecision(1) 
326                               << std::setiosflags(std::ios::fixed) 
327                               << pack->gas_layer_radius[0];
328                 head = (disp[offs])->COUNT;
329                 
330                 if(pack0) pack0 = pack0->next_in_z[0];
331                 if(pack1) pack1 = pack1->next_in_z[1];
332             }
333             
334             *disp[10] << " ---- Rmid=" << std::setw(6) << std::setprecision(1)
335                       << std::setiosflags(std::ios::fixed) << cham->Rmid << " ";
336             align_disp(disp,10,10,head);
337 
338 
339 //          ============= Close and output the stream <------------------------
340             myprin('=',79);
341             printf("\n");
342             cham_info(cham,stat);
343             
344             for(int i=0;i<23;++i)
345             {
346 #if (__GNUC__) && (__GNUC__ > 2)   // put your gcc 3.2 specific code here
347                 std::cout << (disp[i])->str() << std::endl;
348 #else                              // put your gcc 2.95 specific code here
349                 *disp[i] << std::ends;
350                 printf("%s\n",display[i]);
351 #endif
352                 delete disp[i];
353             }
354             
355         }
356     }
357 }
358 
359 void
360 RPCGeometry::dump_pack_check(__osstream **disp, RPC_pack *pack,int proj)
361 {
362     if (!pack) return;
363     
364     int width = (pack->half_dim[1] > 50.)? 10 : 4;
365     float y_center = pack->pack_coo[0]*sin(-pack->rotation_angle) +
366                          pack->pack_coo[1]*cos(-pack->rotation_angle);
367     if (proj == 1)
368     {
369         if (y_center - pack->half_dim[0] + 5. < 0. && !(fabsf(y_center) < 24.)
370                                                    && !s_testbeam)
371         {
372             dump_pack_roof  (disp[11],6,width);
373             dump_pack_bound (disp[10],0,width,pack->nstrip_z,0);
374             dump_pack_bound (disp[9],6,width,0,0);
375             dump_pack_bound (disp[8],6,width,0,0);
376             dump_pack_bound (disp[7],6,width,0,0);
377         }
378         
379         if (fabsf(y_center) < 24. || s_testbeam)
380         {
381             dump_pack_roof  (disp[10],0,width);
382             dump_pack_bound (disp[9],6,width,pack->nstrip_z,0);
383             dump_pack_bound (disp[8],6,width,0,0);
384             dump_pack_bound (disp[7],6,width,0,0);
385         }
386         
387         if(y_center - pack->half_dim[0] - 5. > 0.)
388         {
389             dump_pack_roof  (disp[8],6,width);
390             dump_pack_bound (disp[7],6,width,pack->nstrip_z,0);
391             dump_pack_bound (disp[6],6,width,0,0);
392             
393             *disp[5] << std::setw(5) << std::setprecision(0);
394             *disp[5] << std::setiosflags(std::ios::fixed) << pack->half_dim[0];
395             dump_pack_bound (disp[5],1,width,pack->nstrip_f,1);
396         } else
397         {
398             *disp[6] << std::setw(5) << std::setprecision(0);
399             *disp[6] << std::setiosflags(std::ios::fixed) << pack->half_dim[0];
400             dump_pack_bound (disp[6],1,width,pack->nstrip_f,1);
401             dump_pack_bound (disp[5],6,width,0,0);
402         }
403         
404         dump_pack_bound (disp[4],6,width,0,0);
405         dump_pack_roof  (disp[3],6,width);
406                         
407         *disp[2] << "      |";
408         for(int i=0;i<width/4;++i) *disp[2]<<" ";
409         *disp[2] << std::setw(4) << std::setprecision(0);
410         *disp[2] << std::setiosflags(std::ios::fixed) << pack->half_dim[1]*2;
411         
412         *disp[1] << "      |";
413       
414         *disp[0] << "   " << std::setw(7) << std::setprecision(0)
415                  << std::setiosflags(std::ios::fixed)
416                  << pack->pack_coo[2]-pack->half_dim[1];   
417     }
418     
419     
420     if (proj == 0)
421     { 
422         if (y_center + pack->half_dim[0] - 5. > 0. && !(fabsf(y_center) < 24.)
423                                                    && !s_testbeam )
424         {
425             dump_pack_roof  (disp[9],6,width);
426             dump_pack_bound (disp[10],0,width,pack->nstrip_z,0);
427             dump_pack_bound (disp[11],6,width,0,0);
428             dump_pack_bound (disp[12],6,width,0,0);
429             dump_pack_bound (disp[13],6,width,0,0);
430         }
431         
432         if (fabsf(y_center) < 24. || s_testbeam)
433         {
434             dump_pack_bound (disp[11],6,width,pack->nstrip_z,0);
435             dump_pack_bound (disp[12],6,width,0,0);
436             dump_pack_bound (disp[13],6,width,0,0);
437         }
438         
439         if(y_center + pack->half_dim[0] + 5. < 0.)
440         {
441             dump_pack_roof  (disp[12],6,width);
442             dump_pack_bound (disp[13],6,width,pack->nstrip_z,0);
443             dump_pack_bound (disp[14],6,width,0,0);
444             
445             *disp[15] << std::setw(5) << std::setprecision(0);
446             *disp[15] << std::setiosflags(std::ios::fixed) << pack->half_dim[0];
447             dump_pack_bound (disp[15],1,width,pack->nstrip_f,1);
448         } else
449         {
450             *disp[14] << std::setw(5) << std::setprecision(0);
451             *disp[14] << std::setiosflags(std::ios::fixed) << pack->half_dim[0];
452             dump_pack_bound (disp[14],1,width,pack->nstrip_f,1);
453             dump_pack_bound (disp[15],6,width,0,0);
454         }
455         
456         dump_pack_bound (disp[16],6,width,0,0);
457         dump_pack_roof  (disp[17],6,width);
458         
459         *disp[18] << "      |";
460         for(int i=0;i<width/4;++i) *disp[18]<<" ";
461         *disp[18] << std::setw(4) << std::setprecision(0);
462         *disp[18] << std::setiosflags(std::ios::fixed) << pack->half_dim[1]*2;
463         
464         *disp[19] << "      |";
465         
466         *disp[20] << "   " << std::setw(7) << std::setprecision(0)
467                   << std::setiosflags(std::ios::fixed)
468                   << pack->pack_coo[2]-pack->half_dim[1];
469     }
470 }
471 
472 void
473 RPCGeometry::dump_pack_roof (__osstream *disp,int offset,int width)
474 {
475     for(int i=0;i<offset;++i) *disp << " ";
476     *disp << "+"; 
477     for(int i=0;i<width;++i) *disp << "-"; 
478     *disp << "+";
479 }
480 
481 void
482 RPCGeometry::dump_pack_bound (__osstream *disp,
483                               int offset, 
484                               int width, 
485                               int strips,
486                               int xy)
487 {
488     for(int i=0;i<offset;++i) *disp << " ";
489     *disp << "|";
490     if (strips)
491     {
492         if(xy)
493         {
494             *disp << std::setw(2) << strips;
495             for(int i=0;i<width-2;++i) *disp << " "; 
496             *disp << "|";
497         } else
498         {
499             for(int i=0;i<width/2-1;++i) *disp << " ";
500             *disp << std::setw(2) << strips;
501             for(int i=0;i<width/2-1;++i) *disp << " ";
502             *disp << "|";
503         }
504     } else
505     {
506         for(int i=0;i<width;++i) *disp << " "; 
507         *disp << "|";
508     }
509 }
510 
511 
512 void
513 RPCGeometry::dump_logic_sectors(void)
514 {
515     __osstream *disp[23];
516     for(int stat=First;stat<=Extended;++stat)
517     {
518         for(int logic_sector = 62;logic_sector >= 0; logic_sector -= 2)
519         {
520             int upper_part = logic_sector;
521             int lower_part = (logic_sector%32)? logic_sector-1:logic_sector+31;
522         
523             RPC_pack *next1 = LogicSector[upper_part][stat][0];
524             RPC_pack *next0 = LogicSector[lower_part][stat][0];
525             RPC_pack *pack1 = 0;
526             RPC_pack *pack0 = 0;
527 #if (__GNUC__) && (__GNUC__ > 2)   // put your gcc 3.2 specific code here
528             for(int i=0;i<23;++i) disp[i] = new __osstream();
529 #else                              // put your gcc 2.95 specific code here
530             for(int i=0;i<23;++i) disp[i] = new __osstream(display[i],80);
531 #endif     
532             
533             int offs  = (lower_part < 32)? 21 : 20;
534             int head = -1;
535                     
536             do
537             {
538 
539                 pack1 = next1;
540                 pack0 = next0;
541             
542                 int width = 4;
543         
544 //          =============== Loop for dumping the RPC packs in the X-Y view <---
545            
546                 if (pack1)
547                 {
548                     float y_center = pack1->pack_coo[0]*
549                                 sin(-pack1->rotation_angle) +
550                                 pack1->pack_coo[1]*cos(-pack1->rotation_angle);
551                                         
552                     if (y_center - pack1->half_dim[0] + 5. < 0. && 
553                                      !(fabsf(y_center) < 24.) && !s_testbeam)
554                     {
555                         dump_pack_roof  (disp[11],0,width);
556                         dump_pack_bound (disp[10],0,width,pack1->nstrip_z,0);
557                         dump_pack_bound (disp[9],0,width,0,0);
558                         dump_pack_bound (disp[8],0,width,0,0);
559                         dump_pack_bound (disp[7],0,width,0,0);
560                     }
561         
562                     if (fabsf(y_center) < 24. || s_testbeam)
563                     {
564                         dump_pack_roof  (disp[10],0,width);
565                         dump_pack_bound (disp[9],0,width,pack1->nstrip_z,0);
566                         dump_pack_bound (disp[8],0,width,0,0);
567                         dump_pack_bound (disp[7],0,width,0,0);
568                     }
569         
570                     if(y_center - pack1->half_dim[0] - 5. > 0.)
571                     {
572                         dump_pack_roof  (disp[8],0,width);
573                         dump_pack_bound (disp[7],0,width,pack1->nstrip_z,0);
574                         dump_pack_bound (disp[6],0,width,0,0);
575                         dump_pack_bound (disp[5],0,width,pack1->nstrip_f,1);
576                     } else
577                     {
578                         dump_pack_bound (disp[6],0,width,pack1->nstrip_f,1);
579                         dump_pack_bound (disp[5],0,width,0,0);
580                     }
581         
582 //                  dump_pack_bound (disp[4],0,width,0,0);
583                     dump_pack_id(disp[4],0,width,pack1->station,
584                                                  pack1->lvl1_z_index);
585                     dump_pack_roof  (disp[3],0,width);
586                     
587                     *disp[2] << " " << pack1->cham->name << pack1->cham->type;
588                 }
589            
590                 if (pack0)
591                 {                    
592                     float y_center = pack0->pack_coo[0]*
593                                 sin(-pack0->rotation_angle) +
594                                 pack0->pack_coo[1]*cos(-pack0->rotation_angle);
595                                         
596                     if (y_center + pack0->half_dim[0] - 5. > 0. && 
597                                      !(fabsf(y_center) < 24.) && !s_testbeam)
598                     {
599                         dump_pack_roof  (disp[9],0,width);
600                         dump_pack_bound (disp[10],0,width,pack0->nstrip_z,0);
601                         dump_pack_bound (disp[11],0,width,0,0);
602                         dump_pack_bound (disp[12],0,width,0,0);
603                         dump_pack_bound (disp[13],0,width,0,0);
604                     }
605         
606                     if (fabsf(y_center) < 24. || s_testbeam)
607                     {
608                         dump_pack_bound (disp[11],0,width,pack0->nstrip_z,0);
609                         dump_pack_bound (disp[12],0,width,0,0);
610                         dump_pack_bound (disp[13],0,width,0,0);
611                     }
612         
613                     if(y_center + pack0->half_dim[0] + 5. < 0.)
614                     {
615                         dump_pack_roof  (disp[12],0,width);
616                         dump_pack_bound (disp[13],0,width,pack0->nstrip_z,0);
617                         dump_pack_bound (disp[14],0,width,0,0);
618                         dump_pack_bound (disp[15],0,width,pack0->nstrip_f,1);
619                     } else
620                     {
621                         dump_pack_bound (disp[14],0,width,pack0->nstrip_f,1);
622                         dump_pack_bound (disp[15],0,width,0,0);
623                     }
624                     
625 //                  dump_pack_bound (disp[16],0,width,0,0);
626                     dump_pack_id(disp[16],0,width,pack0->station,
627                                                   pack0->lvl1_z_index);
628                     dump_pack_roof  (disp[17],0,width);
629                     
630                     *disp[18] << " " << pack0->cham->name << pack0->cham->type;
631                     
632                     align_disp(disp,0,18,find_front(disp));  
633                 
634 //                  ==================== Dump the RPC radial view<-------------
635 
636                     if(!s_testbeam)
637                     {
638                     RPC_pack * prev = pack0->prev_in_z[0];
639                       
640                     float radius = give_radius(pack0);
641                     float prev_r = (!prev)?  0 : give_radius(prev);
642                                                         
643                     if (prev && LogicSector[lower_part][stat][0] != pack0)
644                     {   
645                         int next_offs = give_radial_offset(radius,prev_r,offs);
646                         
647                         if(pack0->cham != prev->cham)
648                         {
649                             *disp[offs] << "]";
650                             align_disp(disp,19,22,(disp[17])->COUNT - 6);
651                             if(same_cham(pack0->next_in_z[0],pack0))
652                                 *disp[next_offs] << " [====";
653                             else                               
654                                 *disp[next_offs] << "[====";
655                             
656                         } else
657                         {
658                             align_disp(disp,19,22,head);
659                             if (pack0->pack_coo[2] - pack0->half_dim[1] < 
660                                 prev->pack_coo[2] + prev->half_dim[1]) 
661                             {
662                                 *disp[next_offs] << "====";
663                             
664                             } else
665                             {
666                                 *disp[next_offs] << "  ====";
667                             }
668                         }
669                         
670                         offs = next_offs;
671                     
672                     } else
673                     {
674                         if(same_cham(pack0->next_in_z[0],pack0))
675                             *disp[offs] << " [====";
676                         else                           
677                             *disp[offs] << "[====";
678                     }
679                     
680                     head = (disp[offs])->COUNT;
681                     
682                     }
683                 
684                 }
685                    
686                 if (LogicSector[lower_part][stat][1] == pack0 && pack0)
687                 {
688                     *disp[offs] << "]";
689                 }
690                 
691                    
692                 if (pack1 && pack1->next_in_z[1]) next1 = pack1->next_in_z[1];
693                 if (pack0 && pack0->next_in_z[0]) next0 = pack0->next_in_z[0];
694                      
695             } while (pack1 && pack1 != LogicSector[upper_part][stat][1] &&
696                      pack0 && pack0 != LogicSector[lower_part][stat][1]);
697 
698 //          ============= Close and outup the stream <-------------------------
699                     
700               myprin('=',21);
701               std::string sign = (lower_part < 32)? "negative" : "positive";
702               printf(" %s half barrel, Station = %1d ",sign.c_str(),stat+1);
703               myprin('=',22);
704               printf("\n");
705               
706               
707               myprin(' ',30);
708               printf("-------------------> Z         Logic Sector = %2d\n",
709                       upper_part%32);
710 
711 
712               for(int i=0;i<23;++i)
713               {
714 #if (__GNUC__) && (__GNUC__ > 2)   // put your gcc 3.2 specific code here
715                   std::cout << (disp[i])->str() << std::endl;
716 #else                              // put your gcc 2.95 specific code here
717                   *disp[i] << std::ends;
718                   printf("%s\n",display[i]);
719 #endif
720                   delete disp[i];
721               }       
722 
723               
724               myprin(' ',61);
725               printf("Logic Sector = %2d\n",lower_part%32);
726 
727         }
728     }   
729 }
730 
731 
732 void
733 RPCGeometry::dump_pack_id (__osstream *disp,
734                               int offset, 
735                               int width, 
736                               int station,
737                               int z_index)
738 {
739     for(int i=0;i<offset;++i) *disp << " ";
740     *disp << "|";
741     for(int i=0;i<(width-4)/2;++i) *disp << " ";
742     *disp << std::setw(1) << station + 1 << "*" << std::setw(2) 
743           << std::setfill('0')<<z_index;
744     for(int i=0;i<(width-4)/2;++i) *disp << " ";
745     *disp << "|";
746 }
747 
748 
749 int
750 RPCGeometry::give_radial_offset(float current_rad, float previous_rad, int offs)
751 {
752     float delta = fabs(current_rad - previous_rad);
753     if (delta <= 1.) return offs;
754     if (current_rad > previous_rad) return(--offs);
755     else  return(++offs);
756 }
757 //=============================================================================
758 //*********************** Structure lists utility *****************************
759 //=============================================================================
760 
761 void 
762 RPCGeometry::rreset()
763 {
764     Removed = 0;
765 }
766 
767 
768 template<class entry> entry*
769 RPCGeometry::remove(entry* first, entry* item)
770 {
771     if(!item) return first;
772     
773     entry* tmp = first;
774     
775     if(tmp == item)
776     {
777         entry* newfirst = first->next;
778         item->next = 0;
779         ++Removed;
780         return newfirst;
781     }
782     
783     entry* prv = tmp;
784     tmp = prv->next;
785     
786     while(tmp)
787     {
788         if(tmp == item)
789         {
790             prv->next = tmp->next;
791             item->next = 0;
792             ++Removed;
793             return first;
794         }
795         prv = tmp;
796         tmp = prv->next;
797     }
798     
799     printf("RPCGeometry, remove: error in removing pack!\n");
800     exit(1);
801 }
802 
803 
804 template<class entry> entry*
805 RPCGeometry::give_last(entry* first)
806 {
807     if(!first) return 0;
808     
809     entry* last = first;
810     
811     while(last->next)
812     {
813         last = last->next;
814     }
815     
816     return last;
817 }
818 
819 
820 void
821 RPCGeometry::set_first_packs(MultiChamber* cham)
822 {
823     for (int stat=First;stat<=Extended;++stat)
824     {
825         float Zmin = 99999.;
826         RPC_pack* pack = cham->RPC_packs;
827         while(pack)
828         {
829             if(s_Zsign*pack->pack_coo[2]<=Zmin && pack->station==stat)
830             {
831                 Zmin = s_Zsign*pack->pack_coo[2];
832                 if(pack->f_proj[0]) cham->firstRPC_z[0][stat] = pack;
833                 if(pack->f_proj[1]) cham->firstRPC_z[1][stat] = pack;   
834             }
835             pack = pack->next;
836         }
837         if(cham->firstRPC_z[0][stat] == cham->firstRPC_z[1][stat])
838         {
839             cham->RPC_packs =remove(cham->RPC_packs,cham->firstRPC_z[0][stat]);
840         } else
841         {
842             cham->RPC_packs =remove(cham->RPC_packs,cham->firstRPC_z[0][stat]);
843             cham->RPC_packs =remove(cham->RPC_packs,cham->firstRPC_z[1][stat]);
844         }
845         
846         if (DebuGeo) first_packs_info(cham->firstRPC_z[0][stat],
847                                       cham->firstRPC_z[1][stat],stat);
848     }      
849 }
850 
851 
852 RPC_pack*
853 RPCGeometry::find_next_pack(MultiChamber* cham, RPC_pack* pack,int sect)
854 {
855     RPC_pack* it = cham->RPC_packs;
856     RPC_pack* found = 0; 
857     
858 //    if(!pack->f_proj[sect]) return found;
859     
860     float dist = 99999;
861     while (it)
862     {
863         float tmp_dist=(s_Zsign*(it->pack_coo[2]) - s_Zsign*(pack->pack_coo[2]));
864         if(tmp_dist < dist                                         && 
865            s_Zsign*(it->pack_coo[2]) > s_Zsign*(pack->pack_coo[2]) &&
866            it->station == pack->station                            && 
867            it->f_proj[sect])
868         {
869             dist = tmp_dist;
870             found = it;
871         }
872         it = it->next;
873     }
874     return found;
875 }
876 
877 
878 void
879 RPCGeometry::connect(RPC_pack* prev, RPC_pack* next)
880 {
881     if(!prev||!next) return;
882     
883     if(prev->f_proj[0] && next->f_proj[0])
884     {
885         prev->next_in_z[0] = next;
886         next->prev_in_z[0] = prev;
887     }
888     
889     if(prev->f_proj[1] && next->f_proj[1])
890     {
891         prev->next_in_z[1] = next;
892         next->prev_in_z[1] = prev;
893     }
894     
895 //    if(next->f_proj[0]) prev->next_in_z[0] = next;
896 //    if(next->f_proj[1]) prev->next_in_z[1] = next;
897     
898 //    if(prev->f_proj[0]) next->prev_in_z[0] = prev;
899 //    if(prev->f_proj[1]) next->prev_in_z[1] = prev;
900 }
901 
902 
903 MultiChamber*
904 RPCGeometry::find_first_chamber(MultiChamber *cham, int stat, int proj)
905 {
906     float Zmin = 99999.;
907     MultiChamber *first = 0;
908     
909     while(cham)
910     {
911 //        int i = 0;
912 //      printf("Step=%d\n",++i);
913 //      cham_info (cham,stat);
914         float z = s_Zsign*((cham->firstRPC_z[proj][stat])->pack_coo[2]);
915         if(z <= Zmin)
916         {
917             Zmin = z;
918             first = cham;
919         }
920         cham = cham->next;
921     }
922     return first;
923     
924 }
925 
926 
927 RPC_pack*
928 RPCGeometry::give_last_pack(MultiChamber *cham, int stat, int proj)
929 {
930     RPC_pack *last = cham->firstRPC_z[proj][stat];
931     while(last->next_in_z[proj]) last = last->next_in_z[proj];
932     return last;
933 }
934 
935 
936 float
937 RPCGeometry::give_radius(RPC_pack *pack)
938 {
939     float radius = pack->pack_coo[0]*cos(-pack->rotation_angle) -
940                    pack->pack_coo[1]*sin(-pack->rotation_angle);
941     return radius;
942 }
943 
944 
945 int
946 RPCGeometry::same_cham(RPC_pack *one, RPC_pack *two)
947 {
948     if(!one || !two) return 0;
949     if(one->cham == two->cham) return 1;
950     else return 0;
951 }
952 //=============================================================================
953 //********************** Ordering of geometry structures **********************
954 //=============================================================================
955 
956 void
957 RPCGeometry::setupmap()
958 {    
959 //  == Setting the pack z_linker and sector+station code into a multichamber<--
960     for (int Jtype=1;Jtype<=MaxJtype;++Jtype)
961     {
962         for (int Jff=1;Jff<=Mff;++Jff)
963         {
964             for (int Jzz=-Mzz;Jzz<=Mzz;++Jzz)
965             {
966                 MultiChamber *cham = 
967                                  T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz];
968                                                 
969                 if (cham)
970                 {
971                     if (DebuGeo) list_of_packs(cham);
972                     
973                     rreset();
974                     set_first_packs(cham);
975                     for (int stat=First;stat<=Extended;++stat)
976                     {
977                        if(cham->firstRPC_z[0][stat]==cham->firstRPC_z[1][stat])
978                        {
979                         build_InterChamS_links(cham,cham->firstRPC_z[0][stat]);
980                        }else
981                        {
982                         build_InterChamS_links(cham,cham->firstRPC_z[0][stat]);
983                         build_InterChamS_links(cham,cham->firstRPC_z[1][stat]);
984                        }
985                     }
986                     
987                     if (Removed != cham->nRPC_packs)
988                     {
989                         std::cout << "Jtype=" << Jtype << ",  Jff=" << Jff
990                                   << ",  Jzz=" << Jzz << std::endl;
991                         printf("RPCGeometry error: number of RPC packs = %d, ",
992                         cham->nRPC_packs);
993                         printf("removed packs = %d\n",Removed);
994                         exit(1);
995                     }
996                     
997                     check_map(cham);      // Check the single chamber structure
998                     
999                     if(DumpMap) dump_chamber(cham);    // Dunp chamber on stdio
1000                 }
1001             }
1002         }
1003     }
1004 }
1005 
1006 
1007 void
1008 RPCGeometry::build_InterChamS_links(MultiChamber* cham, RPC_pack* pack)
1009 {
1010     if(!pack) return;
1011 
1012     RPC_pack* pack1 = find_next_pack(cham,pack,0);
1013     RPC_pack* pack2 = find_next_pack(cham,pack,1);
1014     
1015     if(!pack1&&!pack2) return;
1016 
1017     if(pack1 == pack2)
1018     {
1019         connect(pack,pack1);
1020         if(pack->f_proj[1]) cham->RPC_packs = remove(cham->RPC_packs,pack1);
1021 
1022         build_InterChamS_links(cham,pack1);
1023     } else
1024     {
1025         connect(pack,pack1);
1026         connect(pack,pack2);
1027         cham->RPC_packs = remove(cham->RPC_packs,pack1);
1028         cham->RPC_packs = remove(cham->RPC_packs,pack2);
1029 
1030         build_InterChamS_links(cham,pack1);
1031         build_InterChamS_links(cham,pack2);
1032     }
1033 }
1034 
1035 
1036 void
1037 RPCGeometry::check_map(MultiChamber *cham)
1038 {
1039     int count = 0;
1040     
1041     for (int stat=First;stat<=Extended;++stat)
1042     {   
1043         int step[2] = {0,0};
1044         
1045         for(int i=0;i<2;++i)
1046         {
1047             RPC_pack* pack = cham->firstRPC_z[i][stat];
1048             
1049             if(pack)
1050             {
1051                 if(pack->prev_in_z[0] || pack->prev_in_z[1])
1052                    check_error(cham,"link for previous z pack are not empty!");
1053             
1054                 while (RPC_pack* next = pack->next_in_z[i])
1055                 {
1056                     if(pack->station!=next->station) 
1057                         check_error(cham,"station inconsistence!");
1058                     
1059                     if(!pack->f_proj[i]) 
1060                      check_error(cham,"previous(->next) logic inconsistence!");
1061                     if(!next->f_proj[i])
1062                      check_error(cham,"next(->previous) logic inconsistence!");
1063                     
1064                     if(next->prev_in_z[i] != pack)
1065                         check_error(cham,"next->previous link inconsistence!");
1066                  
1067                     pack = next;
1068                     ++step[i];
1069                 }
1070             }
1071         }
1072                 
1073         if(step[0]!=step[1]) check_error(cham,"z index mismatch!");
1074         
1075         RPC_pack* pack0 = cham->firstRPC_z[0][stat];
1076         RPC_pack* pack1 = cham->firstRPC_z[1][stat];
1077         
1078         if (pack0&&pack1)
1079         {
1080             if(pack0==pack1) ++count;
1081             else count += 2;
1082         
1083             while(step[0]--)
1084             {
1085                 pack0 = pack0->next_in_z[0];
1086                 pack1 = pack1->next_in_z[1];
1087                 if(pack0==pack1) ++count;
1088                 else count += 2; 
1089             }
1090         } else
1091         {
1092             if(pack0)
1093             {
1094                 count += step[0] +1;
1095             } else if (pack1)
1096             {
1097                 count += step[1] +1;
1098             }
1099         } 
1100         
1101     }
1102     
1103     if(cham->nRPC_packs != count) check_error(cham,"RPC packs lost!");
1104 }
1105 
1106 
1107 void
1108 RPCGeometry::setuplogic()
1109 {    
1110     for(int sect=0;sect<16;++sect)
1111     {
1112         int Jff = sect/2 + 1;
1113         
1114         for(int proj=0;proj<2;++proj)
1115         {
1116             for(int stat=First;stat<=Extended;++stat)
1117             {
1118                 MultiChamber* cham_sector[3];
1119                 
1120                 cham_sector[0] = 0;
1121                 cham_sector[1] = 0;
1122                 cham_sector[2] = 0;
1123                 
1124                 MultiChamber* last = 0;
1125             
1126                 for (int Jtype=1;Jtype<=MaxJtype;++Jtype)
1127                 {
1128                     for (int Jzz=-Mzz;Jzz<=Mzz;++Jzz)
1129                     {
1130                         int z_index;
1131                         if(Jzz<0)        
1132                         {
1133                             z_index = 0;
1134                             last = give_last(cham_sector[z_index]);
1135                         } else if (Jzz==0)
1136                         {
1137                             z_index = 1;
1138                             last = give_last(cham_sector[z_index]);
1139                         } else   //if (Jzz>0)
1140                         {
1141                             z_index = 2;
1142                             last = give_last(cham_sector[z_index]);
1143                         }
1144                                     
1145                         MultiChamber *cham =
1146                                 T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz];
1147                     
1148                         if(cham                         && 
1149                            cham->firstRPC_z[proj][stat] && 
1150                            sect == cham->physics_sector)
1151                         {
1152                             if(cham_sector[z_index])
1153                             {
1154                                 last->next = cham; 
1155                             } else
1156                             {
1157                                 cham_sector[z_index] = cham;
1158                             }
1159                             last = cham;
1160                         }
1161                     }               
1162                 }
1163                 
1164                 build_TransChamS_links(cham_sector,sect,stat,proj);
1165                 build_RPC_coding(sect,stat,proj);
1166             }
1167         }
1168     }
1169     
1170     if(!s_testbeam) check_logic();
1171     //else shift_logic();
1172     if(DumpLog) dump_logic_sectors();
1173 }
1174 
1175 
1176 void
1177 RPCGeometry::build_TransChamS_links(MultiChamber **chambers,
1178                                     int sect,int stat,int proj)
1179 {
1180     if(!chambers[0] && !s_testbeam )
1181     {
1182       if (stat == Extended) return;
1183       printf("RPCGeometry, build_TransChamS_links: no chambers for Jzz < 0\n");
1184       exit(1);
1185     }
1186     
1187     if(chambers[1] && (chambers[1])->next)
1188     {
1189        printf("sect = %d\n",sect);
1190        printf("RPCGeometry, build_TransChamS_links: more than 1 chamber");
1191        printf(" whit Jzz = 0\n");
1192        MultiChamber *cham = chambers[1];
1193        do {cham_info(cham,10);} while((cham = cham->next)); 
1194        exit(1);
1195     }
1196     
1197     if(!chambers[2] && !s_testbeam )
1198     {
1199       if (stat == Extended) return;
1200       printf("RPCGeometry, build_TransChamS_links: no chambers for Jzz > 0\n");
1201       exit(1);
1202     }
1203     
1204         
1205     int neg_sector = sect*2 + proj - 1;//logic sector for negative half barrel
1206     if (neg_sector < 0) neg_sector =31;//Put the 1-st "half" Large at sector 31
1207     int z_index = 0;
1208             
1209     MultiChamber *found = find_first_chamber(chambers[z_index],stat,proj);
1210     MultiChamber *last  = found;
1211     RPC_pack     *prev  = 0;
1212     RPC_pack     *next  = 0;
1213     
1214     LogicSector[neg_sector][stat][0] = (found)? found->firstRPC_z[proj][stat]:0;    
1215     chambers[z_index] = remove(chambers[z_index],found);
1216     
1217     while((found = find_first_chamber(chambers[z_index],stat,proj)))
1218     {
1219         prev = give_last_pack(last,stat,proj);
1220         next = found->firstRPC_z[proj][stat];
1221         prev->next_in_z[proj] = next;
1222         next->prev_in_z[proj] = prev;
1223         chambers[z_index] = remove(chambers[z_index],found);
1224         last = found;
1225     }
1226     
1227     LogicSector[neg_sector][stat][1] = (LogicSector[neg_sector][stat][0])? 
1228                                        give_last_pack(last,stat,proj) : 0;
1229     
1230     if(chambers[1])
1231     {
1232         RPC_pack *first = (chambers[1])->firstRPC_z[proj][stat];
1233         RPC_pack *last  = give_last_pack(chambers[1],stat,proj);
1234         if(first != last)
1235         {
1236             printf("RPCGeometry, build_TransChamS_links: Jzz = 0 packs ");
1237             printf("inconsistence\n");
1238             exit(1);
1239         }
1240         if(LogicSector[neg_sector][stat][1]) (LogicSector[neg_sector][stat][1])->next_in_z[proj] = first;
1241         first->prev_in_z[proj] = LogicSector[neg_sector][stat][1];
1242         LogicSector[neg_sector][stat][1] = last;
1243         (chambers[1])->next = chambers[2];
1244     }
1245     
1246     z_index = (chambers[1])? 1 : 2;
1247     int pos_sector = neg_sector + 32; // logic sector for positive half barrel
1248     
1249     found = find_first_chamber(chambers[z_index],stat,proj);
1250     last  = found;
1251     
1252     LogicSector[pos_sector][stat][0] = (found)? found->firstRPC_z[proj][stat]:0;
1253     chambers[z_index] = remove(chambers[z_index],found);
1254     
1255     while((found = find_first_chamber(chambers[z_index],stat,proj)))
1256     {
1257         prev = give_last_pack(last,stat,proj);
1258         next = found->firstRPC_z[proj][stat];
1259         prev->next_in_z[proj] = next;
1260         next->prev_in_z[proj] = prev;
1261         chambers[z_index] = remove(chambers[z_index],found);
1262         last = found;
1263     }
1264     
1265     LogicSector[pos_sector][stat][1] = (LogicSector[pos_sector][stat][0])?
1266                                         give_last_pack(last,stat,proj) : 0;
1267     
1268     if((next = LogicSector[pos_sector][stat][0]) !=
1269        (prev = LogicSector[neg_sector][stat][1]))
1270     {
1271        if(prev) prev->next_in_z[proj] = next;
1272        if(next) next->prev_in_z[proj] = prev;
1273     }
1274 }
1275 
1276 
1277 void
1278 RPCGeometry::build_RPC_coding(int sect,int stat,int proj)
1279 {
1280     int sect_neg = sect*2 + proj - 1; // Logic sector for negative half barrel
1281     if (sect_neg < 0) sect_neg = 31;  // Put the 1-st "half" Large at sector 31
1282     int sect_pos = sect_neg + 32;     // Logic sector for positive half barrel
1283         
1284     RPC_pack *negative_half = LogicSector[sect_neg][stat][1];
1285     RPC_pack *positive_half = LogicSector[sect_pos][stat][0];
1286 
1287     bool bothExist = (negative_half && positive_half)? true : false;
1288             
1289     int start     = (negative_half == positive_half)? 0 : 1;
1290     int neg_index = start;
1291     
1292     while(negative_half)
1293     {
1294         int code = neg_index++;
1295         
1296         if(negative_half->lvl1_z_index && negative_half->lvl1_z_index != code)
1297         {
1298             printf("RPCGeometry,  build_RPC_coding: wrong RPC code for ");
1299             printf("sector logic = %2d, stat = %1d\n",sect_neg,stat);
1300             printf("    old code = %d,  new code = %d\n",
1301                                              negative_half->lvl1_z_index,code);
1302             exit(1);
1303         }
1304         
1305         negative_half->lvl1_z_index = code;
1306         negative_half = negative_half->prev_in_z[proj];
1307     }
1308     
1309     int pos_index = start;
1310     
1311     while(positive_half)
1312     {
1313         int code = pos_index++;
1314         
1315         if(positive_half->lvl1_z_index && positive_half->lvl1_z_index != code)
1316         {
1317             printf("RPCGeometry,  build_RPC_coding: wrong RPC code for ");
1318             printf("sector logic = %2d, stat = %1d\n",sect_pos,stat);
1319             printf("    old code = %d,  new code = %d\n",
1320                                              positive_half->lvl1_z_index,code);
1321             exit(1);
1322         }
1323         
1324         positive_half->lvl1_z_index = code;
1325         positive_half = positive_half->next_in_z[proj];
1326     }
1327     
1328     if((pos_index != neg_index) && bothExist)
1329     {
1330         printf("RPCGeometry,  build_RPC_coding: different index for the two ");
1331         printf("half barrel\n");
1332         printf("    logic sector = %2d,   station = %1d\n",sect_neg,stat);
1333         printf("    negative index = %2d,  positive index = %2d\n",neg_index,
1334                                                                    pos_index);
1335         //exit(1);
1336     }
1337 }
1338 
1339 
1340 void
1341 RPCGeometry::check_logic(void)
1342 {
1343     for(int logic_sector=0;logic_sector<64;++logic_sector)
1344     {
1345         for(int RPC_station=First;RPC_station<=Third;++RPC_station)
1346         {
1347             for(int i=0;i<2;++i)
1348             {
1349                 if(!LogicSector[logic_sector][RPC_station][i])
1350                 {
1351                     std::string pos = (i)? "end position" : "start position";
1352                     printf("RPCGeometry, check_logic: no link for %s of ",
1353                                                                  pos.c_str());
1354                     printf("logic sector %2d, station = %1d\n",logic_sector,
1355                                                                RPC_station+1);
1356                     exit(1);
1357                 }
1358             }
1359         }
1360     }
1361 }
1362 
1363 void
1364 RPCGeometry::shift_logic(void)
1365 {
1366     int start_logic = 0;
1367     
1368     for(int logic_sector=0;logic_sector<64;++logic_sector)
1369     {
1370         if(LogicSector[logic_sector][0][0])
1371         {
1372             start_logic = logic_sector;
1373             break;
1374         }
1375 
1376     }
1377     
1378     int sector = 0;
1379     
1380     for(int logic_sector=start_logic;logic_sector<64;++logic_sector)
1381     {
1382         for(int RPC_station=First;RPC_station<=Extended;++RPC_station)
1383         {
1384             for(int i=0;i<2;++i)
1385             {
1386                 LogicSector[sector][RPC_station][i] = 
1387                 LogicSector[logic_sector][RPC_station][i];
1388                 LogicSector[logic_sector][RPC_station][i] = 0;
1389             }
1390         }
1391         ++sector;
1392     }  
1393 }
1394 
1395 //=============================================================================
1396 //***************** Dumping the geometry and the logic maps *******************
1397 //=============================================================================
1398 
1399 bool
1400 RPCGeometry::RPC_packs_not_equal(RPC_pack* pack1, RPC_pack* pack2) const
1401 {
1402     if(!pack1 || !pack2) return true;
1403     if((pack1->lvl1_z_index == pack2->lvl1_z_index) &&
1404        (pack1->station      == pack2->station     ) &&
1405        (pack1->nstrip_z     == pack2->nstrip_z    ) &&    
1406        (pack1->stripsize_z  == pack2->stripsize_z ) &&
1407        (pack1->nstrip_f     == pack2->nstrip_f    ) &&    
1408        (pack1->stripsize_f  == pack2->stripsize_f ) )  return false;
1409     return true;
1410 }
1411 
1412 
1413 bool
1414 RPCGeometry::Logic_sectors_are_equal(int sector1, int sector2) const
1415 {
1416     int proj1 = (sector1+1)%2;
1417     int proj2 = (sector2+1)%2;
1418 
1419     for (int RPC_station=First;RPC_station<=Extended;++RPC_station)
1420     {
1421        RPC_pack* pack1 = (sector1>=32)? LogicSector[sector1][RPC_station][0] :
1422                                         LogicSector[sector1][RPC_station][1];
1423        RPC_pack* pack2 = (sector2>=32)? LogicSector[sector2][RPC_station][0] :
1424                                         LogicSector[sector2][RPC_station][1];
1425 
1426        while(pack1 && pack2)
1427        {
1428           if(RPC_packs_not_equal(pack1,pack2)) return false;
1429           pack1=(sector1>=32)? pack1->next_in_z[proj1]:pack1->prev_in_z[proj1];
1430           pack2=(sector2>=32)? pack2->next_in_z[proj2]:pack2->prev_in_z[proj2];
1431        }
1432                                    
1433     }
1434     
1435     return true;
1436 }
1437 
1438 
1439 void
1440 RPCGeometry::write_geodata(std::ofstream& file) const
1441 {
1442     int type = 0;
1443     int sector_map[64];
1444     int sector_typ[64];
1445     for (int i=0;i<64;++i) {sector_map[i] = 0; sector_typ[i] = 0;}
1446     
1447     for (int sec=0;sec<64;++sec)
1448     {
1449         if (!sector_map[sec])
1450         {
1451             sector_map[sec] = ++type;
1452             sector_typ[type-1] = sec;
1453             for(int i=sec;i<64;++i)
1454             {
1455                 if(Logic_sectors_are_equal(sec,i)) sector_map[i] = type;
1456             }
1457         }
1458     }
1459     
1460     file << std::endl << "# Start Sector type enumeration" << std::endl;
1461     file << std::endl << "SECTOR TYPES (  0 - 15 ): ";
1462     for (int i=0;i<16;++i) file << " " << std::setw(2) << sector_map[i];
1463     file << std::endl << "SECTOR TYPES ( 16 - 31 ): ";
1464     for (int i=16;i<32;++i) file << " " << std::setw(2) << sector_map[i];
1465     file << std::endl << std::endl << "SECTOR TYPES ( 32 - 47 ): ";
1466     for (int i=32;i<48;++i) file << " " << std::setw(2) << sector_map[i];
1467     file << std::endl << "SECTOR TYPES ( 48 - 63 ): ";
1468     for (int i=48;i<64;++i) file << " " << std::setw(2) << sector_map[i];
1469     file << std::endl << std::endl;
1470     
1471     int dumped_type = 0;
1472     while(dumped_type != type)
1473     {
1474         int sec = sector_typ[dumped_type];
1475         ++dumped_type;
1476         file << std::endl << std::endl << "########################################";
1477         file << "#######################################" << std::endl;
1478         file << "#################################### TYPE " << dumped_type;
1479         file << " ###################################" << std::endl;
1480         file << "########################################";
1481         file << "#######################################" << std::endl;
1482         for (int RPC_station=First;RPC_station<=Extended;++RPC_station)
1483         {
1484             if(!LogicSector[sec][RPC_station][0]) break;
1485             
1486             file << std::endl << "RPC GEOM  " << std::setw(2) << dumped_type; 
1487             file << " : station " << std::setw(1) << RPC_station + 1;
1488             int ncham = 0;
1489             if (sec > 32) 
1490             {
1491                 ncham = ((LogicSector[sec][RPC_station][0])->lvl1_z_index)? 
1492                          (LogicSector[sec][RPC_station][1])->lvl1_z_index : 
1493                          (LogicSector[sec][RPC_station][1])->lvl1_z_index + 1; 
1494             } else
1495             {
1496                 ncham = ((LogicSector[sec][RPC_station][1])->lvl1_z_index)? 
1497                          (LogicSector[sec][RPC_station][0])->lvl1_z_index : 
1498                          (LogicSector[sec][RPC_station][0])->lvl1_z_index + 1;
1499             }
1500             file << " made of " << std::setw(2) << ncham << " chamber. ";
1501             file << "Strips in connectors: eta ";
1502             int eta_conn = (RPC_station==Second)? 8 : 4;
1503             file << std::setw(1) << eta_conn << " phi 8" << std::endl;
1504             
1505             file << "{" << std::endl;
1506             RPC_pack* pack = (sec>=32)? LogicSector[sec][RPC_station][0] :
1507                                         LogicSector[sec][RPC_station][1];
1508             int proj = (sec+1)%2;
1509             
1510             while(pack)
1511             {
1512                 file << " cham " << std::setw(2) << pack->lvl1_z_index;
1513                 file << " type " << pack->cham->name << "-";
1514                 file << "    eta_side  " << std::setw(2) << pack->nstrip_z;
1515                 file << " " << std::setw(2) << pack->nstrip_z/eta_conn << "  2";
1516                 file << "    phi_side  " << std::setw(2) << pack->nstrip_f;
1517                 file << " " << std::setw(2) << pack->nstrip_f/8 << "  2";
1518                 file << "  #MDT chamber " << abs(pack->cham->amdb_z_index)
1519                      << std::endl;
1520                 
1521                 pack=(sec>=32)? pack->next_in_z[proj]:pack->prev_in_z[proj];
1522             }
1523             
1524             file << "}" << std::endl;
1525         }
1526     }
1527     
1528    // exit(1);
1529 }
1530 
1531 
1532 //=============================================================================
1533 //********************* facilities for the data decoding **********************
1534 //=============================================================================
1535 const encodeProj*
1536 RPCGeometry::find_proj(int Jobj) const
1537 {
1538     encodeProj* tmp = projMap;
1539     while(tmp)
1540     {
1541         if (tmp->Jobj == Jobj) return tmp;
1542         tmp = tmp->next;
1543     }
1544 
1545     return 0;
1546 }
1547 
1548 void 
1549 RPCGeometry::add_proj(encodeProj* item)
1550 {
1551     if (!projMap) 
1552     {
1553         projMap = item;
1554         return;
1555     }
1556     
1557     encodeProj* tmp = projMap;
1558     while(tmp)
1559     {
1560         if(tmp->Jobj==item->Jobj && tmp->proj==tmp->proj) return;
1561         if(tmp->Jobj==item->Jobj && tmp->proj!=tmp->proj)
1562         {
1563             std::cout << "RPCGeometry ERROR in projMap:" 
1564                       << " try to insert Jobj=" << item->Jobj
1565                       << " with proj=" << item->proj << " in" << std::endl;
1566             dump_projMap();
1567             exit(1);
1568         }
1569         if (tmp->next == 0) 
1570         {
1571             tmp->next = item;
1572             return;
1573         }
1574         tmp = tmp->next;
1575     }
1576 }
1577 
1578 /*
1579 void
1580 RPCGeometry::check_projMap()
1581 {
1582     encodeProj* tmp = projMap;
1583     while(tmp)
1584     {
1585         encodeProj* match = tmp->next;
1586         while(match)
1587         {
1588             if(tmp->Jobj == match->Jobj)
1589             {
1590                 std::cout << "RPCGeometry ERROR in projMap:" << std::endl;
1591                 dump_projMap();
1592                 exit(1);
1593             }
1594             match = match->next;
1595         }
1596         tmp = tmp->next;
1597     }
1598 }
1599 */
1600 void
1601 RPCGeometry::dump_projMap()
1602 {
1603     encodeProj* tmp = projMap;
1604     while(tmp)
1605     {
1606         std::cout << "RPCGeometry projMap:  Jobj=" << tmp->Jobj
1607                   << ",  proj=" << tmp->proj << std::endl;
1608         tmp = tmp->next;
1609     }
1610 }
1611 
1612 //=============================================================================
1613 //***************** Interface between geometry and LVL1 world *****************
1614 //=============================================================================
1615 
1616 #ifndef LVL1_STANDALONE
1617 bool
1618 RPCGeometry::give_strip_index(unsigned long int code,int& Jtype, 
1619                               int& StationEta, int& StationPhi, int& DoubletR, 
1620                               int& DoubletZ,   int& DoubletP,   int& GasGap, 
1621                               int& MeasuresPhi,int& Strip) const
1622 {
1623     AMDC;
1624 
1625     int Jff;
1626     int Jzz;
1627     int Job;
1628     int Jspli;
1629     int Jsl;
1630     int Jsz;
1631     int Jstri;
1632 
1633     RPCdecoder decode(code);
1634 
1635     if (decode)
1636     {
1637         int z_index      = decode.rpc_z_index();
1638         int lvl1_station = decode.lvl1_station() - 1;
1639         int logic_sector = decode.logic_sector();
1640         int proj         = (logic_sector+1)%2;
1641     
1642         RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1643         while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1644 
1645         if (pack == 0) return false;
1646         
1647         Jff   = (int)((pack->cham->physics_sector)/2) + 1;
1648         Jzz   = pack->cham->amdb_z_index;
1649         Job   = pack->Jobj;
1650         Jspli = (pack->Readout_f==1)? 1 : proj + 1;
1651         Jsl   = decode.rpc_layer() + 1;
1652         Jsz   = decode.strip_type();
1653         Jstri = decode.strip_number() + 1;
1654 
1655         std::string StationName = pack->cham->name;
1656         Jtype = amdc->GetJtyp(StationName);
1657 
1658         amdc->GetRPCindexFromAMDC(StationName,Jff,Jzz,Job,Jspli,Jsl,Jsz,Jstri,
1659                                   StationEta,StationPhi,DoubletR,DoubletZ,
1660                                   DoubletP,GasGap,MeasuresPhi,Strip);
1661                                 
1662     }
1663     else return false;
1664     
1665     return true;
1666 }
1667 
1668 
1669 unsigned long int
1670 RPCGeometry::give_strip_code( std::string StationName, int StationEta,
1671                               int StationPhi, int DoubletR, int DoubletZ,
1672                               int DoubletP, int GasGap, int MeasuresPhi,
1673                               int Strip) const
1674 {
1675     AMDC;
1676 
1677     int Jtype = amdc->GetJtyp(StationName);
1678     int Jff;
1679     int Jzz;
1680     int Job;
1681     int Jspli;
1682     int Jsl;
1683     int Jsz;
1684     int Jstri;
1685 /*
1686     std::cout << "StationName=" << StationName << ",  StationEta=" << StationEta
1687               << ",  StationPhi=" << StationPhi << std::endl
1688               << "DoubletR=" << DoubletR << ",  DoubletZ=" << DoubletZ
1689               << ",  DoubletP=" << DoubletP << std::endl
1690               << "GasGap=" << GasGap << ",  MeasurePhi=" << MeasuresPhi
1691               << ",  Strip=" << Strip << std::endl;
1692 */            
1693     amdc->GetAMDCindexFromRPC(StationName,StationEta,StationPhi,DoubletR,
1694                               DoubletZ,DoubletP,GasGap,MeasuresPhi,Strip,
1695                               Jff,Jzz,Job,Jspli,Jsl,Jsz,Jstri);
1696 
1697     if ( Jtype==4 && Jff!=6 && Jff!=7 && Job>=s_JobMinForEncodeProj &&
1698         (Jzz==2 || Jzz==4 || Jzz==-2 || Jzz==-4) )
1699     {
1700         const encodeProj* tmp = find_proj(Job);
1701         if (!tmp) 
1702         {
1703             std::cout << "RPCGeometry ERROR: can't find proj index" 
1704                       << std::endl;
1705             exit(1);
1706         }
1707         else Jspli = tmp->proj;
1708         //std::cout << "Jobj=" << Job << "  and Jspli=" << Jspli << std::endl;
1709     }
1710     
1711     return this->give_strip_code(Jtype,Jff,Jzz,Job,Jsl,Jsz,Jstri,Jspli);
1712 }
1713 #endif
1714 
1715 unsigned long int
1716 RPCGeometry::give_strip_code(int Jtype,int Jff,int Jzz,int Jobj,int Jlay,
1717                              int Jsty,int Jstr,int proj) const                       
1718 {
1719 
1720 //    printf("Jtype=%d, Jff=%d, Jzz=%d. Jobj=%d. Jlay=%d. Jsty=%d, Jstr=%d,proj=%d\n",
1721 //    Jtype,Jff,Jzz,Jobj,Jlay,Jsty,Jstr,proj);
1722     MultiChamber *cham = T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz];
1723 /*    
1724     std::cout << "Jtype="   << Jtype << ",  Jff=" << Jff << ",  Jzz=" << Jzz 
1725          << std::endl
1726          << ",  Jobj=" << Jobj  << ",  Jlay=" << Jlay  << std::endl
1727          << ",  Jsty=" << Jsty  << ",  Jstr=" << Jstr << ",  proj=" << proj 
1728          << std::endl;
1729 */    
1730     RPC_pack *pack = 0;
1731     
1732     for(int stat=First;stat<=Extended;++stat)
1733     {
1734         RPC_pack *tmp = cham->firstRPC_z[proj-1][stat];
1735                 
1736         while(tmp && tmp->cham == cham)
1737         {
1738 //          printf("Jobj = %d\n",tmp->Jobj);
1739             if(tmp->Jobj == Jobj)
1740             {
1741                 if(pack) 
1742                 {
1743                     printf("RPCGeometry,  give_strip_code: ");
1744                     printf("more than 1 pack found!\n");
1745                     return 0;
1746                 } else
1747                 {
1748                     pack = tmp;
1749                 }
1750             }
1751             tmp = tmp->next_in_z[proj-1];
1752         }
1753     }
1754     
1755     if(!pack)
1756     {
1757         proj++;
1758         for(int stat=First;stat<=Extended;++stat)
1759         {
1760             RPC_pack *tmp = cham->firstRPC_z[proj-1][stat];
1761             while(tmp && tmp->cham == cham)
1762             {
1763 //              printf("Jobj = %d\n",tmp->Jobj);
1764                 if(tmp->Jobj == Jobj)
1765                 {
1766                     if(pack) 
1767                     {
1768                         printf("RPCGeometry,  give_strip_code: ");
1769                         printf("more than 1 pack found!\n");
1770                         return 0;
1771                     } else
1772                     {
1773                         pack = tmp;
1774                     }
1775                 }
1776                 tmp = tmp->next_in_z[proj-1];
1777             }
1778         }
1779     }
1780     
1781     int station = pack->station;
1782     
1783     int z_index = pack->lvl1_z_index;
1784     
1785     int logic_sector = cham->physics_sector*2 + proj - 2;
1786     if (logic_sector < 0 ) logic_sector = 31;
1787         
1788     int nstrip = (Jsty-1)? pack->nstrip_z : pack->nstrip_f;
1789     
1790     if (Jstr > nstrip)
1791     {
1792         printf("RPCGeometry,  give_strip_code: ");
1793         printf("inconsistence on strip number!\n");
1794         printf("    RPC strips = %2d,    digitized strip number = %2d\n",
1795                nstrip,Jstr);
1796         return 0;
1797     }
1798     
1799 //    float Z = *(pack->z_proj + Jstr - 1);
1800 //    if(Z > 0.) logic_sector += 32;
1801     if (Jzz >= 0) logic_sector += 32;
1802     
1803 //    printf ("logic_sector = %d, station=%d\n",logic_sector,station);
1804     
1805     RPC_pack *check = LogicSector[logic_sector][station][0];
1806     
1807     while (check)
1808     {
1809         if(check->lvl1_z_index == z_index) break;
1810         check = check->next_in_z[proj-1];
1811     }
1812     
1813     if (check != pack)
1814     {
1815         printf("RPCGeometry,  give_strip_code: ");
1816         printf("RPC pack inconsistence in Sector Logic map!\n");
1817         exit(1);
1818         return 0;
1819     }
1820     
1821     RPCdecoder decode(Jsty,logic_sector,station+1,Jlay-1,z_index,Jstr-1);    
1822     //unsigned int code = Jsty         * 100000000 +
1823     //                    logic_sector * 1000000   +
1824     //              PoolRDOInput = [ "rfio:/castor/cern.ch/user/d/drebuzzi/preprod9.0.2/p.03-mu+eta0.4-0.9.root" ]    station      * 100000    +
1825     //                  (Jlay - 1)   * 10000     +
1826     //                  z_index      * 100       +
1827     //                  Jstr -  1;
1828     
1829     return decode.code();
1830 }
1831 
1832 
1833 bool
1834 RPCGeometry::give_strip_coordinates(unsigned long int code, float *xyz) const
1835 {
1836     RPCdecoder decode(code);
1837     if (!decode) return false;
1838         
1839     int strip_number = decode.strip_number();
1840     int z_index      = decode.rpc_z_index();
1841     int rpc_layer    = decode.rpc_layer();
1842     int lvl1_station = decode.lvl1_station() - 1;
1843     int logic_sector = decode.logic_sector();
1844     int Jsty         = decode.strip_type();
1845     int proj         = (logic_sector+1)%2;
1846     
1847     
1848     //std::cout << "strip_number = " << strip_number << std::endl;
1849     //std::cout << "z_index = " << z_index << std::endl;
1850     //std::cout << "rpc_layer = " << rpc_layer << std::endl;
1851     //std::cout << "lvl1_station = " << lvl1_station << std::endl;
1852     //std::cout << "logic_sector = " << logic_sector << std::endl; 
1853     //std::cout << "Jsty = " << Jsty << std::endl; 
1854     //std::cout << "proj = " << proj << std::endl; 
1855     
1856     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1857     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1858    
1859     if (pack == 0) return false;
1860    
1861     if(Jsty == 1)
1862     {
1863         xyz[0] = *(pack->f_proj[proj] + strip_number*4 + rpc_layer*2);
1864         xyz[1] = *(pack->f_proj[proj] + strip_number*4 + rpc_layer*2 + 1);
1865         xyz[2] = pack->pack_coo[2];
1866     } else
1867     {   
1868         xyz[0] = pack->gas_layer_XY[proj][rpc_layer][0];
1869         xyz[1] = pack->gas_layer_XY[proj][rpc_layer][1];
1870         xyz[2] = *(pack->z_proj + strip_number);
1871         
1872         //float DeltaZ = pack->pack_ref_coo[2][rpc_layer][0] - xyz[2];
1873         //xyz[0] = pack->pack_ref_coo[0][rpc_layer][0] - 
1874         //         DeltaZ*pack->pack_ref_deltas[0][rpc_layer];
1875         //xyz[1] = pack->pack_ref_coo[1][rpc_layer][0] - 
1876         //         DeltaZ*pack->pack_ref_deltas[1][rpc_layer]; 
1877     }
1878     
1879     return true;
1880 }
1881 
1882 
1883 bool
1884 RPCGeometry::give_strip_radius(unsigned long int code,float& radius) const
1885 {
1886     RPCdecoder decode(code);
1887     if(!decode) return false;
1888 
1889     int z_index      = decode.rpc_z_index();
1890     int rpc_layer    = decode.rpc_layer();
1891     int lvl1_station = decode.lvl1_station() - 1;
1892     int logic_sector = decode.logic_sector();
1893     int proj         = (logic_sector+1)%2;
1894 
1895     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1896     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1897     
1898     if (pack==0) return false;
1899     
1900     radius = pack->gas_layer_radius[rpc_layer];
1901     return true;
1902 }
1903 
1904 bool
1905 RPCGeometry::give_eta_strip_size(unsigned int code,float& size) const
1906 {    
1907     RPCdecoder decode(code);
1908     if(!decode) return false;
1909 
1910     int z_index      = decode.rpc_z_index();
1911     int lvl1_station = decode.lvl1_station() - 1;
1912     int logic_sector = decode.logic_sector();
1913     int proj         = (logic_sector+1)%2;
1914 
1915     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1916     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1917     
1918     if (pack==0) return false;
1919     
1920     size = pack->stripsize_z;
1921     return true;
1922 }
1923 
1924 bool
1925 RPCGeometry::give_phi_strip_size(unsigned int code,float& size) const
1926 {
1927     RPCdecoder decode(code);
1928     if(!decode) return false;
1929 
1930     int z_index      = decode.rpc_z_index();
1931     int lvl1_station = decode.lvl1_station() - 1;
1932     int logic_sector = decode.logic_sector();
1933     int proj         = (logic_sector+1)%2;
1934     
1935     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1936     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1937     
1938     if(pack==0) return false;
1939     
1940     size = pack->stripsize_f;
1941     return true;
1942 }
1943 
1944 bool
1945 RPCGeometry::existCode(unsigned int code) const
1946 {
1947     RPCdecoder decode(code);
1948     if(!decode) return false;
1949 
1950     int z_index      = decode.rpc_z_index();
1951     int lvl1_station = decode.lvl1_station() - 1;
1952     int logic_sector = decode.logic_sector();
1953     int proj         = (logic_sector+1)%2;
1954     
1955     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1956     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1957     
1958     if(pack)
1959     {
1960         int strip_number = decode.strip_number();
1961         int Jsty = decode.strip_type();
1962          
1963         if( Jsty==1 ) {
1964             if(strip_number<pack->nstrip_f) return true;   
1965         } else {
1966             if(strip_number<pack->nstrip_z) return true;   
1967         }
1968     }
1969     
1970     return false;
1971 }
1972 
1973 
1974 
1975 bool 
1976 RPCGeometry::give_station_radius(unsigned int code,float& radius) const
1977 {    
1978     RPCdecoder decode(code);
1979     if(!decode) return false;    
1980     
1981     int z_index      = decode.rpc_z_index();
1982     int rpc_layer    = decode.rpc_layer();
1983     int lvl1_station = decode.lvl1_station() - 1;
1984     int logic_sector = decode.logic_sector();
1985     int proj         = (logic_sector+1)%2;
1986     
1987     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
1988     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
1989     
1990     if(pack==0) return false;
1991     
1992     radius = pack->gas_layer_radius[rpc_layer];
1993     
1994     return true;
1995 }
1996 
1997 
1998 bool
1999 RPCGeometry::give_station_phi(unsigned int code,float& phi) const
2000 {
2001     RPCdecoder decode(code);
2002     if(!decode) return false;    
2003     
2004     int z_index      = decode.rpc_z_index();
2005     int lvl1_station = decode.lvl1_station() - 1;
2006     int logic_sector = decode.logic_sector();
2007     int proj         = (logic_sector+1)%2;
2008     
2009     RPC_pack *pack = LogicSector[logic_sector][lvl1_station][0];
2010     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
2011 
2012     if (pack==0) return false;
2013 
2014     phi = pack->rotation_angle;
2015     
2016     // account for the new PHI coordinate system
2017     //float PI = 3.1415926535899;
2018     //if (phi >= PI) phi = phi -2*PI;
2019     
2020     return true;
2021 }
2022 
2023 bool
2024 RPCGeometry::give_rpc_boundaries(int logic_sector,int station,int z_index,
2025                                  float& zmin, float& zmax) const
2026 {
2027     int proj         = (logic_sector+1)%2;
2028     
2029     RPC_pack* pack = LogicSector[logic_sector][station-1][0];    
2030     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
2031 
2032     if(pack==0) return false;
2033 
2034     zmin = fabsf(pack->pack_coo[2] - pack->half_dim[1]);
2035     zmax = fabsf(pack->pack_coo[2] + pack->half_dim[1]);
2036     
2037     if (zmin > zmax)
2038     {
2039         float tmp = zmax;
2040         zmax = zmin;
2041         zmin = tmp;
2042     }
2043 
2044     return true;
2045 }
2046 
2047 bool
2048 RPCGeometry::give_rpc_XYdim(int logic_sector,int station,int z_index,
2049                                  float& XYdim) const
2050 {
2051     int proj         = (logic_sector+1)%2;
2052     
2053     RPC_pack* pack = LogicSector[logic_sector][station-1][0];    
2054     while(pack && pack->lvl1_z_index != z_index) pack = pack->next_in_z[proj];
2055 
2056     if(pack==0) return false;
2057 
2058     XYdim = pack->half_dim[0];
2059 
2060     return true;
2061 }
2062 
2063 bool
2064 RPCGeometry::isFull() const
2065 {
2066     return !s_testbeam;
2067 }
2068 
2069 //=============================================================================
2070 //*********************** Access to the geometry data *************************
2071 //=============================================================================
2072 
2073 
2074 //#ifndef LVL1_STANDALONE
2075 
2076 void
2077 RPCGeometry::buildgeo() 
2078 {
2079   //amdbgeo();
2080   //amdbtoc();   // fill the AMDB bank
2081     
2082    // printf("Proseguo con la geometria!\n");
2083     
2084    // Amdbsimrec*  amdb = new (Amdbsimrec);
2085    // Amdbsimrec*  amdb = s_amdb;
2086    
2087     AMDC;
2088     
2089     Mtype = amdc->Mtyp();
2090     Mff   = amdc->Mphi();
2091     Mzz   = amdc->Mzz();
2092     
2093     Mtype = amdc->StationTypeEnd();
2094     Mff   = amdc->PositionPhiEnd();
2095     Mzz   = amdc->PositionZEnd();
2096     
2097     
2098 //  ===== INITIALIZE THE GEOMETRY MAP<-----------------------------------------
2099     for (int Jtype=0;Jtype<MaxJtype;++Jtype)
2100     {
2101         for (int J=0;J<Mff*(2*Mzz+1);++J)
2102         {
2103             T1muonGeo[Jtype][J] = 0;
2104         }
2105     }
2106     
2107 //  =========== START LOOPING ON MULTI-CHAMBERS<-------------------------------
2108     for (int Jtype=1;Jtype<=MaxJtype;++Jtype)
2109     {
2110         std::string name = amdc->GetStationType(Jtype);
2111         int MJzz = amdc->PositionZEnd();
2112         
2113                                 
2114         int IsValid;
2115         int MJgeo = amdc->StationGeomEnd();
2116         double DimLocX,DimLocY,DimLocZ,CenterLocX,CenterLocY,CenterLocZ;
2117         
2118         for(int Jgeo = amdc->StationGeomBegin();Jgeo < MJgeo;++Jgeo) {
2119 
2120           int Jcut = 0;
2121           amdc->GetStationDimensions(name,Jgeo,Jcut,IsValid,
2122                                      DimLocX,DimLocY,DimLocZ,
2123                                      CenterLocX,CenterLocY,CenterLocZ );
2124         
2125           if (IsValid > 0){
2126               
2127             std::string GeomTechnoName;
2128             int GeomIsValid,GeomTechnoIndex;
2129             int GeomSplitX,GeomSplitY,GeomShapeIndex;
2130             double GeomDx,GeomDy,GeomDz,GeomWs,GeomWl;
2131             double GeomLe,GeomEx,GeomD1,GeomD2,GeomD3;
2132                  
2133             int NJobj = amdc->GetNumberOfStationObjects(name,Jgeo);
2134             
2135             
2136 //          ================ COMPUTING THE OFFSET OF THE R-MIDDLE            
2137             enum tech {SPA,MDT,RPC};
2138             int exist_tech[3] = {0,0,0};
2139             
2140             for(int Jobj=1;Jobj<=NJobj;Jobj++) {
2141                                                   
2142               int Itec = amdc->ITEC(Jtype,Jgeo,Jobj);
2143               std::string tech_name = amdc->TechnologyName(Itec);
2144 
2145               if (tech_name=="SPA") exist_tech[SPA] = Jobj;
2146               if (tech_name=="CHV") exist_tech[SPA] = Jobj;
2147               if (tech_name=="MDT") exist_tech[MDT] = Jobj;
2148               if (tech_name=="RPC") exist_tech[RPC] = Jobj;
2149             }
2150             
2151             int    JobjMid  = 0;
2152             int    IstaMid  = 0;
2153             int    ItecMid  = 0;
2154             double widthSpa = 0;
2155             
2156             
2157             if (exist_tech[SPA]) {
2158               JobjMid = exist_tech[SPA];
2159               IstaMid = amdc->ISTA(Jtype,Jgeo,JobjMid);
2160               ItecMid = amdc->ITEC(Jtype,Jgeo,JobjMid);
2161               
2162               std::string name = amdc->TechnologyName(ItecMid);
2163               
2164               widthSpa = (name == "CHV")? (amdc->STAPP(ItecMid,IstaMid))/2. :
2165                                           (amdc->STAEE(ItecMid,IstaMid))/2.;
2166             } else if (exist_tech[MDT]) {
2167               JobjMid  = exist_tech[MDT];
2168               IstaMid = amdc->ISTA(Jtype,Jgeo,JobjMid);
2169               ItecMid = amdc->ITEC(Jtype,Jgeo,JobjMid);
2170               widthSpa = (amdc->STAEE(ItecMid,IstaMid))/2.;
2171             } else if (exist_tech[RPC]) {
2172               JobjMid  = exist_tech[RPC];
2173               IstaMid = amdc->ISTA(Jtype,Jgeo,JobjMid);
2174               ItecMid = amdc->ITEC(Jtype,Jgeo,JobjMid);
2175               widthSpa = (amdc->STATT(ItecMid,IstaMid,9))/2.;
2176             }
2177             
2178             
2179             amdc->GetStationObjectParam( name,Jgeo,JobjMid,GeomIsValid,
2180                                          GeomTechnoName,GeomTechnoIndex, 
2181                                          GeomSplitX,GeomSplitY,
2182                                          GeomShapeIndex,GeomDx,GeomDy,
2183                                          GeomDz,GeomWs,GeomWl,GeomLe,  
2184                                          GeomEx,GeomD1,GeomD2,GeomD3 );
2185             
2186             double RmidOffset = GeomDz + widthSpa;
2187                                     
2188             
2189             if (!RmidOffset) {
2190               printf("RPCGeometry: error in computing RmidOffset!\n");
2191               exit(1);
2192             }
2193             
2194 
2195             // Start looping over all stations
2196             for (int Jff=amdc->PositionPhiBegin();Jff<=Mff;++Jff) {   
2197               for (int Jzz=amdc->PositionZBegin();Jzz<=MJzz;++Jzz) {
2198               
2199                 int PosiIsValid,PosiJgeo,PosiJcut,PosiIsBarrel;
2200                 double PosiPhi,PosiZ,PosiR,PosiS;
2201                 double PosiAlfa, PosiBeta,PosiGamma;
2202                              
2203                 amdc->GetStationPositionParam(name,Jff,Jzz,PosiIsValid,
2204                                               PosiJgeo,PosiJcut,PosiIsBarrel,
2205                                               PosiPhi,PosiZ,PosiR,PosiS,
2206                                               PosiAlfa,PosiBeta,PosiGamma);
2207                                                 
2208                 if( PosiIsValid > 0 && PosiJgeo == Jgeo ) {
2209 
2210                   //if(!m_full && Jff == 1) m_full = true;
2211                                 
2212                   MultiChamber*  cham = 0;
2213                   RPC_pack* last_pack = 0; 
2214                   
2215                   double Rmid = nor(PosiR + RmidOffset);
2216                                     
2217                   for(int Jobj=1;Jobj<=NJobj;Jobj++) {
2218                 
2219                   amdc->GetStationObjectParam( name,Jgeo,Jobj,GeomIsValid,
2220                                                GeomTechnoName,GeomTechnoIndex, 
2221                                                GeomSplitX,GeomSplitY,
2222                                                GeomShapeIndex,GeomDx,GeomDy,
2223                                                GeomDz,GeomWs,GeomWl,GeomLe,  
2224                                                GeomEx,GeomD1,GeomD2,GeomD3 );
2225                                                   
2226                   int Ista = amdc->ISTA(Jtype,Jgeo,Jobj);
2227                   int Itec = amdc->ITEC(Jtype,Jgeo,Jobj);             
2228                   std::string tech_name = amdc->TechnologyName(Itec);
2229                           
2230                     if(tech_name == "RPC")
2231                     {
2232 
2233 ///////////////////////////////////////////////////////////////////////////////
2234 //                          ==================================== CHAM INIT <---
2235                             if (!cham)
2236                             {
2237                                 cham = new (MultiChamber);
2238                                 cham->Rmid = Rmid;
2239                                 cham->name = name;
2240                                 cham->type = Jgeo;
2241                                 cham->amdb_z_index = Jzz;
2242                                 cham->physics_sector =(name.c_str()[2] == 'L')?
2243                                                       (Jff-1)*2 : (Jff-1)*2+1;
2244                                 cham->nRPC_packs = 0;
2245                                 cham->RPC_packs  = 0;
2246                                 cham->next = 0;
2247                                 for(int i=0;i<2;++i)for(int j=First;j<=Extended;++j)
2248                                     cham->firstRPC_z[i][j] = 0;
2249                                 T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz] =
2250                                 cham;
2251                             }
2252                             
2253                             RPC_pack* pack = new (RPC_pack);
2254                             pack->Jobj        = Jobj;
2255                             pack->Readout_f   = 0;
2256                             pack->cham        = cham;
2257                             pack->next = 0;
2258                             pack->z_proj = 0;
2259                             pack->lvl1_z_index = 0;
2260                             for(int i=0;i<2;++i)
2261                             {
2262                                 pack->f_proj[i] = 0;
2263                                 pack->next_in_z[i] = 0;
2264                                 pack->prev_in_z[i] = 0;
2265                                 for(int j=0;j<2;++j) for(int k=0;k<2;++k)
2266                                     pack->gas_layer_XY[i][j][k] = 0.;
2267                             }
2268                             
2269 //                          ======================== STRIP SIZE AND NUMBER <---
2270                             pack->stripsize_f = nor(amdc->STATT(Itec,Ista,10));
2271                             pack->stripsize_z = nor(amdc->STATT(Itec,Ista,11));
2272                             
2273                             float Readout_f = amdc->STAOO(Itec,Ista,2);
2274                             // Change to account Anna's whishes!
2275                             // float Readout_z = amdc->STAOO(Itec,Ista,3);
2276                             float Readout_z = 1.;
2277                             float Length    = nor(GeomLe);
2278                             float Width     = nor(GeomWs);
2279                             float Dead      = nor(GeomD1);
2280                             
2281                             pack->Readout_f = (int) Readout_f;
2282                             
2283                             pack->nstrip_z=(int)((Length/Readout_z - Dead)/
2284                                                   pack->stripsize_z);
2285                             pack->nstrip_f=(int)((Width/Readout_f - Dead)/
2286                                                   pack->stripsize_f);
2287                             
2288 //                          ========================== CHAMBER POSITION <------
2289                             pack->half_dim[0] = Width/2.;
2290                             pack->half_dim[1] = Length/2.;
2291                             pack->half_dim[2] = nor(amdc->STATT(Itec,Ista,9))/2.;
2292                             
2293                             
2294                             float shift_rad = nor(GeomDz);
2295                             float shift_z   = nor(GeomDy);
2296                             float shift_s   = nor(GeomDx);
2297                             float orto_rad  = nor(PosiS);
2298                             bool  isOrtoRad = (orto_rad>15.) ? true : false;
2299                             float x = nor(PosiR)+ shift_rad + pack->half_dim[2];
2300                             float y = shift_s + orto_rad;
2301                             float z = nor(PosiZ)+ shift_z + pack->half_dim[1];
2302                             float phi = PosiPhi;
2303                             
2304                             
2305                             float tmp[3] = {x,y,z};
2306                                                 
2307                             rotate(nor(PosiZ),nor(PosiR),orto_rad,
2308                                    PosiAlfa,PosiBeta,PosiGamma,tmp);
2309                                                                     
2310                             pack->rotation_angle = phi;
2311                             
2312                             x = tmp[0];
2313                             y = tmp[1];
2314                             z = tmp[2];
2315                             
2316                             pack->pack_coo[0] = x*cos(phi) - y*sin(phi);
2317                             pack->pack_coo[1] = x*sin(phi) + y*cos(phi);
2318                             pack->pack_coo[2] = z;                                              
2319                                                 
2320                                                 
2321                                                 
2322                             if (name.c_str()[1] == 'O')
2323                             {                                           
2324                                 pack->station = Third;
2325                                 if (name.c_str()[2] == 'F' ||
2326                                     name.c_str()[2] == 'G'   )
2327                                 {
2328                                     pack->station = (x<Rmid)? Third : Extended;   
2329                                 }
2330                             } else
2331                             {
2332                                 pack->station = (x<Rmid)? First : Second;
2333                                 if( !exist_tech[SPA] && !exist_tech[MDT])
2334                                 pack->station = Second;
2335                                 //std::cout << "special pack=" << pack->station
2336                                 //     << std::endl;
2337                             }
2338                             
2339                             
2340 //                          ========================== RPC INTERNAL STRUCTURE<-
2341                             float rpc_thickness= nor(amdc->STATT(Itec,Ista,4));
2342                             float low_honeycomb= nor(amdc->STATT(Itec,Ista,7));
2343                             float cen_honeycomb= nor(amdc->STATT(Itec,Ista,5));
2344                             float gas_volume   = nor(amdc->STATT(Itec,Ista,13));
2345                             float bakelite     = nor(amdc->STATT(Itec,Ista,14));
2346                             float gas_gap_pet  = nor(amdc->STAOO(Itec,Ista,7));
2347                             float gas_gap_air  = nor(amdc->STAOO(Itec,Ista,8));
2348                             float str_thickness= nor(amdc->STATT(Itec,Ista,15));
2349                             float str_support  = nor(amdc->STATT(Itec,Ista,16));
2350                             float str_pet_upper= nor(amdc->STAOO(Itec,Ista,9));
2351                             float str_pet_lower= nor(amdc->STAOO(Itec,Ista,10));
2352                             
2353 //                         printf("rpc_thickness     = %f\n",rpc_thickness);
2354 //                         printf("lower_honeycomb   = %f\n",low_honeycomb);
2355 //                         printf("central_honeycomb = %f\n",cen_honeycomb);
2356 //                         printf("gas_volume        = %f\n",gas_volume);
2357 //                         printf("bakelite          = %f\n",bakelite);
2358 //                         printf("gas_gap_pet       = %f\n",gas_gap_pet);
2359 //                         printf("gas_gap_air       = %f\n",gas_gap_air);
2360 //                         printf("strip_thickness   = %f\n",str_thickness);
2361 //                         printf("strip_support     = %f\n",str_support);
2362 //                         printf("strip_pet_upper   = %f\n",str_pet_upper);
2363 //                         printf("strip_pet_lower   = %f\n",str_pet_lower);
2364 
2365                             float gas_center  = str_thickness + 
2366                                                 str_support*2 + 
2367                                                 str_pet_upper + 
2368                                                 str_pet_lower +
2369                                                 gas_gap_pet   +
2370                                                 gas_gap_air   +
2371                                                 bakelite      +
2372                                                 gas_volume/2. ;
2373                                                  
2374                             float mid_layer_1 = low_honeycomb + 
2375                                                 gas_center; 
2376                                                
2377                             float mid_layer_2 = low_honeycomb +
2378                                                 rpc_thickness +
2379                                                 cen_honeycomb +
2380                                                 gas_center;
2381                             
2382                             float radius_lay1 = nor(PosiR) + shift_rad +
2383                                                 mid_layer_1;
2384                             float radius_lay2 = nor(PosiR) + shift_rad +
2385                                                 mid_layer_2;
2386                                                 
2387                             if(amdc->ISHAPE(Jtype,Jgeo,Jobj) == -1)
2388                             {
2389                                 radius_lay1 -= low_honeycomb;
2390                                 radius_lay2 -= low_honeycomb;
2391                             }
2392                             
2393                             pack->gas_layer_radius[0] = radius_lay1;
2394                             pack->gas_layer_radius[1] = radius_lay2;
2395                             
2396                             
2397                             // Sertup coordinate systems for radius computation
2398                             float Ref1[3];
2399                             Ref1[0] = radius_lay1;
2400                             Ref1[1] = shift_s + orto_rad;
2401                             Ref1[2] = nor(PosiZ) + shift_z + pack->half_dim[1];
2402                             
2403                             float Ref2[3];
2404                             Ref2[0] = radius_lay1;
2405                             Ref2[1] = shift_s + orto_rad;
2406                             Ref2[2] = nor(PosiZ) + shift_z;
2407                             
2408                             rotate(nor(PosiZ),nor(PosiR),orto_rad,
2409                                    PosiAlfa,PosiBeta,PosiGamma,Ref1);
2410                             
2411                             rotate(nor(PosiZ),nor(PosiR),orto_rad,
2412                                    PosiAlfa,PosiBeta,PosiGamma,Ref2);
2413                             
2414                             for (int nco=0;nco<3;++nco)
2415                             {
2416                                 pack->pack_ref_coo[nco][0][0] = Ref2[nco];
2417                                 pack->pack_ref_coo[nco][0][1] = Ref1[nco];
2418                             }
2419                             
2420                             float DeltaX = pack->pack_ref_coo[0][0][0] -
2421                                            pack->pack_ref_coo[0][0][1];
2422                             float DeltaY = pack->pack_ref_coo[1][0][0] -
2423                                            pack->pack_ref_coo[1][0][1];
2424                             float DeltaZ = pack->pack_ref_coo[2][0][0] -
2425                                            pack->pack_ref_coo[2][0][1];
2426                             
2427                             pack->pack_ref_deltas[0][0] = DeltaX/DeltaZ;
2428                             pack->pack_ref_deltas[1][0] = DeltaY/DeltaZ;
2429                             
2430                             
2431                             
2432                             Ref1[0] = radius_lay2;
2433                             Ref1[1] = shift_s + orto_rad;
2434                             Ref1[2] = nor(PosiZ) + shift_z + pack->half_dim[1];
2435                             
2436                             Ref2[0] = radius_lay2;
2437                             Ref2[1] = shift_s + orto_rad;
2438                             Ref2[2] = nor(PosiZ) + shift_z;
2439                             
2440                             rotate(nor(PosiZ),nor(PosiR),orto_rad,
2441                                    PosiAlfa,PosiBeta,PosiGamma,Ref1);
2442                             
2443                             rotate(nor(PosiZ),nor(PosiR),orto_rad,
2444                                    PosiAlfa,PosiBeta,PosiGamma,Ref2);
2445                             
2446                             for (int nco=0;nco<3;++nco)
2447                             {
2448                                 pack->pack_ref_coo[nco][1][0] = Ref2[nco];
2449                                 pack->pack_ref_coo[nco][1][1] = Ref1[nco];
2450                             }
2451                             
2452                             DeltaX = pack->pack_ref_coo[0][1][0] -
2453                                      pack->pack_ref_coo[0][1][1];
2454                             DeltaY = pack->pack_ref_coo[1][1][0] -
2455                                      pack->pack_ref_coo[1][1][1];
2456                             DeltaZ = pack->pack_ref_coo[2][1][0] -
2457                                      pack->pack_ref_coo[2][1][1];
2458                             
2459                             pack->pack_ref_deltas[0][1] = DeltaX/DeltaZ;
2460                             pack->pack_ref_deltas[1][1] = DeltaY/DeltaZ;
2461                             
2462                             float Zp = nor(PosiZ);
2463                             float Rp = nor(PosiR);
2464                             float Sp = orto_rad;
2465                             float Aa = PosiAlfa;
2466                             float Ba = PosiBeta;
2467                             float Ga = PosiGamma;
2468 
2469                             if((int)Readout_f == 2)
2470                             {
2471                                 for(int i=0;i<2;++i) for(int j=0;j<2;++j)
2472                                 {
2473                                     float sy = (i)? pack->half_dim[0]/2. :
2474                                                     -pack->half_dim[0]/2.;
2475                                     float sx = pack->gas_layer_radius[j];
2476                                     float z  = nor(PosiZ)+ shift_z + 
2477                                                pack->half_dim[1];
2478                                                
2479                                     float Ce[3] = {sx,sy,z};
2480                                     rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2481                                     float X = Ce[0]*cos(phi) - Ce[1]*sin(phi);
2482                                     float Y = Ce[0]*sin(phi) + Ce[1]*cos(phi);
2483                                     pack->gas_layer_XY[i][j][0] = X;
2484                                     pack->gas_layer_XY[i][j][1] = Y;
2485                                 }
2486                             } else
2487                             {
2488                                 //std::cout << "Jtype=" << Jtype << ", Jff="
2489                                 //          << Jff << ", Jzz=" << Jzz << ", Jobj="
2490                                 //        << Jobj << ",  shift_s=" << shift_s
2491                                 //        << std::endl;
2492                                 if (Jtype!=4 || Jff==6 || Jff==7 || //Jobj<=16 ||
2493                                     !(Jzz==-2 || Jzz==-4 || Jzz==2 || Jzz==4))
2494                                 {
2495                                     std::cout << "RPCGeometry WARNING:"
2496                                               << " ambiguities in data decoding"
2497                                               << std::endl;
2498                                 }
2499                                 if(Jobj <= s_JobMinForEncodeProj)
2500                                     s_JobMinForEncodeProj = Jobj; 
2501                                 encodeProj* tmp = new (encodeProj);
2502                                 tmp->Jobj = Jobj;
2503                                 tmp->proj = (shift_s<=0)? 1 : 2;
2504                                 tmp->next = 0;
2505                                 add_proj(tmp);
2506                                 
2507                                 //warning: do not trust orto_rad !!!!!!
2508                                 int i = (shift_s<0||(orto_rad<0 && isOrtoRad))? 0:1;
2509                                 for(int j=0;j<2;++j)
2510                                 {
2511                                     float sy = shift_s + orto_rad;
2512                                     float sx = pack->gas_layer_radius[j];
2513                                     float z  = nor(PosiZ)+ shift_z + 
2514                                                pack->half_dim[1];
2515                                     
2516                                     
2517                                     float Ce[3] = {sx,sy,z};
2518                                     rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2519                                     float X = Ce[0]*cos(phi) - Ce[1]*sin(phi);
2520                                     float Y = Ce[0]*sin(phi) + Ce[1]*cos(phi);
2521                                     pack->gas_layer_XY[i][j][0] = X;
2522                                     pack->gas_layer_XY[i][j][1] = Y;
2523                                 }
2524                             }
2525 //                          ========================== SETUP OF THE Z STRIPS<--
2526                             float *str_z = new float [pack->nstrip_z];
2527                             
2528                             float sign_z  = (Jzz)? (Jzz)/abs(Jzz) : +1;
2529                             
2530                             float start_position = (sign_z > 0)?
2531                                                    nor(PosiZ) + shift_z:
2532                                                    nor(PosiZ) + shift_z+ Length;
2533            
2534                             float offset = (pack->half_dim[1]*2 - 
2535                                   pack->stripsize_z * pack->nstrip_z)/2.;
2536                             float start_z = start_position + (offset +
2537                                   pack->stripsize_z/2.)*sign_z; 
2538 
2539                             //float start_z = start_position + Dead*sign_z + 
2540                             //      sign_z*pack->stripsize_z/2.;
2541 
2542                             for (int i=0;i<pack->nstrip_z;++i)
2543                             {
2544                                 float x = pack->gas_layer_radius[0];
2545                                 float y = shift_s + orto_rad;
2546                                 float z = start_z + i*pack->stripsize_z*sign_z;
2547                                 float Ce[3] = {pack->gas_layer_radius[0],0,z};
2548                                 rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2549                                 *(str_z+i) = Ce[2];
2550                             }
2551                             
2552                             pack->z_proj = str_z;
2553 
2554 //                          ========================== SETUP OF THE F STRIPS<--
2555                             //-06-05-2005:
2556                             //modified for reversing the order of Phi strip
2557                             //numbering.
2558                             
2559                             if ((int)Readout_f == 2) 
2560                             {
2561                                 float* str_f1 = new float [4*pack->nstrip_f];
2562                                 float* str_f2 = new float [4*pack->nstrip_f];
2563                       
2564                                 float offset = (pack->half_dim[0] - 
2565                                   pack->stripsize_f * pack->nstrip_f)/2.;
2566                               
2567                                 //-06-05-2005 
2568                                 //float start_y = pack->half_dim[0] - offset  + 
2569                                 //                  shift_s + orto_rad -
2570                                 //                  pack->stripsize_f/2.;
2571                                 float start_y = offset + shift_s + orto_rad +
2572                                                 pack->stripsize_f/2.;   
2573 
2574                                 for (int i=0;i<pack->nstrip_f;++i)
2575                                     for(int r=0;r<2;++r)
2576                                     {
2577                                         //-06-05-2005
2578                                         //float sy = start_y-i*pack->stripsize_f;
2579                                         float sy = start_y+i*pack->stripsize_f;
2580                                         float sx = pack->gas_layer_radius[r];
2581                                         float z  = nor(PosiZ)+ shift_z + 
2582                                                    pack->half_dim[1];
2583                                                    
2584                                         float Ce[3] = {sx,sy,z};
2585                                         rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2586                                         float X = Ce[0]*cos(phi) - Ce[1]*sin(phi);
2587                                         float Y = Ce[0]*sin(phi) + Ce[1]*cos(phi);
2588                                         *(str_f1+i*4+r*2)   = X;
2589                                         *(str_f1+i*4+r*2+1) = Y;
2590                                     }
2591                                 
2592                                 
2593                                 //-06-05-2005
2594                                 //start_y = - offset + shift_s + orto_rad -
2595                                 //            pack->stripsize_f/2.;
2596                                             
2597                                 start_y = - pack->half_dim[0] + offset 
2598                                           + shift_s + orto_rad +
2599                                             pack->stripsize_f/2.;                               
2600                                 
2601                                 for (int i=0;i<pack->nstrip_f;++i)
2602                                     for(int r=0;r<2;++r)
2603                                     {
2604                                         //-06-05-2005
2605                                         //float sy = start_y-i*pack->stripsize_f;
2606                                         float sy = start_y+i*pack->stripsize_f;
2607                                         float sx = pack->gas_layer_radius[r];
2608                                         float z  = nor(PosiZ)+ shift_z + 
2609                                                    pack->half_dim[1];
2610                                         
2611                                         float Ce[3] = {sx,sy,z};
2612                                         rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2613                                         float X = Ce[0]*cos(phi) - Ce[1]*sin(phi);
2614                                         float Y = Ce[0]*sin(phi) + Ce[1]*cos(phi);
2615                                         *(str_f2+i*4+r*2)   = X;
2616                                         *(str_f2+i*4+r*2+1) = Y;
2617                                     }
2618                                 
2619                                 pack->f_proj[1] = str_f1;
2620                                 pack->f_proj[0] = str_f2;
2621                             
2622                             } else
2623                             {
2624                                 float* str_f1 = new float [4*pack->nstrip_f];
2625                                 
2626                                 //-06-05-2005
2627                                 //float start_y = pack->half_dim[0] - Dead +
2628                                 //                shift_s + orto_rad -
2629                                 //              pack->stripsize_f/2.;
2630                                 float start_y = -pack->half_dim[0] + Dead +
2631                                                 shift_s + orto_rad +
2632                                                 pack->stripsize_f/2.;
2633                                 
2634                                 for (int i=0;i<pack->nstrip_f;++i)
2635                                     for(int r=0;r<2;++r)
2636                                     {
2637                                         //-06-05-2005
2638                                         //float sy = start_y-i*pack->stripsize_f;
2639                                         float sy = start_y+i*pack->stripsize_f;
2640                                         float sx = pack->gas_layer_radius[r];
2641                                         float z  = nor(PosiZ)+ shift_z + 
2642                                                    pack->half_dim[1];
2643                                         
2644                                         float Ce[3] = {sx,sy,z};
2645                                         rotate(Zp,Rp,Sp,Aa,Ba,Ga,Ce);
2646                                         float X = Ce[0]*cos(phi) - Ce[1]*sin(phi);
2647                                         float Y = Ce[0]*sin(phi) + Ce[1]*cos(phi);
2648                                         *(str_f1+i*4+r*2)   = X;
2649                                         *(str_f1+i*4+r*2+1) = Y;
2650                                     }
2651                                 
2652                                 if(shift_s!=0&&(orto_rad!=0 && isOrtoRad)) 
2653                                 {
2654                                     printf("RPCGeometry: inconsistence!\n");
2655                                     printf("    shift_s=%f,  orto_rad=%f\n",
2656                                            shift_s,orto_rad);
2657                                     exit(1);
2658                                 }
2659                                 
2660                                 if(shift_s<0||(orto_rad<0 && isOrtoRad)) 
2661                                 {
2662                                     pack->f_proj[1] = 0;
2663                                     pack->f_proj[0] = str_f1;
2664                                 } else
2665                                 {
2666                                     pack->f_proj[1] = str_f1;
2667                                     pack->f_proj[0] = 0;
2668                                 }
2669                                 
2670                                 
2671                             }
2672                             
2673                             
2674                             //tmp[0] = pack->gas_layer_radius[0];
2675                             //tmp[1] = shift_s + orto_rad;
2676                             //tmp[2] = nor(PosiZ)+ shift_z + pack->half_dim[1]; 
2677                             //rotate(Zp,Rp,Sp,Aa,Ba,Ga,tmp);
2678                             //pack->gas_layer_radius[0] = tmp[0];
2679                             
2680                             //tmp[0] = pack->gas_layer_radius[1];
2681                             //tmp[1] = shift_s + orto_rad;
2682                             //tmp[2] = nor(PosiZ)+ shift_z + pack->half_dim[1]; 
2683                             //rotate(Zp,Rp,Sp,Aa,Ba,Ga,tmp);
2684                             //pack->gas_layer_radius[1] = tmp[0];
2685                             
2686                             
2687                             ++(cham->nRPC_packs);
2688                             if (!cham->RPC_packs)
2689                             {
2690                                 cham->RPC_packs = pack;
2691                             } else
2692                             {
2693                                 last_pack->next = pack;
2694                             }
2695                             last_pack = pack;
2696 
2697                /*
2698                 printf("--------------------------------------------------\n");
2699                 printf("MultiChamber type %s: Jtype=%d, Jff=%d, Jzz=%d\n",
2700                                    name.c_str(),Jtype,Jff,Jzz);
2701                 printf("MultiChamber position: Z=%9.2f,  R=%9.2f,  PHI=%9.2f\n",
2702 
2703                                    amdc->PosZ(Jtype,Jff,Jzz),
2704                                    amdc->PosR(Jtype,Jff,Jzz),
2705                                    amdc->PosPhi(Jtype,Jff,Jzz));
2706                 printf("MultiChamber position: Rmid=%9.2f\n",cham->Rmid);
2707           printf("Rpc object: Jobj=%d, type=%d, strip_z=%9.2f, strip_f=%9.2f\n",
2708                                  Jobj,Ista,pack->stripsize_z,pack->stripsize_f);
2709                 printf("RPC station: trigger station = %d,  Readout_z = %f",
2710                                  pack->station,Readout_z);
2711                 printf(",  Readout_f = %f\n",Readout_f);
2712                 printf("RPC station: nstrip_z = %d,  nstrip__f = %d\n",
2713                                  pack->nstrip_z,pack->nstrip_f);
2714                 printf("RPC station: chamber center=> x=%f, y=%f, z=%f\n",
2715                         pack->pack_coo[0],pack->pack_coo[1],pack->pack_coo[2]);
2716                 printf("RPC shift position: dx=%9.2f,  dy=%9.2f, dz=%9.2f\n",
2717                                    amdc->Geodx(Jtype,Jgeo,Jobj),
2718                                    amdc->Geody(Jtype,Jgeo,Jobj),
2719                                    amdc->Geodz(Jtype,Jgeo,Jobj));
2720                 printf("RPC chamber dimension: width=%9.2f,  length=%9.2f\n",
2721                                    amdc->GeoWs(Jtype,Jgeo,Jobj),
2722                                    amdc->GeoLe(Jtype,Jgeo,Jobj));
2723                 printf("RPC dead spaces: D1=%9.2f,  D2=%9.2f, D3=%9.2f\n",
2724                                    amdc->GeoD1(Jtype,Jgeo,Jobj),
2725                                    amdc->GeoD2(Jtype,Jgeo,Jobj),
2726                                    amdc->GeoD3(Jtype,Jgeo,Jobj));
2727                 printf("RPC radius: radius1=%9.2f,  radius2=%9.2f\n",
2728                                    pack->gas_layer_radius[0],
2729                                    pack->gas_layer_radius[1]);
2730                 printf("Pippo\n"); */                   
2731                         
2732                     }
2733               
2734                   }      
2735                 }  // end loop over stations objects
2736 
2737               }
2738             }  // end loop over all stations
2739             
2740             
2741           }
2742         }
2743         
2744         
2745 /*      
2746         //printf ("Name=%s, Mzz=%d \n",name.c_str(),MJzz);
2747         for (int Jff=1;Jff<=Mff;++Jff)
2748         {
2749             for (int Jzz=-MJzz;Jzz<=MJzz;++Jzz)
2750             {           
2751                 if (int Jgeo = amdc->IGEO(Jtype,Jff,Jzz))
2752                 {
2753                     MultiChamber*  cham = 0;
2754                     RPC_pack* last_pack = 0;
2755                     int MJobj = amdc->NOBJ(Jtype,Jgeo);
2756                     //int MJobj = amdc->GetNumberOfStationObjects(name,Jgeo);
2757 
2758 //                  ================ COMPUTING THE R-MIDDLE OF THE MULTICHAMBER
2759                     float Rmid = 0;
2760                     
2761                     enum tech {SPA,MDT,RPC};
2762                     int exist_tech[3] = {0,0,0};
2763                     
2764                     for (int Jobj=1;Jobj<=MJobj;++Jobj)
2765                     {
2766                         int Itec = amdc->ITEC(Jtype,Jgeo,Jobj);
2767                         std::string tech_name = amdc->TechnologyName(Itec);
2768                         if (tech_name=="SPA") exist_tech[SPA] = Jobj;
2769                         if (tech_name=="MDT") exist_tech[MDT] = Jobj;
2770                         if (tech_name=="RPC") exist_tech[RPC] = Jobj;
2771                     }
2772                     
2773                     if (exist_tech[SPA])
2774                     {
2775                         int Ista = amdc->ISTA(Jtype,Jgeo,exist_tech[SPA]);
2776                         int Itec = amdc->ITEC(Jtype,Jgeo,exist_tech[SPA]);
2777                         Rmid = amdc->PosR(Jtype,Jff,Jzz)/1.                +
2778                                amdc->Geodz(Jtype,Jgeo,exist_tech[SPA])/1.  +
2779                                amdc->STAEE(Itec,Ista)/2.;
2780                     } else if (exist_tech[MDT])
2781                     {
2782                         int Ista = amdc->ISTA(Jtype,Jgeo,exist_tech[MDT]);
2783                         int Itec = amdc->ITEC(Jtype,Jgeo,exist_tech[MDT]);
2784                         Rmid = amdc->PosR(Jtype,Jff,Jzz)/1.                +
2785                                amdc->Geodz(Jtype,Jgeo,exist_tech[MDT])/1.  +
2786                                amdc->STAEE(Itec,Ista)/2.;
2787                     } else if (exist_tech[RPC])
2788                     {
2789                         int Ista = amdc->ISTA(Jtype,Jgeo,exist_tech[RPC]);
2790                         int Itec = amdc->ITEC(Jtype,Jgeo,exist_tech[RPC]);
2791                         Rmid = amdc->PosR(Jtype,Jff,Jzz)/1.                +
2792                                amdc->Geodz(Jtype,Jgeo,exist_tech[RPC])/1.  +
2793                                amdc->STATT(Itec,Ista,9)/2.;
2794                     }
2795                                     
2796                     if (!Rmid) 
2797                     {
2798                         printf("RPCGeometry: error in computing Rmid!\n");
2799                         exit(1);
2800                     }
2801                     
2802 //                  ===================== COMPUTING CHAMBER STRUCTURE <--------
2803                     for (int Jobj=1;Jobj<=MJobj;++Jobj)
2804                     {
2805                         int Itec = amdc->ITEC(Jtype,Jgeo,Jobj);
2806                         if (amdc->TechnologyName(Itec)=="RPC")
2807                         {
2808                             int Ista = amdc->ISTA(Jtype,Jgeo,Jobj);
2809                             
2810 //                          ==================================== CHAM INIT <---
2811                             if (!cham)
2812                             {
2813                                 cham = new (MultiChamber);
2814                                 cham->Rmid = Rmid;
2815                                 cham->name = name;
2816                                 cham->type = Jgeo;
2817                                 cham->amdb_z_index = Jzz;
2818                                 cham->physics_sector =(name.c_str()[2] == 'L')?
2819                                                       (Jff-1)*2 : (Jff-1)*2+1;
2820                                 cham->nRPC_packs = 0;
2821                                 cham->RPC_packs  = 0;
2822                                 cham->next = 0;
2823                                 for(int i=0;i<2;++i)for(int j=First;j<=Third;++j)
2824                                     cham->firstRPC_z[i][j] = 0;
2825                                 T1muonGeo[Jtype-1][(Jff-1)*(2*Mzz+1)+Jzz+Mzz] =
2826                                 cham;
2827                             }
2828                             
2829                             RPC_pack* pack = new (RPC_pack);
2830                             pack->Jobj        = Jobj;
2831                             pack->Readout_f   = 0;
2832                             pack->cham        = cham;
2833                             pack->next = 0;
2834                             pack->z_proj = 0;
2835                             pack->lvl1_z_index = 0;
2836                             for(int i=0;i<2;++i)
2837                             {
2838                                 pack->f_proj[i] = 0;
2839                                 pack->next_in_z[i] = 0;
2840                                 pack->prev_in_z[i] = 0;
2841                                 for(int j=0;j<2;++j) for(int k=0;k<2;++k)
2842                                     pack->gas_layer_XY[i][j][k] = 0.;
2843                             }
2844                             
2845 //                          ======================== STRIP SIZE AND NUMBER <---
2846                             pack->stripsize_f = amdc->STATT(Itec,Ista,10)/1.;
2847                             pack->stripsize_z = amdc->STATT(Itec,Ista,11)/1.;
2848                             
2849                             float Readout_f = amdc->STAOO(Itec,Ista,2);
2850                             // Change to account Anna's whishes!
2851                             // float Readout_z = amdc->STAOO(Itec,Ista,3);
2852                             float Readout_z = 1.;
2853                             float Length    = amdc->GeoLe(Jtype,Jgeo,Jobj)/1.;
2854                             float Width     = amdc->GeoWs(Jtype,Jgeo,Jobj)/1.;
2855                             float Dead      = amdc->GeoD1(Jtype,Jgeo,Jobj)/1.;
2856                             
2857                             pack->Readout_f = (int) Readout_f;
2858                             
2859                             pack->nstrip_z=(int)((Length/Readout_z - Dead)/
2860                                                   pack->stripsize_z);
2861                             pack->nstrip_f=(int)((Width/Readout_f - Dead)/
2862                                                   pack->stripsize_f);
2863                             
2864 //                          ========================== CHAMBER POSITION <------
2865                             pack->half_dim[0] = Width/2.;
2866                             pack->half_dim[1] = Length/2.;
2867                             pack->half_dim[2] = amdc->STATT(Itec,Ista,9)/2.;
2868                             
2869                             
2870                             float shift_rad = amdc->Geodz(Jtype,Jgeo,Jobj)/1.;
2871                             float shift_z   = amdc->Geody(Jtype,Jgeo,Jobj)/1.;
2872                             float shift_s   = amdc->Geodx(Jtype,Jgeo,Jobj)/1.;
2873                             float orto_rad  = amdc->PosS(Jtype,Jff,Jzz)/1.;
2874                             float x = amdc->PosR(Jtype,Jff,Jzz)/1.+ shift_rad+
2875                                       pack->half_dim[2];
2876                             float y = shift_s + orto_rad;
2877                             float phi = amdc->PosPhi(Jtype,Jff,Jzz);
2878                                                                     
2879                             pack->rotation_angle = phi;
2880                             
2881                             pack->pack_coo[0] = x*cos(phi) - y*sin(phi);
2882                             pack->pack_coo[1] = x*sin(phi) + y*cos(phi);
2883                             pack->pack_coo[2] = amdc->PosZ (Jtype,Jff,Jzz)/1.+
2884                                                 shift_z + pack->half_dim[1];
2885                                                 
2886                             if (name.c_str()[1] == 'O')
2887                             {                                           
2888                                 pack->station = Third;
2889                             } else
2890                             {
2891                                 pack->station = (x<Rmid)? First : Second;
2892                             }
2893                             
2894                             
2895 //                          ========================== RPC INTERNAL STRUCTURE<-
2896                             float rpc_thickness= amdc->STATT(Itec,Ista,4)/1.;
2897                             float low_honeycomb= amdc->STATT(Itec,Ista,7)/1.;
2898                             float cen_honeycomb= amdc->STATT(Itec,Ista,5)/1.;
2899                             float gas_volume   = amdc->STATT(Itec,Ista,13)/1.;
2900                             float bakelite     = amdc->STATT(Itec,Ista,14)/1.;
2901                             float gas_gap_pet  = amdc->STAOO(Itec,Ista,7)/1.;
2902                             float gas_gap_air  = amdc->STAOO(Itec,Ista,8)/1.;
2903                             float str_thickness= amdc->STATT(Itec,Ista,15)/1.;
2904                             float str_support  = amdc->STATT(Itec,Ista,16)/1.;
2905                             float str_pet_upper= amdc->STAOO(Itec,Ista,9)/1.;
2906                             float str_pet_lower= amdc->STAOO(Itec,Ista,10)/1.;
2907                             
2908 //                         printf("rpc_thickness     = %f\n",rpc_thickness);
2909 //                         printf("lower_honeycomb   = %f\n",low_honeycomb);
2910 //                         printf("central_honeycomb = %f\n",cen_honeycomb);
2911 //                         printf("gas_volume        = %f\n",gas_volume);
2912 //                         printf("bakelite          = %f\n",bakelite);
2913 //                         printf("gas_gap_pet       = %f\n",gas_gap_pet);
2914 //                         printf("gas_gap_air       = %f\n",gas_gap_air);
2915 //                         printf("strip_thickness   = %f\n",str_thickness);
2916 //                         printf("strip_support     = %f\n",str_support);
2917 //                         printf("strip_pet_upper   = %f\n",str_pet_upper);
2918 //                         printf("strip_pet_lower   = %f\n",str_pet_lower);
2919 
2920                             float gas_center  = str_thickness + 
2921                                                 str_support*2 + 
2922                                                 str_pet_upper + 
2923                                                 str_pet_lower +
2924                                                 gas_gap_pet   +
2925                                                 gas_gap_air   +
2926                                                 bakelite      +
2927                                                 gas_volume/2. ;
2928                                                  
2929                             float mid_layer_1 = low_honeycomb + 
2930                                                 gas_center; 
2931                                                
2932                             float mid_layer_2 = low_honeycomb +
2933                                                 rpc_thickness +
2934                                                 cen_honeycomb +
2935                                                 gas_center;
2936 
2937                             float radius_lay1 = amdc->PosR(Jtype,Jff,Jzz)/1. +
2938                                                 shift_rad +
2939                                                 mid_layer_1;
2940                             float radius_lay2 = amdc->PosR(Jtype,Jff,Jzz)/1. +
2941                                                 shift_rad +
2942                                                 mid_layer_2;
2943                                                 
2944                             if(amdc->ISHAPE(Jtype,Jgeo,Jobj) == -1)
2945                             {
2946                                 radius_lay1 -= low_honeycomb;
2947                                 radius_lay2 -= low_honeycomb;
2948                             }
2949                             
2950                             pack->gas_layer_radius[0] = radius_lay1;
2951                             pack->gas_layer_radius[1] = radius_lay2;
2952                             
2953                             if((int)Readout_f == 2)
2954                             {
2955                                 for(int i=0;i<2;++i) for(int j=0;j<2;++j)
2956                                 {
2957                                     float sy = (i)? pack->half_dim[0]/2. :
2958                                                     -pack->half_dim[0]/2.;
2959                                     float sx = pack->gas_layer_radius[j];
2960                                     
2961                                     float X = sx*cos(phi) - sy*sin(phi);
2962                                     float Y = sx*sin(phi) + sy*cos(phi);
2963                                     pack->gas_layer_XY[i][j][0] = X;
2964                                     pack->gas_layer_XY[i][j][1] = Y;
2965                                 }
2966                             } else
2967                             {
2968                                 int i = (shift_s<0||orto_rad<0)? 0:1;
2969                                 for(int j=0;j<2;++j)
2970                                 {
2971                                     float sy = shift_s + orto_rad;
2972                                     float sx = pack->gas_layer_radius[j];
2973                                     
2974                                     float X = sx*cos(phi) - sy*sin(phi);
2975                                     float Y = sx*sin(phi) + sy*cos(phi);
2976                                     pack->gas_layer_XY[i][j][0] = X;
2977                                     pack->gas_layer_XY[i][j][1] = Y;
2978                                 }
2979                             }
2980 //                          ========================== SETUP OF THE Z STRIPS<--
2981                             float *str_z = new float [pack->nstrip_z];
2982                             
2983                             float sign_z  = (Jzz)? (Jzz)/abs(Jzz) : +1;
2984                             
2985                             float start_position = (sign_z > 0)?
2986                                amdc->PosZ(Jtype,Jff,Jzz)/10. + shift_z:
2987                                amdc->PosZ(Jtype,Jff,Jzz)/10. + shift_z+ Length;
2988            
2989                             float offset = (pack->half_dim[1]*2 - 
2990                                   pack->stripsize_z * pack->nstrip_z)/2.;
2991                             float start_z = start_position + (offset +
2992                                   pack->stripsize_z/2.)*sign_z; 
2993 
2994                             //float start_z = start_position + Dead*sign_z + 
2995                             //      sign_z*pack->stripsize_z/2.;
2996 
2997                             for (int i=0;i<pack->nstrip_z;++i)
2998                             {
2999                                 *(str_z+i) = start_z +
3000                                                  i*pack->stripsize_z*sign_z;
3001                             }
3002                             
3003                             pack->z_proj = str_z;
3004 
3005 //                          ========================== SETUP OF THE F STRIPS<--
3006                             //-10-12-2003:
3007                             //modified for reversing the order of Phi strip
3008                             //numbering.
3009                             
3010                             if ((int)Readout_f == 2) 
3011                             {
3012                                 float* str_f1 = new float [4*pack->nstrip_f];
3013                                 float* str_f2 = new float [4*pack->nstrip_f];
3014                       
3015                                 float offset = (pack->half_dim[0] - 
3016                                   pack->stripsize_f * pack->nstrip_f)/2.;
3017                                   
3018                               //-10-12-2003 
3019                               //float start_y = pack->half_dim[0] - offset  + 
3020                               //                  shift_s + orto_rad -
3021                               //                  pack->stripsize_f/2.;
3022                                 float start_y = offset + shift_s + orto_rad +
3023                                                 pack->stripsize_f/2.;           
3024                               //float start_y = pack->half_dim[0] - Dead  + 
3025                               //                  shift_s + orto_rad -
3026                               //                  pack->stripsize_f/2.;
3027 
3028                                 for (int i=0;i<pack->nstrip_f;++i)
3029                                     for(int r=0;r<2;++r)
3030                                     {
3031                                       //-10-12-2003
3032                                       //float sy = start_y-i*pack->stripsize_f;
3033                                         float sy = start_y+i*pack->stripsize_f;
3034                                         float sx = pack->gas_layer_radius[r];
3035                                         float X = sx*cos(phi) - sy*sin(phi);
3036                                         float Y = sx*sin(phi) + sy*cos(phi);
3037                                         *(str_f1+i*4+r*2)   = X;
3038                                         *(str_f1+i*4+r*2+1) = Y;
3039                                     }
3040                                 
3041                                 //-10-12-2003
3042                                 //start_y = - offset + shift_s + orto_rad -
3043                                 //            pack->stripsize_f/2.;
3044                                             
3045                                 start_y = - pack->half_dim[0] + offset 
3046                                           + shift_s + orto_rad +
3047                                             pack->stripsize_f/2.;                               
3048                                 //start_y = - Dead + shift_s + orto_rad -
3049                                 //            pack->stripsize_f/2.;
3050                                 for (int i=0;i<pack->nstrip_f;++i)
3051                                     for(int r=0;r<2;++r)
3052                                     {
3053                                       //-10-12-2003
3054                                       //float sy = start_y-i*pack->stripsize_f;
3055                                         float sy = start_y+i*pack->stripsize_f;
3056                                         float sx = pack->gas_layer_radius[r];
3057                                         float X = sx*cos(phi) - sy*sin(phi);
3058                                         float Y = sx*sin(phi) + sy*cos(phi);
3059                                         *(str_f2+i*4+r*2)   = X;
3060                                         *(str_f2+i*4+r*2+1) = Y;
3061                                     }
3062                                 
3063                                 pack->f_proj[1] = str_f1;
3064                                 pack->f_proj[0] = str_f2;
3065                             
3066                             } else
3067                             {
3068                                 float* str_f1 = new float [4*pack->nstrip_f];
3069                                 
3070                                 //-10-12-2003
3071                                 //float start_y = pack->half_dim[0] - Dead +
3072                                 //                shift_s + orto_rad -
3073                                 //              pack->stripsize_f/2.;
3074                                 float start_y = -pack->half_dim[0] + Dead +
3075                                                 shift_s + orto_rad +
3076                                                 pack->stripsize_f/2.;
3077                                                                 
3078                                 for (int i=0;i<pack->nstrip_f;++i)
3079                                     for(int r=0;r<2;++r)
3080                                     { 
3081                                       //-10-12-2003
3082                                       //float sy = start_y-i*pack->stripsize_f;
3083                                         float sy = start_y+i*pack->stripsize_f;
3084                                         float sx = pack->gas_layer_radius[r];
3085                                         float X = sx*cos(phi) - sy*sin(phi);
3086                                         float Y = sx*sin(phi) + sy*cos(phi);
3087                                         *(str_f1+i*4+r*2)   = X;
3088                                         *(str_f1+i*4+r*2+1) = Y;
3089                                     }
3090                                 
3091                                 if(shift_s!=0&&orto_rad!=0) 
3092                                 {
3093                                     printf("RPCGeometry: inconsistence!\n");
3094                                     printf("    shift_s=%f,  orto_rad=%f\n",
3095                                            shift_s,orto_rad);
3096                                     exit(1);
3097                                 }
3098                                 
3099                                 if(shift_s<0||orto_rad<0) 
3100                                 {
3101                                     pack->f_proj[1] = 0;
3102                                     pack->f_proj[0] = str_f1;
3103                                 } else
3104                                 {
3105                                     pack->f_proj[1] = str_f1;
3106                                     pack->f_proj[0] = 0;
3107                                 }
3108                             }
3109 
3110                             ++(cham->nRPC_packs);
3111                             if (!cham->RPC_packs)
3112                             {
3113                                 cham->RPC_packs = pack;
3114                             } else
3115                             {
3116                                 last_pack->next = pack;
3117                             }
3118                             last_pack = pack;
3119 
3120                
3121                 printf("--------------------------------------------------\n");
3122                 printf("MultiChamber type %s: Jtype=%d, Jff=%d, Jzz=%d\n",
3123                                    name.c_str(),Jtype,Jff,Jzz);
3124                 printf("MultiChamber position: Z=%9.2f,  R=%9.2f,  PHI=%9.2f\n",
3125 
3126                                    amdc->PosZ(Jtype,Jff,Jzz),
3127                                    amdc->PosR(Jtype,Jff,Jzz),
3128                                    amdc->PosPhi(Jtype,Jff,Jzz));
3129                 printf("MultiChamber position: Rmid=%9.2f\n",cham->Rmid);
3130           printf("Rpc object: Jobj=%d, type=%d, strip_z=%9.2f, strip_f=%9.2f\n",
3131                                  Jobj,Ista,pack->stripsize_z,pack->stripsize_f);
3132                 printf("RPC station: trigger station = %d,  Readout_z = %f",
3133                                  pack->station,Readout_z);
3134                 printf(",  Readout_f = %f\n",Readout_f);
3135                 printf("RPC station: nstrip_z = %d,  nstrip__f = %d\n",
3136                                  pack->nstrip_z,pack->nstrip_f);
3137                 printf("RPC station: chamber center=> x=%f, y=%f, z=%f\n",
3138                         pack->pack_coo[0],pack->pack_coo[1],pack->pack_coo[2]);
3139                 printf("RPC shift position: dx=%9.2f,  dy=%9.2f, dz=%9.2f\n",
3140                                    amdc->Geodx(Jtype,Jgeo,Jobj),
3141                                    amdc->Geody(Jtype,Jgeo,Jobj),
3142                                    amdc->Geodz(Jtype,Jgeo,Jobj));
3143                 printf("RPC chamber dimension: width=%9.2f,  length=%9.2f\n",
3144                                    amdc->GeoWs(Jtype,Jgeo,Jobj),
3145                                    amdc->GeoLe(Jtype,Jgeo,Jobj));
3146                 printf("RPC dead spaces: D1=%9.2f,  D2=%9.2f, D3=%9.2f\n",
3147                                    amdc->GeoD1(Jtype,Jgeo,Jobj),
3148                                    amdc->GeoD2(Jtype,Jgeo,Jobj),
3149                                    amdc->GeoD3(Jtype,Jgeo,Jobj));
3150                 printf("RPC radius: radius1=%9.2f,  radius2=%9.2f\n",
3151                                    pack->gas_layer_radius[0],
3152                                    pack->gas_layer_radius[1]);
3153                 printf("Pippo\n"); 
3154                         }
3155                     }
3156                     
3157                     
3158                 }
3159             }
3160         }
3161 */      
3162         
3163     }
3164 }
3165 
3166 //#else
3167 
3168 void
3169 RPCGeometry::rotate(float ZposCha, float RposCha, float OrtoRadp,
3170                     float Alfa, float Beta, float Gamma,
3171                     float xyz[3]) const
3172 {
3173     
3174     Alfa  = -Alfa*s_pi/180;
3175     Beta  = -Beta*s_pi/180;
3176     Gamma = -Gamma*s_pi/180;
3177     
3178     float t = xyz[0] - RposCha;
3179     float s = xyz[1] - OrtoRadp;
3180     float z = xyz[2] - ZposCha;
3181     
3182     //implement rotation around t axis
3183     s =  s*cos(Alfa) + z*sin(Alfa); 
3184     z = -s*sin(Alfa) + z*cos(Alfa); 
3185     
3186     //implement rotation around z axis
3187     t =  t*cos(Beta) + s*sin(Beta); 
3188     s = -t*sin(Beta) + s*cos(Beta); 
3189     
3190     //implement rotation around s axis
3191     z =  z*cos(Gamma) + t*sin(Gamma); 
3192     t = -z*sin(Gamma) + t*cos(Gamma); 
3193     
3194     xyz[0] = t + RposCha;
3195     xyz[1] = s + OrtoRadp;
3196     xyz[2] = z + ZposCha; 
3197     
3198 }
3199 
3200 void
3201 RPCGeometry::readgeo() const
3202 {
3203   /*
3204     ifstream t2mugeo;
3205     Header header;
3206     
3207     const Lvl1Configuration* config = Lvl1Configuration::instance();
3208     
3209     t2mugeo.open(config->geometry_file().c_str(),ios::in | ios::binary);
3210     
3211     if(!t2mugeo) {
3212       cout << "Cannot open file t1mugeo in the working directory" << endl;
3213       exit (1);
3214     }
3215         
3216     while (t2mugeo.peek()!=EOF) {           // start looping on all geom data.
3217       t2mugeo.read((unsigned char *) &header, sizeof header);
3218       t2mugeo.read((unsigned char *) &incha, sizeof incha);
3219       t2mubgeo(&header.JJ,&header.JFF,&header.JZZ);
3220     }
3221 
3222     t2mugeo.close();                       // close the geometry data file.
3223   */
3224 }
3225 
3226 //#endif
3227 

source navigation ] diff markup ] identifier search ] general search ]

Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems
This page was automatically generated by the LXR engine. Valid HTML 4.01!