Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:54

0001 #!/usr/bin/env perl
0002 
0003 
0004 #18012008 Various tests of EE numberings
0005 use warnings;
0006 use strict;
0007 $|++;
0008 
0009 use POSIX;
0010 
0011 package CondDB::channelView;
0012 
0013 sub new {
0014   my $proto = shift;
0015   my $class = ref($proto) || $proto;
0016   my $this = {};
0017 
0018   $this->{condDB} = shift;
0019   die "Usage:  channelView->new( \$condDB )\n" unless $this->{condDB};
0020 
0021   $this->{defs} = {
0022            'DUMMY' => \&define_dummy,
0023            'ECAL' => \&define_ECAL,
0024            'EB' => \&define_EB,
0025            'EE' => \&define_EE,
0026            'EB_crystal_number' => \&define_EB_crystal_number,
0027            'EB_elec_crystal_number' => \&define_EB_elec_crystal_number,
0028            'EB_fe_crystal_number' => \&define_EB_fe_crystal_number,
0029            'ECAL_crystal_number_fedccuxt' => \&define_ECAL_crystal_number_fedccuxt,
0030            'ECAL_readout_strip' => \&define_ECAL_readout_strip,
0031            'ECAL_trigger_tower' => \&define_ECAL_trigger_tower,
0032            'EB_crystal_index' => \&define_EB_crystal_index,
0033            'EB_trigger_tower' => \&define_EB_trigger_tower,
0034            'EB_readout_tower' => \&define_EB_readout_tower,
0035            'EB_supermodule' => \&define_EB_supermodule,
0036            'EB_module' => \&define_EB_module,
0037            'EB_HV_channel' => \&define_EB_HV_channel,
0038            'EB_HV_board' => \&define_EB_HV_board,
0039            'EB_LV_channel' => \&define_EB_LV_channel,
0040            'EB_ESS_temp' => \&define_EB_ESS_temp,
0041            'EB_PTM_H_amb' => \&define_EB_PTM_H_amb,
0042            'EB_PTM_T_amb' => \&define_EB_PTM_T_amb,
0043            'EB_token_ring' => \&define_EB_token_ring,
0044            'EB_LM_channel' => \&define_EB_LM_channel,
0045            'EB_LM_side' => \&define_EB_LM_side,
0046            'EB_LM_PN' => \&define_EB_LM_PN,
0047            'EB_T_capsule' => \&define_EB_T_capsule,
0048            'EB_VFE' => \&define_EB_VFE,
0049            'EB_LVRB_DCU' => \&define_EB_LVRB_DCU,
0050            'EB_LVRB_T_sensor' => \&define_EB_LVRB_T_sensor,
0051            'EB_mem_TT' => \&define_EB_mem_TT,
0052            'EB_mem_channel' => \&define_EB_mem_channel,
0053            'EB_trigger_crystal_number' => \&define_EB_trigger_crystal_number,
0054            'ECAL_crystal_number_fedccuxt_to_EB_crystal_number' => \&define_ECAL_crystal_number_fedccuxt_to_EB_crystal_number,
0055            'ECAL_trigger_tower_to_EB_trigger_tower' => \&define_ECAL_trigger_tower_to_EB_trigger_tower,
0056            'EB_readout_tower_to_EB_trigger_tower' => \&define_EB_readout_tower_to_EB_trigger_tower,
0057            'EB_crystal_number_to_EB_trigger_tower' => \&define_EB_crystal_number_to_EB_trigger_tower,
0058            'EB_crystal_number_to_EB_LV_channel'
0059            => \&define_EB_crystal_number_to_EB_LV_channel,
0060            'EB_crystal_number_to_EB_module'
0061            => \&define_EB_crystal_number_to_EB_module,
0062            'EB_crystal_number_to_EB_HV_channel'
0063            => \&define_EB_crystal_number_to_EB_HV_channel,
0064            'EB_crystal_number_to_EB_LV_channel'
0065            => \&define_EB_crystal_number_to_EB_LV_channel,
0066            'EB_crystal_number_to_EB_LM_channel'
0067            => \&define_EB_crystal_number_to_EB_LM_channel,
0068            'EB_crystal_number_to_EB_LM_PN'
0069            => \&define_EB_crystal_number_to_EB_LM_PN,
0070            'EB_crystal_number_to_EB_LM_side'
0071            => \&define_EB_crystal_number_to_EB_LM_side,
0072            'EB_LM_side_to_EB_crystal_number'
0073            => \&define_EB_LM_side_to_EB_crystal_number,
0074            'EB_crystal_number_to_EB_T_capsule'
0075            => \&define_EB_crystal_number_to_EB_T_capsule,
0076            'EB_T_capsule_to_EB_crystal_number'
0077            => \&define_EB_T_capsule_to_EB_crystal_number,
0078            'EB_crystal_number_to_EB_VFE'
0079            => \&define_EB_crystal_number_to_EB_VFE,
0080            'EB_crystal_number_to_EB_elec_crystal_number',
0081            => \&define_EB_crystal_number_to_EB_elec_crystal_number,
0082            'EB_crystal_number_to_EB_fe_crystal_number'
0083            => \&define_EB_crystal_number_to_EB_fe_crystal_number,
0084            'EB_elec_crystal_number_to_EB_crystal_number',
0085            => \&define_EB_elec_crystal_number_to_EB_crystal_number,
0086            'EB_constr_crystal_number_to_EB_crystal_number'
0087            => \&define_EB_constr_crystal_number_to_EB_crystal_number,
0088            'EB_constr_supermodule_to_EB_supermodule'
0089            => \&define_EB_constr_supermodule_to_EB_supermodule,
0090            'EB_crystal_number_to_ECAL_LMR'
0091            => \&define_EB_crystal_number_to_ECAL_LMR,
0092            'EB_fe_crystal_number_to_EB_crystal_number',
0093            => \&define_EB_fe_crystal_number_to_EB_crystal_number,
0094 
0095         #endcap
0096            'EE_side' => \&define_EE_side,
0097            'EE_D' => \&define_EE_D,
0098            'EE_sector' => \&define_EE_sector,
0099            'EE_DCC' =>\&define_EE_DCC,  
0100            'EE_crystal_number' => \&define_EE_crystal_number,
0101            'ECAL_TCC'=>\&define_ECAL_TCC,
0102            'EE_crystal_hashed' => \&define_EE_crystal_hashed,
0103            'EE_trigger_tower'=>\&define_EE_trigger_tower,
0104            'EE_readout_tower'=>\&define_EE_readout_tower,
0105            'EE_readout_strip'=>\&define_EE_readout_strip,
0106            'EE_trigger_strip'=>\&define_EE_trigger_strip,
0107            'EE_crystal_readout_strip'=>\&define_EE_crystal_readout_strip,
0108            'EE_crystal_trigger_strip'=>\&define_EE_crystal_trigger_strip,
0109            'EE_elec_crystal_number'=>\&define_EE_elec_crystal_number,
0110            'EE_readout_tower_xyz'=>\&define_EE_readout_tower_xyz,
0111            'EE_readout_tower_zseccu'=>\&define_EE_readout_tower_zseccu,
0112            'EE_HV_channel'=>\&define_EE_HV_channel,
0113            'EE_HVA_channel'=>\&define_EE_HVA_channel,
0114            'EE_HVD_channel'=>\&define_EE_HVD_channel,
0115            'EE_LV_channel'=>\&define_EE_LV_channel,
0116            'EE_readout_strip_zseccu'=>\&define_EE_readout_strip_zseccu,
0117            'EE_LM_LMM'=>\&define_EE_LM_LMM,
0118            'EE_LM_PN'=>\&define_EE_LM_PN,
0119            'EE_PTM_T_amb'=>\&define_EE_PTM_T_amb,
0120            'EE_PTM_H_amb'=>\&define_EE_PTM_H_amb,
0121            'EE_LM_PN'=>\&define_EE_LM_PN,
0122            'ECAL_LMR'=>\&define_ECAL_LMR,
0123            'ECAL_DCC'=>\&define_ECAL_DCC,
0124            'ECAL_DCC_input'=>\&define_ECAL_DCC_input,
0125            'ECAL_SRP'=>\&define_ECAL_SRP,
0126 
0127         #endcap mappings
0128                    'ECAL_crystal_number_fedccuxt_to_EE_crystal_number' => \&define_ECAL_crystal_number_fedccuxt_to_EE_crystal_number,
0129            'EE_readout_tower_to_EE_crystal_number'=>\&define_EE_readout_tower_to_EE_crystal_number,
0130            'EE_trigger_tower_to_EE_crystal_number'=>\&define_EE_trigger_tower_to_EE_crystal_number,
0131            'EE_crystal_number_to_EE_trigger_tower'=>\&define_EE_crystal_number_to_EE_trigger_tower,
0132            'EE_crystal_number_to_EE_readout_tower'=>\&define_EE_crystal_number_to_EE_readout_tower,
0133            'EE_crystal_number_to_EE_readout_strip'=>\&define_EE_crystal_number_to_EE_readout_strip,
0134            'EE_crystal_readout_strip_to_EE_crystal_number'=>\&define_EE_crystal_readout_strip_to_EE_crystal_number,
0135            'EE_crystal_trigger_strip_to_EE_crystal_number'=>\&define_EE_crystal_trigger_strip_to_EE_crystal_number,
0136            'EE_crystal_number_to_EE_LV_channel'        => \&define_EE_crystal_number_to_EE_LV_channel,
0137            'EE_crystal_number_to_EE_HV_channel'        => \&define_EE_crystal_number_to_EE_HV_channel,
0138            'EE_readout_tower_xyz_to_EE_readout_tower'=>\&define_EE_readout_tower_xyz_to_EE_readout_tower,
0139            'EE_readout_tower_xyz_to_EE_HV_channel'=>\&define_EE_readout_tower_xyz_to_EE_HV_channel,
0140            'EE_readout_tower_xyz_to_EE_HVA_channel'=>\&define_EE_readout_tower_xyz_to_EE_HVA_channel,
0141            'EE_readout_tower_xyz_to_EE_HVD_channel'=>\&define_EE_readout_tower_xyz_to_EE_HVD_channel,
0142            'EE_readout_tower_xyz_to_EE_LV_channel'=>\&define_EE_readout_tower_xyz_to_EE_LV_channel,
0143            'EE_crystal_number_to_EE_sector'        => \&define_EE_crystal_number_to_EE_sector,
0144            'EE_sector_to_EE_crystal_number'        => \&define_EE_sector_to_EE_crystal_number,
0145            'EE_readout_tower_zseccu_to_EE_readout_tower'=>\&define_EE_readout_tower_zseccu_to_EE_readout_tower,
0146            'EE_readout_strip_zseccu_to_EE_readout_strip'=>\&define_EE_readout_strip_zseccu_to_EE_readout_strip ,
0147            'EE_crystal_number_to_EE_LM_LMM'    => \&define_EE_crystal_number_to_EE_LM_LMM,
0148            'EE_crystal_number_to_EE_LM_PN'         => \&define_EE_crystal_number_to_EE_LM_PN,
0149            'EE_crystal_number_to_ECAL_LMR'     => \&define_EE_crystal_number_to_ECAL_LMR,
0150                    'EE_trigger_tower_to_EE_offline_towerid' => \&define_EE_trigger_tower_to_EE_offline_towerid,
0151                    'EE_trigger_tower_to_EE_offline_towerid_more' => \&define_EE_trigger_tower_to_EE_offline_towerid_more,
0152            'EE_trigger_strip_to_EE_offline_stripid' => \&define_EE_trigger_strip_to_EE_offline_stripid,
0153            'ECAL_readout_strip_to_EE_readout_strip' => \&define_ECAL_readout_strip_to_EE_readout_strip,
0154            'ECAL_readout_strip_to_EE_offline_stripid' => \&define_ECAL_readout_strip_to_EE_offline_stripid,
0155            'ECAL_readout_strip_to_EB_VFE' => \&define_ECAL_readout_strip_to_EB_VFE
0156 
0157      };
0158 
0159   bless($this, $class);
0160   return $this;
0161 }
0162 
0163 
0164 sub define {
0165   my $this = shift;
0166   my $def = shift;
0167 
0168   if (exists $this->{defs}->{$def}) {
0169     $this->import_def( &{$this->{defs}->{$def}} );
0170   } else {
0171     warn "No such definition:  $def\n";
0172   }
0173 }
0174 
0175 sub define_all {
0176   my $this = shift;
0177 
0178   foreach my $def (keys %{$this->{defs}}) {
0179     $this->import_def( &{ $this->{defs}->{$def} } );
0180   }
0181 }
0182 
0183 sub import_def {
0184   my $this = shift;
0185   my $def = shift;
0186   unless ($def->{logic_ids} && $def->{channel_ids}) {
0187     die "ERROR:  import_def() needs logic_ids and channel_ids!\n";
0188   }
0189 
0190   my $condDB = $this->{condDB};
0191 
0192 
0193   my $logic_ids = $def->{logic_ids};
0194   my $channel_ids = $def->{channel_ids};
0195   my $count = scalar @{$logic_ids};
0196 
0197   print "\t$def->{name}:  $count channels...";
0198   $condDB->begin_work();
0199 
0200   $def->{maps_to} = $def->{name} unless defined $def->{maps_to};
0201 
0202   if ($def->{description} || $def->{idnames}) {
0203     $condDB->new_channelView_type(-name => $def->{name},
0204                   -description => $def->{description},
0205                   -idnames => $def->{idnames},
0206                   -maps_to => $def->{maps_to});
0207   }
0208 
0209   for my $i (0..$count-1) {
0210   #  print "\t\t$i inserting ".$$logic_ids[$i]."\n";
0211     $condDB->insert_channel(-name => $def->{name},
0212                 -maps_to =>$def->{maps_to},
0213                 -channel_ids => $$channel_ids[$i],
0214                 -logic_id => $$logic_ids[$i]
0215                );
0216   }
0217   $condDB->commit();
0218   print "Done.\n";
0219 }
0220 
0221 sub define_dummy {
0222   my $name = "DUMMY";
0223   my $idnames = [];
0224   my $description = "A dummy logic_id for testing purposes";
0225   
0226   my @logic_ids = (-1);
0227   my @channel_ids = ([]);
0228 
0229   return {name => $name, idnames => $idnames,
0230       description => $description, logic_ids => \@logic_ids,
0231      channel_ids => \@channel_ids};
0232 }
0233 
0234 sub define_EE_side {
0235 
0236     my $name = "EE_side";
0237     my $idnames = ["side"];
0238     my $description = "Endcap side (wrt z axis)";
0239 
0240     my @channel_ids;
0241     my @logic_ids;
0242 
0243     #side = 2: EE+ - side = 0: EE- in logicId assignment.
0244     
0245     #FIXME hardwired?
0246     push @logic_ids, 2000000012;
0247     push @logic_ids, 2000000010;
0248 
0249     push @channel_ids, [1];
0250     push @channel_ids, [-1];
0251 
0252     return {name => $name, idnames => $idnames,
0253         description => $description, logic_ids => \@logic_ids,
0254         channel_ids => \@channel_ids};
0255 
0256 }
0257 
0258 sub define_EE_D {
0259 
0260     my $name = "EE_D";
0261     my $idnames = ["D"];
0262     my $description = "Endcap Dee";
0263 
0264     my @channel_ids;
0265     my @logic_ids;
0266 
0267     foreach my $D (1..4) {
0268         my $logic_id = sprintf "20000001%02d", $D;
0269         push @logic_ids, $logic_id;
0270         push @channel_ids, [$D];
0271     }
0272 
0273     return {name => $name, idnames => $idnames,
0274         description => $description, logic_ids => \@logic_ids,
0275         channel_ids => \@channel_ids};
0276 
0277 }
0278 
0279 
0280 sub define_EE_sector {
0281 
0282     my $name = "EE_sector";
0283     my $idnames = ["side", "sector"];
0284     my $description = "Data Sectors by number in Endcap";
0285 
0286     my @channel_ids;
0287     my @logic_ids;
0288 
0289     foreach my $side (1,-1) {
0290         my $sideIndex = $side + 1;
0291         foreach my $sector (1..9) {
0292             my $logic_id = sprintf "2000001%01d%02d", $sideIndex, $sector;
0293             push @logic_ids, $logic_id;
0294             push @channel_ids, [$side, $sector];
0295         }
0296     }
0297 
0298     return {name => $name, idnames => $idnames,
0299         description => $description, logic_ids => \@logic_ids,
0300         channel_ids => \@channel_ids};
0301 
0302 }
0303 
0304 sub define_EE_DCC {
0305 
0306     my $name = "EE_DCC";
0307     my $idnames = ["DCC"];
0308     my $description = "DCC sectors by number in Endcap";
0309 
0310     my @channel_ids;
0311     my @logic_ids;
0312 
0313     #EE-
0314     foreach my $DCC (601..609) {
0315         my  $logic_id = sprintf "2000001%03d", $DCC;
0316         push @logic_ids, $logic_id;
0317         push @channel_ids, [$DCC];
0318     }
0319     #EE+
0320     foreach my $DCC (646..654) {
0321         my $logic_id = sprintf "2000001%03d", $DCC;
0322         push @logic_ids, $logic_id;
0323         push @channel_ids, [$DCC];
0324     }
0325 
0326     return {name => $name, idnames => $idnames,
0327         description => $description, logic_ids => \@logic_ids,
0328         channel_ids => \@channel_ids};
0329 
0330 }
0331 
0332 sub define_EE_crystal_number {
0333 
0334     my $name = "EE_crystal_number";
0335     my $idnames = ["side", "ix", "iy"];
0336     my $description = "Crystals in Ecal Endcap by number";
0337 
0338     my @channel_ids;
0339     my @logic_ids;
0340 
0341     #opening file
0342     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0343     #reading it into an array
0344     my @lines = <FILE>;
0345     #getting the first line out 
0346     shift @lines;
0347 
0348     #temp variables
0349     my $ix;
0350     my $iy;
0351     my $side;
0352 
0353     foreach my $line (@lines) {
0354 
0355         my @channels = split (/ /, $line);
0356         $ix = $channels[0];
0357         $iy = $channels[1];
0358         $side = $channels[2];
0359         my $sideIndex = $side + 1;
0360 
0361         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
0362         push @logic_ids, $logic_id;
0363         push @channel_ids, [$side, $ix, $iy];
0364     }
0365 
0366     close(FILE);
0367 
0368     return {name => $name, idnames => $idnames,
0369         description => $description, logic_ids => \@logic_ids,
0370         channel_ids => \@channel_ids};
0371 
0372 }
0373 
0374 sub define_ECAL_TCC {
0375 
0376     my $name = "ECAL_TCC";
0377     my $idnames = ["TCC"];
0378     my $description = "TCC sectors by number in ECAL";
0379 
0380     my @channel_ids;
0381     my @logic_ids;
0382 
0383     #whole ECAL
0384     foreach my $TCC (1..108) {
0385         my  $logic_id = sprintf "1%03d", $TCC;
0386         push @logic_ids, $logic_id;
0387         push @channel_ids, [$TCC];
0388     }
0389     
0390     return {name => $name, idnames => $idnames,
0391         description => $description, logic_ids => \@logic_ids,
0392         channel_ids => \@channel_ids};
0393 }
0394 
0395 sub define_ECAL_DCC {
0396 
0397     my $name = "ECAL_DCC";
0398     my $idnames = ["DCC"];
0399     my $description = "DCC number in ECAL";
0400 
0401     my @channel_ids;
0402     my @logic_ids;
0403 
0404     #whole ECAL
0405     foreach my $DCC (601..654) {
0406         my  $logic_id = sprintf "2%03d", $DCC;
0407         push @logic_ids, $logic_id;
0408         push @channel_ids, [$DCC];
0409     }
0410     
0411     return {name => $name, idnames => $idnames,
0412         description => $description, logic_ids => \@logic_ids,
0413         channel_ids => \@channel_ids};
0414 }
0415 
0416 sub define_ECAL_DCC_input {
0417 
0418     my $name = "ECAL_DCC_input";
0419     my $idnames = ["DCC", "channel"];
0420     my $description = "DCC input channel MEM 1-2 SRP 3 TCC 4-7";
0421 
0422     my @channel_ids;
0423     my @logic_ids;
0424 
0425     #whole ECAL
0426     foreach my $DCC (601..654) {
0427         foreach my $chan (1..7) {
0428         my  $logic_id = sprintf "%03d%01d", $DCC,$chan;
0429         push @logic_ids, $logic_id;
0430         push @channel_ids, [$DCC,$chan];
0431         }
0432     }
0433     
0434     return {name => $name, idnames => $idnames,
0435         description => $description, logic_ids => \@logic_ids,
0436         channel_ids => \@channel_ids};
0437 }
0438 
0439 sub define_ECAL_SRP {
0440 
0441     my $name = "ECAL_SRP";
0442     my $idnames = ["SRP"];
0443     my $description = "SRP number in ECAL";
0444 
0445     my @channel_ids;
0446     my @logic_ids;
0447 
0448     #whole ECAL
0449     foreach my $SRP (1..54) {
0450         my  $logic_id = sprintf "2%03d", $SRP;
0451         push @logic_ids, $logic_id;
0452         push @channel_ids, [$SRP];
0453     }
0454     
0455     return {name => $name, idnames => $idnames,
0456         description => $description, logic_ids => \@logic_ids,
0457         channel_ids => \@channel_ids};
0458 }
0459 
0460 
0461 sub define_EE_crystal_hashed {
0462 
0463     my $name = "EE_crystal_hashed";
0464     my $idnames = ["hi"];
0465     my $description = "Crystals in Ecal Endcap by hashed index";
0466 
0467     my @channel_ids;
0468     my @logic_ids;
0469 
0470     #opening file
0471     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0472     #reading it into an array
0473     my @lines = <FILE>;
0474     #getting the first line out 
0475     shift @lines;
0476 
0477     #temp variables
0478     my $hi;
0479     
0480     foreach my $line (@lines) {
0481 
0482         my @channels = split (/ /, $line);
0483         $hi = $channels[3];
0484 
0485         #2 02X XXX XXX: avoid possible (but not happening anyways) overlapping with crystal_number when 1st free digit is 0
0486         my $logic_id = sprintf "202%07d", $hi;
0487         push @logic_ids, $logic_id;
0488         push @channel_ids, [$hi];
0489     }
0490     
0491     return {name => $name, idnames => $idnames,
0492         description => $description, logic_ids => \@logic_ids,
0493         channel_ids => \@channel_ids};
0494 }
0495 
0496 sub define_EE_readout_tower {
0497 
0498     my $name = "EE_readout_tower";
0499     my $idnames = ["DCC","readout_tower"];
0500     my $description = "Readout Towers in the ECAL Endcap";
0501 
0502     my @channel_ids;
0503     my @logic_ids;
0504 
0505     #opening file
0506     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0507     #reading it into an array
0508     my @lines = <FILE>;
0509     #getting the first line out 
0510     shift @lines;
0511 
0512     #temp variables
0513     my $DCC;
0514     my $readout_tower;
0515     my @ids;
0516 
0517     foreach my $line (@lines) {
0518 
0519             my @channels = split (/ /, $line);
0520 
0521             #id =DCC-600 ROTower;
0522             my $id = sprintf "%03d %02d", $channels[4],$channels[5];
0523 
0524             push @ids, $id;
0525     }
0526 
0527     #perlish - returns unique entries using internal references AND a hash
0528     #(actually, not an original idea)
0529 
0530     undef my %saw;
0531     my @unique = grep(!$saw{$_}++, @ids);
0532     
0533     foreach my $id (@unique) {
0534 
0535         my @channels = split (/ /, $id);
0536         $DCC = $channels[0] + 600;
0537         $readout_tower = $channels[1];
0538 
0539         my $logic_id = sprintf "21100%03d%02d", $DCC, $readout_tower;
0540         push @logic_ids, $logic_id;
0541         push @channel_ids, [$DCC, $readout_tower];
0542     }
0543     
0544     return {name => $name, idnames => $idnames,
0545         description => $description, logic_ids => \@logic_ids,
0546         channel_ids => \@channel_ids};
0547 }
0548 
0549 sub define_EE_trigger_tower {
0550 
0551     my $name = "EE_trigger_tower";
0552     my $idnames = ["TCC","trigger_tower"];
0553     my $description = "Trigger Towers in the ECAL Endcap";
0554 
0555     my @channel_ids;
0556     my @logic_ids;
0557 
0558     #opening file
0559     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0560     #reading it into an array
0561     my @lines = <FILE>;
0562     #getting the first line out 
0563     shift @lines;
0564 
0565     #temp variables
0566     my $TCC;
0567     my $trigger_tower;
0568     my @ids;
0569 
0570     foreach my $line (@lines) {
0571 
0572             my @channels = split (/ /, $line);
0573 
0574             #id =DCC.TTower;
0575             my $id = sprintf "%03d %02d", $channels[8],$channels[9];
0576 
0577             push @ids, $id;
0578     }
0579 
0580     #perlish - returns unique entries using internal references AND a hash
0581     #(actually, not an original idea)
0582 
0583     undef my %saw;
0584     my @unique = grep(!$saw{$_}++, @ids);
0585     
0586     foreach my $id (@unique) {
0587 
0588         my @channels = split (/ /, $id);
0589         $TCC = $channels[0];
0590         $trigger_tower = $channels[1];
0591 
0592         my $logic_id = sprintf "21000%03d%02d", $TCC, $trigger_tower;
0593         push @logic_ids, $logic_id;
0594         push @channel_ids, [$TCC, $trigger_tower];
0595     }
0596 
0597 # additional towers for Pascal inner sectors 
0598 
0599     my @TT_new1 = (21,23,25,27);
0600     foreach my $TCC_m (1..18) {
0601         foreach my $TT_m (@TT_new1) {
0602         my  $logic_id = sprintf "21000%03d%02d", $TCC_m, $TT_m;
0603         push @logic_ids, $logic_id;
0604         push @channel_ids, [$TCC_m, $TT_m];
0605         }
0606     }
0607 
0608     my @TT_new2 = (22,24,26,28);
0609     foreach my $TCC_m (91..108) {
0610         foreach my $TT_m (@TT_new2) {
0611         my  $logic_id = sprintf "21000%03d%02d", $TCC_m, $TT_m;
0612         push @logic_ids, $logic_id;
0613         push @channel_ids, [$TCC_m, $TT_m];
0614         }
0615     }
0616 
0617 
0618     
0619     return {name => $name, idnames => $idnames,
0620         description => $description, logic_ids => \@logic_ids,
0621         channel_ids => \@channel_ids};
0622 }
0623 
0624 sub define_EE_readout_strip {
0625 
0626     my $name = "EE_readout_strip";
0627     my $idnames = ["DCC","readout_tower","readout_strip"];
0628     my $description = "Readout Strips in the ECAL Endcap";
0629 
0630     my @channel_ids;
0631     my @logic_ids;
0632 
0633     #opening file
0634     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0635     #reading it into an array
0636     my @lines = <FILE>;
0637     #getting the first line out 
0638     shift @lines;
0639 
0640     #temp variables
0641     my $DCC;
0642     my $readout_tower;
0643     my $readout_strip;
0644     my @ids;
0645 
0646     foreach my $line (@lines) {
0647 
0648             my @channels = split (/ /, $line);
0649 
0650             #id =DCC TTower readout_strip;
0651             my $id = sprintf "%03d %02d %01d", $channels[4],$channels[5],$channels[6];
0652 
0653             push @ids, $id;
0654     }
0655 
0656     #perlish - returns unique entries using internal references AND a hash
0657     #(actually, not an original idea)
0658 
0659     undef my %saw;
0660     my @unique = grep(!$saw{$_}++, @ids);
0661     
0662     foreach my $id (@unique) {
0663 
0664         my @channels = split (/ /, $id);
0665         $DCC = $channels[0];
0666         $readout_tower = $channels[1];
0667         $readout_strip = $channels[2];
0668         my $logic_id = sprintf "2121%03d%02d%01d", $DCC, $readout_tower, $readout_strip;
0669         push @logic_ids, $logic_id;
0670         push @channel_ids, [$DCC, $readout_tower,$readout_strip];
0671     }
0672     
0673     return {name => $name, idnames => $idnames,
0674         description => $description, logic_ids => \@logic_ids,
0675         channel_ids => \@channel_ids};
0676 }
0677 
0678 sub define_EE_readout_strip_zseccu {
0679 
0680     my $name = "EE_readout_strip_zseccu";
0681     my $idnames = ["z_x_sector","ccu_id","readout_strip"];
0682     my $description = "Readout Strips in the EE by sector and ccu";
0683 
0684     my @channel_ids;
0685     my @logic_ids;
0686 
0687     #opening file
0688     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0689     #reading it into an array
0690     my @lines = <FILE>;
0691     #getting the first line out 
0692     shift @lines;
0693 
0694     #temp variables
0695     my $readout_tower;
0696     my $readout_strip;
0697     my @ids;
0698 
0699 
0700     foreach my $line (@lines) {
0701 
0702             my @channels = split (/ /, $line);
0703 
0704     
0705         my $DCC=$channels[4];
0706         my $sector=0;
0707         if($DCC<10) {
0708             if($DCC<=3){
0709             $sector=$DCC+6;
0710             } else {
0711             $sector=$DCC-3;
0712             } 
0713         }else {
0714             if($DCC<=48){
0715             $sector=$DCC-39;
0716             } else {
0717             $sector=$DCC-48;
0718             }
0719         } 
0720         my $ccu_id=$channels[13];
0721 
0722         my $sideIndex=$channels[2]+1;
0723         
0724 
0725             #id =DCC TTower readout_strip;
0726             my $id = sprintf "%01d %01d %02d %01d",$sideIndex, $sector, $ccu_id, $channels[6];
0727 
0728             push @ids, $id;
0729     }
0730 
0731     #perlish - returns unique entries using internal references AND a hash
0732     #(actually, not an original idea)
0733 
0734     undef my %saw;
0735     my @unique = grep(!$saw{$_}++, @ids);
0736     
0737     foreach my $id (@unique) {
0738 
0739         my @channels = split (/ /, $id);
0740         my $sideIndex = $channels[0];
0741         my $sector= $channels[1];
0742         my $ccu_id= $channels[2];
0743         my $strip= $channels[3];
0744         my $side= $sideIndex-1;
0745         my $logic_id = sprintf "213%01d%02d%02d%02d", $sideIndex, $sector, $ccu_id, $strip;
0746         push @logic_ids, $logic_id;
0747         $sector=$sector*$side;
0748         push @channel_ids, [$sector, $ccu_id, $strip];
0749     }
0750     
0751     return {name => $name, idnames => $idnames,
0752         description => $description, logic_ids => \@logic_ids,
0753         channel_ids => \@channel_ids};
0754 }
0755 
0756 sub define_EE_trigger_strip {
0757 
0758     my $name = "EE_trigger_strip";
0759     my $idnames = ["TCC","trigger_tower","trigger_strip"];
0760     my $description = "Trigger Strips in the ECAL Endcap";
0761     my @channel_ids;
0762     my @logic_ids;
0763 
0764     #opening file
0765     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0766     #reading it into an array
0767     my @lines = <FILE>;
0768     #getting the first line out 
0769     shift @lines;
0770 
0771     #temp variables
0772     my $TCC;
0773     my $trigger_tower;
0774     my $trigger_strip;
0775     my @ids;
0776 
0777     foreach my $line (@lines) {
0778 
0779             my @channels = split (/ /, $line);
0780 
0781             #id =TCC TriggerTower trigger_strip;
0782             my $id = sprintf "%03d %02d %01d", $channels[8],$channels[9],$channels[10];
0783 
0784             push @ids, $id;
0785     }
0786 
0787     #perlish - returns unique entries using internal references AND a hash
0788     #(actually, not an original idea)
0789 
0790     undef my %saw;
0791     my @unique = grep(!$saw{$_}++, @ids);
0792     
0793     foreach my $id (@unique) {
0794 
0795         my @channels = split (/ /, $id);
0796         $TCC = $channels[0];
0797         $trigger_tower = $channels[1];
0798         $trigger_strip = $channels[2];
0799         my $logic_id = sprintf "2140%03d%02d%01d", $TCC, $trigger_tower, $trigger_strip;
0800         push @logic_ids, $logic_id;
0801         push @channel_ids, [$TCC, $trigger_tower,$trigger_strip];
0802     }
0803     
0804     return {name => $name, idnames => $idnames,
0805         description => $description, logic_ids => \@logic_ids,
0806         channel_ids => \@channel_ids};
0807 }
0808 
0809 
0810 sub define_EE_crystal_trigger_strip {
0811 
0812     my $name = "EE_crystal_trigger_strip";
0813     #crystal_trigger_strip_id is a combination of other ids
0814     my $idnames = ["TCC","trigger_tower","crystal_trigger_strip_id"];
0815     my $description = "Numbering of crystal in Trigger Strip in the ECAL Endcap";
0816     my @channel_ids;
0817     my @logic_ids;
0818 
0819     #opening file
0820     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0821     #reading it into an array
0822     my @lines = <FILE>;
0823     #getting the first line out 
0824     shift @lines;
0825 
0826     #temp variables
0827     my $TCC;
0828     my $trigger_tower;
0829     my $trigger_strip;
0830     my $crystal_trigger_strip;
0831     my $crystal_trigger_strip_id;
0832 
0833     my @ids;
0834 
0835     foreach my $line (@lines) {
0836 
0837             my @channels = split (/ /, $line);
0838 
0839             $TCC = $channels[8];
0840         $trigger_tower= $channels[9];
0841         $trigger_strip= $channels[10];
0842         $crystal_trigger_strip=$channels[11];
0843 
0844         #crystal_trigger_strip_id=(strip-1)*5+channel_in_strip
0845         $crystal_trigger_strip_id=($trigger_strip-1)*5+$crystal_trigger_strip;
0846 
0847         my $logic_id = sprintf "203%03d%02d%02d", $TCC, $trigger_tower, $crystal_trigger_strip_id;
0848         push @logic_ids, $logic_id;
0849         push @channel_ids, [$TCC, $trigger_tower,$crystal_trigger_strip_id];
0850     }
0851     
0852     return {name => $name, idnames => $idnames,
0853         description => $description, logic_ids => \@logic_ids,
0854         channel_ids => \@channel_ids};
0855 }
0856 
0857 sub define_EE_crystal_readout_strip {
0858 
0859     my $name = "EE_crystal_readout_strip";
0860     #crystal_readout_strip_id is a combination of other ids
0861     my $idnames = ["DCC","readout_tower","crystal_readout_strip_id"];
0862     my $description = "Numbering of crystal in Readout Strip in the ECAL Endcap";
0863     my @channel_ids;
0864     my @logic_ids;
0865 
0866     #opening file
0867     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0868     #reading it into an array
0869     my @lines = <FILE>;
0870     #getting the first line out 
0871     shift @lines;
0872 
0873     #temp variables
0874     my $DCC;
0875     my $readout_tower;
0876     my $readout_strip;
0877     my $crystal_readout_strip;
0878     my $crystal_readout_strip_id;
0879 
0880     my @ids;
0881 
0882     foreach my $line (@lines) {
0883 
0884             my @channels = split (/ /, $line);
0885 
0886             $DCC = $channels[4] + 600;
0887         $readout_tower= $channels[5];
0888         $readout_strip= $channels[6];
0889         $crystal_readout_strip=$channels[7];
0890 
0891         #crystal_readout_strip_id=(strip-1)*5+channel_in_strip
0892         $crystal_readout_strip_id=($readout_strip-1)*5+$crystal_readout_strip;
0893 
0894         my $logic_id = sprintf "204%03d%02d%02d", $DCC, $readout_tower, $crystal_readout_strip_id;
0895         push @logic_ids, $logic_id;
0896         push @channel_ids, [$DCC, $readout_tower,$crystal_readout_strip_id];
0897     }
0898     
0899     return {name => $name, idnames => $idnames,
0900         description => $description, logic_ids => \@logic_ids,
0901         channel_ids => \@channel_ids};
0902 }
0903 
0904 sub define_EE_elec_crystal_number {
0905 
0906     my $name = "EE_elec_crystal_number";
0907     my $idnames = ["ele_number"];
0908     my $description = "Crystals in Ecal Endcap by electronic number";
0909 
0910     my @channel_ids;
0911     my @logic_ids;
0912 
0913     #opening file
0914     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
0915     #reading it into an array
0916     my @lines = <FILE>;
0917     #getting the first line out 
0918     shift @lines;
0919 
0920     #temp variables
0921     my $DCC;
0922     my $readout_tower;
0923     my $readout_strip;
0924     my $crystal_readout_strip;
0925     my $ele_number;
0926     
0927     foreach my $line (@lines) {
0928 
0929         my @channels = split (/ /, $line);
0930         $DCC = $channels[4];
0931         $readout_tower = $channels[5];
0932         $readout_strip = $channels[6];
0933         $crystal_readout_strip = $channels[7];
0934         
0935         #ele_number = 10 000 * DCC_id (<--FED-600) + readout_tower * 100 + (strip-1) * 5 + ch_in_sttip
0936         $ele_number = $DCC*10000 + $readout_tower*100 + ($readout_strip-1)*5 + $crystal_readout_strip;
0937 
0938         my $logic_id = sprintf "205%07d", $ele_number;
0939         push @logic_ids, $logic_id;
0940         push @channel_ids, [$ele_number];
0941     }
0942 
0943     close(FILE);
0944 
0945     return {name => $name, idnames => $idnames,
0946         description => $description, logic_ids => \@logic_ids,
0947         channel_ids => \@channel_ids};
0948 
0949 }
0950 
0951 
0952 sub define_EE_HV_channel {
0953 
0954     my $name = "EE_HV_channel";
0955     my $idnames = ["dee", "channel"];
0956     my $description = "HV channel by Dee and chan in the Endcaps";
0957 
0958     my @channel_ids;
0959     my @logic_ids;
0960 
0961 
0962     foreach my $D (1..4) {
0963         foreach my $chan (1..13) {
0964         my $logic_id = sprintf "20600%02d0%02d", $D, $chan;
0965             push @logic_ids, $logic_id;
0966             push @channel_ids, [$D, $chan];
0967         }
0968     }
0969 
0970     return {name => $name, idnames => $idnames,
0971         description => $description, logic_ids => \@logic_ids,
0972         channel_ids => \@channel_ids};
0973 
0974 }
0975 sub define_EE_HVA_channel {
0976 
0977     my $name = "EE_HVA_channel";
0978     my $idnames = ["dee", "channel"];
0979     my $description = "HV anode by Dee and chan in the Endcaps";
0980 
0981     my @channel_ids;
0982     my @logic_ids;
0983 
0984 
0985     foreach my $D (1..4) {
0986         foreach my $chan (1..2) {
0987         my $logic_id = sprintf "20604%02d0%02d", $D, $chan;
0988             push @logic_ids, $logic_id;
0989             push @channel_ids, [$D, $chan];
0990         }
0991     }
0992 
0993     return {name => $name, idnames => $idnames,
0994         description => $description, logic_ids => \@logic_ids,
0995         channel_ids => \@channel_ids};
0996 
0997 }
0998 
0999 sub define_EE_HVD_channel {
1000 
1001     my $name = "EE_HVD_channel";
1002     my $idnames = ["dee", "channel"];
1003     my $description = "HV dynode by Dee and chan in the Endcaps";
1004 
1005     my @channel_ids;
1006     my @logic_ids;
1007 
1008 
1009     foreach my $D (1..4) {
1010         foreach my $chan (1..2) {
1011         my $logic_id = sprintf "20605%02d0%02d", $D, $chan;
1012             push @logic_ids, $logic_id;
1013             push @channel_ids, [$D, $chan];
1014         }
1015     }
1016 
1017     return {name => $name, idnames => $idnames,
1018         description => $description, logic_ids => \@logic_ids,
1019         channel_ids => \@channel_ids};
1020 
1021 }
1022 
1023 sub define_EE_LMM {
1024 
1025     my $name = "EE_LMM";
1026     my $idnames = ["dee", "lmm"];
1027     my $description = "Endcap Laser Monitoring Modules";
1028 
1029     my @channel_ids;
1030     my @logic_ids;
1031 
1032 
1033     foreach my $D (1..4) {
1034         foreach my $chan (1..19) {
1035         my $logic_id = sprintf "20601%02d0%02d", $D, $chan;
1036             push @logic_ids, $logic_id;
1037             push @channel_ids, [$D, $chan];
1038         }
1039     }
1040 
1041     return {name => $name, idnames => $idnames,
1042         description => $description, logic_ids => \@logic_ids,
1043         channel_ids => \@channel_ids};
1044 
1045 }
1046 
1047 sub define_EE_LM_PN {
1048 
1049     my $name = "EE_LM_PN";
1050     my $idnames = ["dee", "PN"];
1051     my $description = "Endcap PN";
1052 
1053     my @channel_ids;
1054     my @logic_ids;
1055 
1056 
1057     foreach my $D (1..4) {
1058         foreach my $chan (0..9) {
1059         foreach my $side (1..2) {
1060             my $channel=$side*100+$chan;
1061             my $logic_id = sprintf "20602%02d%03d", $D,$channel;
1062             push @logic_ids, $logic_id;
1063             push @channel_ids, [$D, $channel];
1064         }
1065         }
1066     }
1067 
1068     return {name => $name, idnames => $idnames,
1069         description => $description, logic_ids => \@logic_ids,
1070         channel_ids => \@channel_ids};
1071 
1072 }
1073 
1074 sub define_ECAL_LMR {
1075 
1076     my $name = "ECAL_LMR";
1077     my $idnames = ["channel"];
1078     my $description = "EcalLaserMonitoringRegion EB-EB+EE+EE-";
1079 
1080     my @channel_ids;
1081     my @logic_ids;
1082 
1083 
1084         foreach my $chan (1..92) {
1085         my $logic_id="";
1086         if($chan<=72){
1087             $logic_id = sprintf "10603000%02d", $chan;
1088         } else {
1089             $logic_id = sprintf "20603000%02d", $chan;
1090         }       
1091         push @logic_ids, $logic_id;
1092         push @channel_ids, [$chan];
1093         }
1094         
1095     
1096     return {name => $name, idnames => $idnames,
1097         description => $description, logic_ids => \@logic_ids,
1098         channel_ids => \@channel_ids};
1099 
1100 }
1101 sub define_EE_PTM_H_amb {
1102   my $name = "EE_PTM_H_amb";
1103   my $idnames = ["Dee", "channel"];
1104   my $description = "ECAL Endcap Ambient humidity sensors";
1105 
1106   my @logic_ids;
1107   my @channel_ids;
1108   foreach my $Dee (1..4) {
1109     foreach my $channel (1..4) {
1110       my $logic_id = sprintf "211020%01d0%02d", $Dee, $channel;
1111       push @logic_ids, $logic_id;
1112       push @channel_ids, [ $Dee, $channel ];
1113     }
1114   }
1115 
1116   return {
1117       name => $name, idnames => $idnames,
1118       description => $description,
1119       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1120      };
1121 }
1122 
1123 sub define_EE_PTM_T_amb {
1124   my $name = "EE_PTM_T_amb";
1125   my $idnames = ["Dee", "channel"];
1126   my $description = "ECAL Endcap Ambient temperature sensors";
1127 
1128   my @logic_ids;
1129   my @channel_ids;
1130   foreach my $Dee (1..4) {
1131     foreach my $channel (1..24) {
1132       my $logic_id = sprintf "211010%01d0%02d", $Dee, $channel;
1133       push @logic_ids, $logic_id;
1134       push @channel_ids, [ $Dee, $channel ];
1135     }
1136   }
1137 
1138   return {
1139       name => $name, idnames => $idnames,
1140       description => $description,
1141       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1142      };
1143 }
1144 
1145 
1146 sub define_EE_LV_channel {
1147 
1148     my $name = "EE_LV_channel";
1149     my $idnames = ["dee", "channel"];
1150     my $description = "LV channel by Dee and chan in the Endcaps";
1151 
1152     my @channel_ids;
1153     my @logic_ids;
1154 
1155 
1156     foreach my $D (1..4) {
1157         foreach my $chan (10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88) {
1158         my $logic_id = sprintf "20700%02d0%02d", $D, $chan;
1159             push @logic_ids, $logic_id;
1160             push @channel_ids, [$D, $chan];
1161         }
1162     }
1163 
1164     return {name => $name, idnames => $idnames,
1165         description => $description, logic_ids => \@logic_ids,
1166         channel_ids => \@channel_ids};
1167 
1168 }
1169 
1170 
1171 sub define_EE_readout_tower_xyz {
1172 
1173     my $name = "EE_readout_tower_xyz";
1174     my $idnames = ["iz","ix","iy"];
1175     my $description = "Readout Towers in the Endcaps by zxy";
1176 
1177     my @channel_ids;
1178     my @logic_ids;
1179 
1180         #opening file
1181     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
1182         #reading it into an array
1183     my @lines = <FILE>;
1184         #getting the first line out
1185     shift @lines;
1186 
1187         #temp variables
1188     my $ix;
1189     my $iy;
1190     my $iz;
1191 
1192     my $DCC;
1193     my $readout_tower;
1194 
1195     my @ids;
1196     my @ixs;
1197     my @iys;
1198     my @izs;
1199 
1200     my $count=-1;
1201     foreach my $line (@lines) {
1202 
1203     my @channels = split (/ /, $line);
1204 
1205     $ix= $channels[0];
1206     $iy= $channels[1];
1207     $iz= $channels[2];
1208 
1209 
1210                 #id =DCC-600 ROTower;
1211     $DCC=$channels[4]+600;
1212     my $id = sprintf "20800%03d%02d", $DCC,$channels[5];
1213     my $ifound=-1;
1214     for my $i (0..$count) {
1215         if($id== $ids[$i]) {
1216         $ifound=$i;
1217         if( $ix<= $ixs[$i] && $iy<= $iys[$i] ) {
1218             $ixs[$i]=$ix;
1219             $iys[$i]=$iy;
1220         }
1221 
1222         last;
1223         }
1224     }
1225     
1226     if($ifound==-1) {
1227         $count++;
1228         push @ids, $id;
1229         push @ixs, $ix;
1230         push @iys, $iy;
1231         push @izs, $iz;
1232     }
1233 
1234     }
1235 
1236     for my $i (0..$count) {
1237     push @logic_ids, $ids[$i];
1238     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
1239     }
1240 
1241     return {name => $name, idnames => $idnames,
1242                 description => $description, logic_ids => \@logic_ids,
1243         channel_ids => \@channel_ids};
1244 }
1245 
1246 sub define_EE_readout_tower_zseccu {
1247 
1248     my $name = "EE_readout_tower_zseccu";
1249     my $idnames = ["iz","sector","ccuid"];
1250     my $description = "Readout Towers in the EE by z sector ccuid";
1251 
1252     my @channel_ids;
1253     my @logic_ids;
1254 
1255         #opening file
1256     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
1257         #reading it into an array
1258     my @lines = <FILE>;
1259         #getting the first line out
1260     shift @lines;
1261 
1262         #temp variables
1263     my $ix;
1264     my $iy;
1265     my $iz;
1266 
1267 
1268     my @ids;
1269     my @ixs;
1270     my @iys;
1271     my @izs;
1272 
1273     my $count=-1;
1274     foreach my $line (@lines) {
1275 
1276     my @channels = split (/ /, $line);
1277 
1278     $ix= $channels[0];
1279     $iy= $channels[1];
1280     $iz= $channels[2];
1281 
1282     my $sideIndex = $channels[2] + 1;
1283 
1284         
1285     my $DCC=$channels[4];
1286     my $sector=0;
1287     if($DCC<10) {
1288         if($DCC<=3){
1289         $sector=$DCC+6;
1290         } else {
1291         $sector=$DCC-3;
1292         } 
1293     }else {
1294         if($DCC<=48){
1295         $sector=$DCC-39;
1296         } else {
1297         $sector=$DCC-48;
1298         }
1299     } 
1300     my $ccu_id=$channels[13];
1301 
1302 
1303     my $id = sprintf "209%02d%03d%02d", $sideIndex, $sector,$ccu_id;
1304     my $ifound=-1;
1305     for my $i (0..$count) {
1306         if($id== $ids[$i]) {
1307         $ifound=$i;
1308         last;
1309         }
1310     }
1311     
1312     if($ifound==-1) {
1313         $count++;
1314         push @ids, $id;
1315         push @ixs, $sector;
1316         push @iys, $ccu_id;
1317         push @izs, $iz;
1318     }
1319 
1320     }
1321 
1322     for my $i (0..$count) {
1323     push @logic_ids, $ids[$i];
1324     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
1325     }
1326 
1327     return {name => $name, idnames => $idnames,
1328                 description => $description, logic_ids => \@logic_ids,
1329         channel_ids => \@channel_ids};
1330 }
1331 
1332 
1333 
1334 sub define_ECAL {
1335   my $name = "ECAL";
1336   my $idnames = [];
1337   my $description = "The entire ECAL subdetector";
1338   
1339   my @logic_ids = (1);
1340   my @channel_ids = ([]);
1341 
1342   return {name => $name, idnames => $idnames,
1343       description => $description, logic_ids => \@logic_ids,
1344      channel_ids => \@channel_ids};
1345 }
1346 
1347 sub define_EB {
1348   my $name = "EB";
1349   my $idnames = [];
1350   my $description = "The entire ECAL Barrel";  
1351   my @logic_ids = (1000000000);
1352   my @channel_ids = ([]);
1353 
1354   return {name => $name, idnames => $idnames,
1355       description => $description, logic_ids => \@logic_ids,
1356      channel_ids => \@channel_ids};
1357 }
1358 
1359 sub define_EE {
1360   my $name = "EE";
1361   my $idnames = [];
1362   my $description = "The entire ECAL Endcap";  
1363   my @logic_ids = (2000000000);
1364   my @channel_ids = ([]);
1365 
1366   return {name => $name, idnames => $idnames,
1367       description => $description, logic_ids => \@logic_ids,
1368      channel_ids => \@channel_ids};
1369 }
1370 
1371 sub define_EB_crystal_number {
1372   my $name = "EB_crystal_number";
1373   my $idnames = ["SM", "crystal_number"];
1374   my $description = "Crystals in ECAL barrel super-modules by number";
1375 
1376   my @logic_ids;
1377   my @channel_ids;
1378   foreach my $SM (0..36) {
1379     foreach my $xtal (1..1700) {
1380       my $logic_id = sprintf "1011%02d%04d", $SM, $xtal;
1381       push @logic_ids, $logic_id;
1382       push @channel_ids, [ $SM, $xtal ];
1383     }
1384   }
1385 
1386   return {name => $name, idnames => $idnames,
1387       description => $description, logic_ids => \@logic_ids, 
1388       channel_ids => \@channel_ids};
1389 }
1390 
1391 
1392 
1393 sub define_EB_crystal_index {
1394   my $name = "EB_crystal_index";
1395   my $idnames = ["SM", "i", "j"];
1396   my $description = "Crystals in ECAL barrel super-modules by i,j index";
1397 
1398   my @logic_ids;
1399   my @channel_ids;
1400   foreach my $SM (0..36) {
1401     foreach my $i (0..84) {
1402       foreach my $j (0..19) {
1403     my $logic_id = sprintf "1012%02d%02d%02d", $SM, $i, $j;
1404     push @logic_ids, $logic_id;
1405     push @channel_ids, [ $SM, $i, $j ];
1406       }
1407     }
1408   }
1409 
1410   return {name => $name, idnames => $idnames,
1411       description => $description, logic_ids => \@logic_ids,
1412      channel_ids => \@channel_ids};
1413 }
1414 
1415 sub define_EB_elec_crystal_number {
1416   my $name = "EB_elec_crystal_number";
1417   my $idnames = ["SM", "channel"];
1418   my $description = "ECAL Barrel crystals, electronics numbering scheme";
1419 
1420   my @logic_ids;
1421   my @channel_ids;
1422   foreach my $SM (0..36) {
1423     foreach my $xtal (0..1699) {
1424       my $logic_id = sprintf "1013%02d%04d", $SM, $xtal;
1425       push @logic_ids, $logic_id;
1426       push @channel_ids, [ $SM, $xtal ];
1427     }
1428   }
1429 
1430   return {name => $name, idnames => $idnames,
1431       description => $description,
1432       logic_ids => \@logic_ids, channel_ids => \@channel_ids};
1433 }
1434 
1435 
1436 sub define_EB_fe_crystal_number {
1437   my $name = "EB_fe_crystal_number";
1438   my $idnames = ["SM", "TT", "channel"];
1439   my $description = "ECAL Barrel crystals, front-end configuration numbering scheme";
1440 
1441   my @logic_ids;
1442   my @channel_ids;
1443   foreach my $SM (0..36) {
1444     foreach my $TT (1..68) {
1445       foreach my $xtal (0..24) {
1446     my $logic_id = sprintf "1014%02d%02d%02d", $SM, $TT, $xtal;
1447     push @logic_ids, $logic_id;
1448     push @channel_ids, [ $SM, $TT, $xtal ];
1449       }
1450     }
1451   }
1452 
1453   return {name => $name, idnames => $idnames,
1454       description => $description,
1455       logic_ids => \@logic_ids, channel_ids => \@channel_ids};
1456 }
1457 
1458 sub define_ECAL_crystal_number_fedccuxt {
1459   my $name = "ECAL_crystal_number_fedccuxt";
1460   my $idnames = ["FED", "CCU", "channel"];
1461   my $description = "ECAL Barrel crystals by fed ccu crystal(0-24)";
1462 
1463   my @logic_ids;
1464   my @channel_ids;
1465   foreach my $FED (610..645) {
1466     foreach my $TT (1..68) {
1467       foreach my $xtal (0..24) {
1468     my $logic_id = sprintf "120%03d%02d%02d", $FED, $TT, $xtal;
1469     push @logic_ids, $logic_id;
1470     push @channel_ids, [ $FED, $TT, $xtal ];
1471       }
1472     }
1473   }
1474 
1475 # endcap part
1476   #opening file
1477   open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
1478   #reading it into an array
1479   my @lines = <FILE>;
1480   #getting the first line out   
1481   shift @lines;
1482   
1483   #temp variables
1484   my $DCC;
1485   my $readout_tower;
1486   my $readout_strip;
1487   my $crystal_readout_strip;
1488   my $crystal_readout_strip_id;
1489   
1490   foreach my $line (@lines) {
1491 
1492       my @channels = split (/ /, $line);
1493       
1494       $DCC = $channels[4] + 600;
1495       $readout_tower= $channels[5];
1496       $readout_strip= $channels[6];
1497       $crystal_readout_strip=$channels[7];
1498       
1499       
1500       $crystal_readout_strip_id=($readout_strip-1)*5+$crystal_readout_strip-1;
1501       
1502       my $logic_id = sprintf "120%03d%02d%02d", $DCC, $readout_tower, $crystal_readout_strip_id;
1503       push @logic_ids, $logic_id;
1504       push @channel_ids, [$DCC, $readout_tower,$crystal_readout_strip_id];
1505   }
1506 
1507 
1508   return {name => $name, idnames => $idnames,
1509       description => $description,
1510       logic_ids => \@logic_ids, channel_ids => \@channel_ids};
1511 }
1512 
1513 
1514 sub define_EB_trigger_tower {
1515   my $name = "EB_trigger_tower";
1516   my $idnames = ["SM", "trigger_tower"];
1517   my $description = "Trigger towers in the ECAL barrel super-modules";
1518 
1519   my @logic_ids;
1520   my @channel_ids;
1521   foreach my $SM (0..36) {
1522     foreach my $tt (1..68) {
1523       my $logic_id = sprintf "1021%02d00%02d", $SM, $tt;
1524       push @logic_ids, $logic_id;
1525       push @channel_ids, [ $SM, $tt ];
1526     }
1527   }
1528 
1529   return {
1530       name => $name, idnames => $idnames,
1531       description => $description,
1532       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1533      };
1534 }
1535 
1536 sub define_ECAL_trigger_tower {
1537   my $name = "ECAL_trigger_tower";
1538   my $idnames = ["TCC", "trigger_tower"];
1539   my $description = "Trigger towers in the ECAL barrel by tcc and tt";
1540 
1541   my @logic_ids;
1542   my @channel_ids;
1543   foreach my $SM (37..72) {
1544     foreach my $tt (1..68) {
1545       my $logic_id = sprintf "122%03d00%02d", $SM, $tt;
1546       push @logic_ids, $logic_id;
1547       push @channel_ids, [ $SM, $tt ];
1548     }
1549   }
1550 
1551 
1552 
1553   return {
1554       name => $name, idnames => $idnames,
1555       description => $description,
1556       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1557      };
1558 }
1559 sub define_EB_readout_tower {
1560   my $name = "EB_readout_tower";
1561   my $idnames = ["FED", "tower"];
1562   my $description = "Readout towers in the ECAL barrel by fed and tower";
1563 
1564   my @logic_ids;
1565   my @channel_ids;
1566   foreach my $SM (610..645) {
1567     foreach my $tt (1..68) {
1568       my $logic_id = sprintf "123%03d00%02d", $SM, $tt;
1569       push @logic_ids, $logic_id;
1570       push @channel_ids, [ $SM, $tt ];
1571     }
1572   }
1573 
1574 
1575 
1576   return {
1577       name => $name, idnames => $idnames,
1578       description => $description,
1579       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1580      };
1581 }
1582 
1583 sub define_EB_module {
1584   my $name = "EB_module";
1585   my $idnames = ["SM", "M"];
1586   my $description = "ECAL barrel modules";
1587 
1588   my @logic_ids;
1589   my @channel_ids;
1590   foreach my $SM (0..36) {
1591     foreach my $M (1..4) {
1592       my $logic_id = sprintf "1031%02d00%02d", $SM, $M;
1593       push @logic_ids, $logic_id;
1594       push @channel_ids, [ $SM, $M ];
1595     }
1596   }
1597 
1598   return {
1599       name => $name, idnames => $idnames,
1600       description => $description,
1601       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1602      };
1603 }
1604 
1605 sub define_EB_supermodule {
1606   my $name = "EB_supermodule";
1607   my $idnames = ["SM"];
1608   my $description = "ECAL barrel Super-Modules";
1609 
1610   my @logic_ids;
1611   my @channel_ids;
1612   foreach my $SM (0..36) {
1613     my $logic_id = sprintf "1041%02d00%02d", $SM, $SM;
1614     push @logic_ids, $logic_id;
1615     push @channel_ids, [ $SM ];
1616   }
1617 
1618   return {
1619       name => $name, idnames => $idnames,
1620       description => $description,
1621       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1622      };
1623 }
1624 
1625 sub define_EB_HV_channel {
1626   my $name = "EB_HV_channel";
1627   my $idnames = ["SM", "channel"];
1628   my $description = "ECAL Barrel High Voltage Channel";
1629 
1630   my @logic_ids;
1631   my @channel_ids;
1632   foreach my $SM (0..36) {
1633     foreach my $channel (1..34) {
1634       my $logic_id = sprintf "1051%02d00%02d", $SM, $channel;
1635       push @logic_ids, $logic_id;
1636       push @channel_ids, [ $SM, $channel ];
1637     }
1638   }
1639   return {
1640       name => $name, idnames => $idnames,
1641       description => $description,
1642       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1643      };
1644 }
1645 
1646 sub define_EB_HV_board {
1647   my $name = "EB_HV_board";
1648   my $idnames = ["SM", "board#"];
1649   my $description = "ECAL Barrel High Voltage Board Number";
1650 
1651   my @logic_ids;
1652   my @channel_ids;
1653   foreach my $SM (0..36) {
1654     foreach my $board (0,2,4,6) {
1655       my $logic_id = sprintf "1061%02d00%02d", $SM, $board;
1656       push @logic_ids, $logic_id;
1657       push @channel_ids, [ $SM, $board ];
1658     }
1659   }
1660 
1661   return {
1662       name => $name, idnames => $idnames,
1663       description => $description,
1664       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1665      };
1666 }
1667 
1668 sub define_EB_LV_channel {
1669   my $name = "EB_LV_channel";
1670   my $idnames = ["SM", "channel"];
1671   my $description = "ECAL Barrel Low Voltage Channel";
1672 
1673   my @logic_ids;
1674   my @channel_ids;
1675   foreach my $SM (0..36) {
1676     foreach my $channel (1..17) {
1677       my $logic_id = sprintf "1071%02d00%02d", $SM, $channel;
1678       push @logic_ids, $logic_id;
1679       push @channel_ids, [ $SM, $channel ];
1680     }
1681   }
1682 
1683   return {
1684       name => $name, idnames => $idnames,
1685       description => $description,
1686       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1687      };
1688 }
1689 
1690 sub define_EB_ESS_temp {
1691   my $name = "EB_ESS_temp";
1692   my $idnames = ["SM", "channel"];
1693   my $description = "ECAL Barrel ESS temperature channel";
1694 
1695   my @logic_ids;
1696   my @channel_ids;
1697   foreach my $SM (0..36) {
1698     foreach my $channel (0..7) {
1699       my $logic_id = sprintf "1081%02d00%02d", $SM, $channel;
1700       push @logic_ids, $logic_id;
1701       push @channel_ids, [ $SM, $channel ];
1702     }
1703   }
1704 
1705   return {
1706       name => $name, idnames => $idnames,
1707       description => $description,
1708       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1709      };
1710 }
1711 
1712 sub define_EB_PTM_H_amb {
1713   my $name = "EB_PTM_H_amb";
1714   my $idnames = ["SM", "channel"];
1715   my $description = "ECAL Barrel Ambient humidity sensors";
1716 
1717   my @logic_ids;
1718   my @channel_ids;
1719   foreach my $SM (0..36) {
1720     foreach my $channel (1..4) {
1721       my $logic_id = sprintf "1091%02d00%02d", $SM, $channel;
1722       push @logic_ids, $logic_id;
1723       push @channel_ids, [ $SM, $channel ];
1724     }
1725   }
1726 
1727   return {
1728       name => $name, idnames => $idnames,
1729       description => $description,
1730       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1731      };
1732 }
1733 
1734 sub define_EB_PTM_T_amb {
1735   my $name = "EB_PTM_T_amb";
1736   my $idnames = ["SM", "channel"];
1737   my $description = "ECAL Barrel Ambient temperature sensors";
1738 
1739   my @logic_ids;
1740   my @channel_ids;
1741   foreach my $SM (0..36) {
1742     foreach my $channel (1..10) {
1743       my $logic_id = sprintf "1101%02d00%02d", $SM, $channel;
1744       push @logic_ids, $logic_id;
1745       push @channel_ids, [ $SM, $channel ];
1746     }
1747   }
1748 
1749   return {
1750       name => $name, idnames => $idnames,
1751       description => $description,
1752       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1753      };
1754 }
1755 
1756 sub define_EB_token_ring {
1757   my $name = "EB_token_ring";
1758   my $idnames = ["SM", "channel"];
1759   my $description = "ECAL Barrel Token Ring";
1760 
1761   my @logic_ids;
1762   my @channel_ids;
1763   foreach my $SM (0..36) {
1764     foreach my $channel (1..8) {
1765       my $logic_id = sprintf "1111%02d00%02d", $SM, $channel;
1766       push @logic_ids, $logic_id;
1767       push @channel_ids, [ $SM, $channel ];
1768     }
1769   }
1770 
1771   return {
1772       name => $name, idnames => $idnames,
1773       description => $description,
1774       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1775      };
1776 }
1777 
1778 
1779 
1780 sub define_EB_LM_side {
1781   my $name = "EB_LM_side";
1782   my $idnames = ["SM", "side"];
1783   my $description = "ECAL Barrel Laser Regions";
1784 
1785   my @logic_ids;
1786   my @channel_ids;
1787   foreach my $SM (0..36) {
1788     foreach my $channel (0..1) {
1789       my $logic_id = sprintf "1132%02d00%02d", $SM, $channel;
1790       push @logic_ids, $logic_id;
1791       push @channel_ids, [ $SM, $channel ];
1792     }
1793   }
1794 
1795   return {
1796       name => $name, idnames => $idnames,
1797       description => $description,
1798       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1799      };
1800 }
1801 
1802 sub define_EB_LM_channel {
1803   my $name = "EB_LM_channel";
1804   my $idnames = ["SM", "channel"];
1805   my $description = "ECAL Barrel Laser Monitoring";
1806 
1807   my @logic_ids;
1808   my @channel_ids;
1809   foreach my $SM (0..36) {
1810     foreach my $channel (1..9) {
1811       my $logic_id = sprintf "1121%02d00%02d", $SM, $channel;
1812       push @logic_ids, $logic_id;
1813       push @channel_ids, [ $SM, $channel ];
1814     }
1815   }
1816 
1817   return {
1818       name => $name, idnames => $idnames,
1819       description => $description,
1820       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1821      };
1822 }
1823 
1824 sub define_EB_LM_PN {
1825   my $name = "EB_LM_PN";
1826   my $idnames = ["SM", "channel"];
1827   my $description = "ECAL Barrel Laser Monitoring PN numbers";
1828 
1829   my @logic_ids;
1830   my @channel_ids;
1831   foreach my $SM (0..36) {
1832     foreach my $channel (0..9) {
1833       my $logic_id = sprintf "1131%02d00%02d", $SM, $channel;
1834       push @logic_ids, $logic_id;
1835       push @channel_ids, [ $SM, $channel ];
1836     }
1837   }
1838 
1839   return {
1840       name => $name, idnames => $idnames,
1841       description => $description,
1842       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1843      };
1844 }
1845 
1846 sub define_EB_T_capsule {
1847   my $name = "EB_T_capsule";
1848   my $idnames = ["SM", "channel"];
1849   my $description = "Ecal Barrel Capsule temperature sensors";
1850 
1851   my @logic_ids;
1852   my @channel_ids;
1853   foreach my $SM (0..36) {
1854     foreach my $channel (1..170) {
1855       my $logic_id = sprintf "1141%02d0%03d", $SM, $channel;
1856       push @logic_ids, $logic_id;
1857       push @channel_ids, [ $SM, $channel ];
1858     }
1859   }
1860 
1861   return {
1862       name => $name, idnames => $idnames,
1863       description => $description,
1864       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1865      }
1866 }
1867 
1868 sub define_EB_VFE {
1869   my $name = "EB_VFE";
1870   my $idnames = ["SM", "TT", "VFE#"];
1871   my $description = "Ecal Barrel Very Front End cards";
1872 
1873   my @logic_ids;
1874   my @channel_ids;
1875   foreach my $SM (0..36) {
1876     foreach my $TT (1..68) {
1877       foreach my $VFE (1..5) {
1878     my $logic_id = sprintf "1151%02d%02d%02d", $SM, $TT, $VFE;
1879     push @logic_ids, $logic_id;
1880     push @channel_ids, [ $SM, $TT, $VFE ];
1881       }
1882     }
1883   }
1884 
1885   return {
1886       name => $name, idnames => $idnames,
1887       description => $description,
1888       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1889      }
1890 }
1891 
1892 sub define_ECAL_readout_strip {
1893   my $name = "ECAL_readout_strip";
1894   my $idnames = ["FED", "CCU", "strip"];
1895   my $description = "Ecal readout strip by fed ccu strip";
1896 
1897   my @logic_ids;
1898   my @channel_ids;
1899   foreach my $SM (610..645) {
1900     foreach my $TT (1..68) {
1901       foreach my $VFE (1..5) {
1902     my $logic_id = sprintf "121%03d%02d%02d", $SM, $TT, $VFE;
1903     push @logic_ids, $logic_id;
1904     push @channel_ids, [ $SM, $TT, $VFE ];
1905       }
1906     }
1907   }
1908 
1909 
1910 # endcap strips 
1911 
1912     #opening file
1913     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
1914     #reading it into an array
1915     my @lines = <FILE>;
1916     #getting the first line out 
1917     shift @lines;
1918 
1919     #temp variables
1920     my $DCC;
1921     my $readout_tower;
1922     my $readout_strip;
1923     my @ids;
1924 
1925     foreach my $line (@lines) {
1926 
1927             my @channels = split (/ /, $line);
1928 
1929             #id =DCC TTower readout_strip;
1930         my $FED=$channels[4]+600;
1931             my $id = sprintf "%03d %02d %02d", $FED,$channels[5],$channels[6];
1932 
1933             push @ids, $id;
1934     }
1935 
1936     #perlish - returns unique entries using internal references AND a hash
1937     #(actually, not an original idea)
1938 
1939     undef my %saw;
1940     my @unique = grep(!$saw{$_}++, @ids);
1941     
1942     foreach my $id (@unique) {
1943 
1944         my @channels = split (/ /, $id);
1945         $DCC = $channels[0];
1946         $readout_tower = $channels[1];
1947         $readout_strip = $channels[2];
1948         my $logic_id = sprintf "121%03d%02d%02d", $DCC, $readout_tower, $readout_strip;
1949         push @logic_ids, $logic_id;
1950         push @channel_ids, [$DCC, $readout_tower,$readout_strip];
1951     }
1952 
1953   return {
1954       name => $name, idnames => $idnames,
1955       description => $description,
1956       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1957      }
1958 }
1959 
1960 sub define_EB_LVRB_DCU {
1961   my $name = "EB_LVRB_DCU";
1962   my $idnames = ["SM", "TT", "LVRB_DCU#"];
1963   my $description = "Ecal Barrel DCUs on the LVRB";
1964 
1965   my @logic_ids;
1966   my @channel_ids;
1967   foreach my $SM (0..36) {
1968     foreach my $TT (1..68) {
1969       foreach my $DCU (1..3) {
1970     my $logic_id = sprintf "1161%02d%02d%02d", $SM, $TT, $DCU;
1971     push @logic_ids, $logic_id;
1972     push @channel_ids, [ $SM, $TT, $DCU ];
1973       }
1974     }
1975   }
1976 
1977   return {
1978       name => $name, idnames => $idnames,
1979       description => $description,
1980       logic_ids => \@logic_ids, channel_ids => \@channel_ids
1981      }
1982 }
1983 
1984 sub define_EB_LVRB_T_sensor {
1985   my $name = "EB_LVRB_T_sensor";
1986   my $idnames = ["SM", "TT", "T_sens#"];
1987   my $description = "Ecal Barrel thermisters on the LVRB";
1988 
1989   my @logic_ids;
1990   my @channel_ids;
1991   foreach my $SM (0..36) {
1992     foreach my $TT (1..68) {
1993       foreach my $sens (1..3) {
1994     my $logic_id = sprintf "1171%02d%02d%02d", $SM, $TT, $sens;
1995     push @logic_ids, $logic_id;
1996     push @channel_ids, [ $SM, $TT, $sens ];
1997       }
1998     }
1999   }
2000 
2001   return {
2002       name => $name, idnames => $idnames,
2003       description => $description,
2004       logic_ids => \@logic_ids, channel_ids => \@channel_ids
2005      }
2006 }
2007 
2008 sub define_EB_mem_TT {
2009   my $name = "EB_mem_TT";
2010   my $idnames = ["SM", "TT"];
2011   my $description = "Supermodule mem box pseudo trigger tower";
2012 
2013   my @logic_ids;
2014   my @channel_ids;
2015   foreach my $SM (0..36) {
2016     foreach my $TT (69..70) {
2017       my $logic_id = sprintf "1181%02d00%02d", $SM, $TT;
2018       push @logic_ids, $logic_id;
2019       push @channel_ids, [ $SM, $TT ];
2020     }
2021   }
2022 
2023   return {
2024       name => $name, idnames => $idnames,
2025       description => $description,
2026       logic_ids => \@logic_ids, channel_ids => \@channel_ids
2027      }
2028 }
2029 
2030 sub define_EB_mem_channel {
2031   my $name = "EB_mem_channel";
2032   my $idnames = ["SM", "channel"];
2033   my $description = "Supermodule mem box pseudo channel";
2034 
2035   my @logic_ids;
2036   my @channel_ids;
2037   foreach my $SM (0..36) {
2038     foreach my $ch (1..50) {
2039       my $logic_id = sprintf "1191%02d00%02d", $SM, $ch;
2040       push @logic_ids, $logic_id;
2041       push @channel_ids, [ $SM, $ch ];
2042     }
2043   }
2044 
2045   return {
2046       name => $name, idnames => $idnames,
2047       description => $description,
2048       logic_ids => \@logic_ids, channel_ids => \@channel_ids
2049      }
2050 }
2051 
2052 
2053 
2054 ###
2055 ###   Cross-Channel mappings
2056 ###
2057 
2058 sub define_EE_crystal_number_to_EE_trigger_tower {
2059 
2060     my $name = "EE_crystal_number";
2061     my $maps_to = "EE_trigger_tower";
2062 
2063     my @channel_ids;
2064     my @logic_ids;
2065 
2066     #opening file
2067     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2068     #reading it into an array
2069     my @lines = <FILE>;
2070     #getting the first line out 
2071     shift @lines;
2072 
2073     #temp variables
2074     my $TCC;
2075     my $trigger_tower;
2076     my $ix;
2077     my $iy;
2078     my $side;
2079     my $logic_id;
2080 
2081 
2082     foreach my $line (@lines) {
2083 
2084             my @channels = split (/ /, $line);
2085 
2086         $ix = $channels[0];
2087         $iy = $channels[1];
2088         $side = $channels[2];
2089 
2090         $TCC = $channels[8];
2091         $trigger_tower = $channels[9];
2092 
2093         #xtal number ids: side, ix, iy
2094         push @channel_ids, [$side, $ix, $iy];
2095 
2096         #trigger tower logic id: 20000XXXYY XXX=TCC, YY=trigger tower
2097         $logic_id = sprintf "21000%03d%02d", $TCC, $trigger_tower;
2098         push @logic_ids, $logic_id;
2099 
2100     }
2101 
2102     return { 
2103         name => $name, maps_to => $maps_to, 
2104         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2105     };
2106 
2107 }
2108 
2109 
2110 
2111 sub define_EE_trigger_tower_to_EE_crystal_number {
2112 
2113     my $name = "EE_trigger_tower";  
2114     my $maps_to = "EE_crystal_number";
2115 
2116     my @channel_ids;
2117     my @logic_ids;
2118 
2119     #opening file
2120     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2121     #reading it into an array
2122     my @lines = <FILE>;
2123     #getting the first line out 
2124     shift @lines;
2125 
2126     #temp variables
2127     my $TCC;
2128     my $trigger_tower;
2129     my $ix;
2130     my $iy;
2131     my $sideIndex;
2132     my $logic_id;
2133 
2134 
2135     foreach my $line (@lines) {
2136 
2137             my @channels = split (/ /, $line);
2138 
2139         $ix = $channels[0];
2140         $iy = $channels[1];
2141         $sideIndex = $channels[2] + 1;
2142 
2143         $TCC = $channels[8];
2144         $trigger_tower = $channels[9];
2145 
2146         #trigger tower ids: TCC, trigger tower
2147         push @channel_ids, [$TCC, $trigger_tower];
2148 
2149         #xtal number logic id: 201XYYYZZZ XX=TCC, YY=trigger tower
2150         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
2151         push @logic_ids, $logic_id;
2152 
2153     }
2154 
2155     return { 
2156         name => $name, maps_to => $maps_to, 
2157         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2158     };
2159 
2160 }
2161 
2162 
2163 sub define_EE_trigger_tower_to_EE_offline_towerid {
2164 
2165     my $name = "EE_trigger_tower";  
2166     my $maps_to = "EE_offline_towerid";
2167 
2168     my @channel_ids;
2169     my @logic_ids;
2170 
2171     #opening file
2172     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2173     #reading it into an array
2174     my @lines = <FILE>;
2175     #getting the first line out 
2176     shift @lines;
2177 
2178     #temp variables
2179     my $TCC;
2180     my $offline_id;
2181     my $trigger_tower;
2182     my $ix;
2183     my $iy;
2184     my $sideIndex;
2185     my $logic_id;
2186     my @ids;
2187 
2188 
2189     foreach my $line (@lines) {
2190 
2191             my @channels = split (/ /, $line);
2192 
2193         $ix = $channels[0];
2194         $iy = $channels[1];
2195         $sideIndex = $channels[2] + 1;
2196 
2197         $TCC = $channels[8];
2198         $trigger_tower = $channels[9];
2199         $offline_id = $channels[18];
2200 
2201             my $id = sprintf "%03d %02d %9d", $channels[8],$channels[9], $channels[18];
2202 
2203             push @ids, $id;
2204     }
2205 
2206     #perlish - returns unique entries using internal references AND a hash
2207     #(actually, not an original idea)
2208 
2209     undef my %saw;
2210     my @unique = grep(!$saw{$_}++, @ids);
2211     
2212     foreach my $id (@unique) {
2213 
2214         my @channels = split (/ /, $id);
2215         $TCC           = $channels[0];
2216         $trigger_tower = $channels[1];
2217         $offline_id    = $channels[2];
2218 
2219         my $logic_id = sprintf "%9d", $offline_id;
2220         push @logic_ids, $logic_id;
2221 
2222         #trigger tower ids: TCC, trigger tower
2223         push @channel_ids, [$TCC, $trigger_tower];
2224 
2225     }
2226 
2227     return { 
2228         name => $name, maps_to => $maps_to, 
2229         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2230     };
2231 
2232 }
2233 sub define_EE_trigger_tower_to_EE_offline_towerid_more {
2234 
2235     my $name = "EE_trigger_tower";  
2236     my $maps_to = "EE_offline_towerid";
2237 
2238     my @channel_ids;
2239     my @logic_ids;
2240 
2241     #opening file
2242     open (FILE , "additionaltowers.txt") || die ("could not open EE numbering file");
2243     #reading it into an array
2244     my @lines = <FILE>;
2245     #getting the first line out 
2246     shift @lines;
2247 
2248     #temp variables
2249     my $TCC;
2250     my $offline_id;
2251     my $trigger_tower;
2252     my $ix;
2253     my $iy;
2254     my $sideIndex;
2255     my $logic_id;
2256     my @ids;
2257 
2258 
2259     foreach my $line (@lines) {
2260 
2261             my @channels = split (/ /, $line);
2262 
2263 
2264         $TCC           = $channels[0];
2265         $trigger_tower = $channels[1];
2266         $offline_id    = $channels[2];
2267 
2268         my $logic_id = sprintf "%9d", $offline_id;
2269         push @logic_ids, $logic_id;
2270 
2271         #trigger tower ids: TCC, trigger tower
2272         push @channel_ids, [$TCC, $trigger_tower];
2273 
2274 
2275     }
2276 
2277     #perlish - returns unique entries using internal references AND a hash
2278     #(actually, not an original idea)
2279     
2280     return { 
2281         name => $name, maps_to => $maps_to, 
2282         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2283     };
2284 
2285 }
2286 
2287 sub define_EE_trigger_strip_to_EE_offline_stripid {
2288 
2289     my $name = "EE_trigger_strip";  
2290     my $maps_to = "EE_offline_stripid";
2291 
2292     my @channel_ids;
2293     my @logic_ids;
2294 
2295     #opening file
2296     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2297     #reading it into an array
2298     my @lines = <FILE>;
2299     #getting the first line out 
2300     shift @lines;
2301 
2302     #temp variables
2303     my $TCC;
2304     my $trigger_tower;
2305     my $trigger_strip;
2306     my @ids;
2307     my $offline_id ;
2308 
2309     foreach my $line (@lines) {
2310 
2311             my @channels = split (/ /, $line);
2312 
2313             #id =TCC TriggerTower trigger_strip;
2314             my $id = sprintf "%03d %02d %01d %06d", $channels[8],$channels[9],$channels[10], $channels[19];
2315 
2316             push @ids, $id;
2317     }
2318 
2319     #perlish - returns unique entries using internal references AND a hash
2320     #(actually, not an original idea)
2321 
2322     undef my %saw;
2323     my @unique = grep(!$saw{$_}++, @ids);
2324     
2325     foreach my $id (@unique) {
2326 
2327         my @channels = split (/ /, $id);
2328         $TCC = $channels[0];
2329         $trigger_tower = $channels[1];
2330         $trigger_strip = $channels[2];
2331         $offline_id= $channels[3];
2332 
2333         my $logic_id = sprintf "%6d", $offline_id;
2334 
2335         push @logic_ids, $logic_id;
2336         push @channel_ids, [$TCC, $trigger_tower,$trigger_strip];
2337     }
2338 
2339 
2340     return { 
2341         name => $name, maps_to => $maps_to, 
2342         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2343     };
2344 
2345 }
2346 
2347 
2348 sub define_EE_crystal_number_to_EE_sector {
2349 
2350     my $name = "EE_crystal_number";
2351     my $maps_to = "EE_sector";
2352 
2353     my @channel_ids;
2354     my @logic_ids;
2355 
2356     #opening file
2357     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2358     #reading it into an array
2359     my @lines = <FILE>;
2360     #getting the first line out 
2361     shift @lines;
2362 
2363     #temp variables
2364 
2365     my $ix;
2366     my $iy;
2367     my $side;
2368     my $logic_id;
2369 
2370 
2371     foreach my $line (@lines) {
2372 
2373             my @channels = split (/ /, $line);
2374 
2375         $ix = $channels[0];
2376         $iy = $channels[1];
2377         $side = $channels[2];
2378         my $sideIndex = $side + 1;
2379         my $DCC = $channels[4];
2380         my $sector=0;
2381         if($DCC<10) {
2382             if($DCC<=3){
2383             $sector=$DCC+6;
2384             } else {
2385             $sector=$DCC-3;
2386             } 
2387         }else {
2388             if($DCC<=48){
2389             $sector=$DCC-39;
2390             } else {
2391             $sector=$DCC-48;
2392             }
2393         } 
2394         my $logic_id = sprintf "2000001%01d%02d", $sideIndex, $sector;
2395 
2396         #xtal number ids: side, ix, iy
2397         push @channel_ids, [$side, $ix, $iy];
2398         push @logic_ids, $logic_id;
2399 
2400     }
2401 
2402     return { 
2403         name => $name, maps_to => $maps_to, 
2404         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2405     };
2406 
2407 }
2408 
2409 
2410 
2411 
2412 sub define_EE_sector_to_EE_crystal_number {
2413 
2414     my $name = "EE_sector"; 
2415     my $maps_to = "EE_crystal_number";
2416 
2417     my @channel_ids;
2418     my @logic_ids;
2419 
2420     #opening file
2421     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2422     #reading it into an array
2423     my @lines = <FILE>;
2424     #getting the first line out 
2425     shift @lines;
2426 
2427     #temp variables
2428 
2429     my $ix;
2430     my $iy;
2431     my $sideIndex;
2432     my $logic_id;
2433 
2434 
2435     foreach my $line (@lines) {
2436 
2437             my @channels = split (/ /, $line);
2438 
2439         $ix = $channels[0];
2440         $iy = $channels[1];
2441         my $side=$channels[2];
2442         $sideIndex = $channels[2] + 1;
2443 
2444         my $DCC = $channels[4];
2445         my $sector=0;
2446         if($DCC<10) {
2447             if($DCC<=3){
2448             $sector=$DCC+6;
2449             } else {
2450             $sector=$DCC-3;
2451             } 
2452         }else {
2453             if($DCC<=48){
2454             $sector=$DCC-39;
2455             } else {
2456             $sector=$DCC-48;
2457             }
2458         } 
2459 
2460         #trigger tower ids: TCC, trigger tower
2461         push @channel_ids, [$side, $sector];
2462 
2463         #xtal number logic id: 201XYYYZZZ XX=TCC, YY=trigger tower
2464         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
2465         push @logic_ids, $logic_id;
2466 
2467     }
2468 
2469     return { 
2470         name => $name, maps_to => $maps_to, 
2471         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2472     };
2473 
2474 }
2475 
2476 sub define_EE_crystal_number_to_EE_readout_tower {
2477 
2478     my $name = "EE_crystal_number";
2479     my $maps_to = "EE_readout_tower";
2480 
2481     my @channel_ids;
2482     my @logic_ids;
2483 
2484     #opening file
2485     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2486     #reading it into an array
2487     my @lines = <FILE>;
2488     #getting the first line out 
2489     shift @lines;
2490 
2491     #temp variables
2492     my $DCC;
2493     my $readout_tower;
2494     my $ix;
2495     my $iy;
2496     my $side;
2497 
2498     my $logic_id;
2499 
2500     foreach my $line (@lines) {
2501 
2502             my @channels = split (/ /, $line);
2503 
2504         $ix = $channels[0];
2505         $iy = $channels[1];
2506         $side = $channels[2];
2507 
2508         $DCC = $channels[4] + 600;
2509         $readout_tower = $channels[5];
2510 
2511         #readout tower logic id: 2000XXXYY XXX=DCC, YY=readout tower
2512         $logic_id = sprintf "21100%03d%02d", $DCC, $readout_tower;
2513         push @logic_ids, $logic_id;
2514 
2515         #xtal number ids: ix, iy, iz
2516         push @channel_ids, [$side, $ix, $iy];
2517 
2518     }
2519 
2520     return { 
2521         name => $name, maps_to => $maps_to, 
2522         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2523     };
2524 
2525 }
2526 
2527 sub define_EE_crystal_number_to_EE_readout_strip {
2528 
2529     my $name = "EE_crystal_number";
2530     my $maps_to = "EE_readout_strip";
2531 
2532     my @channel_ids;
2533     my @logic_ids;
2534 
2535     #opening file
2536     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2537     #reading it into an array
2538     my @lines = <FILE>;
2539     #getting the first line out 
2540     shift @lines;
2541 
2542     #temp variables
2543     my $DCC;
2544     my $readout_tower;
2545     my $readout_strip;
2546     my $ix;
2547     my $iy;
2548     my $side;
2549 
2550     my $logic_id;
2551 
2552     foreach my $line (@lines) {
2553 
2554             my @channels = split (/ /, $line);
2555 
2556         $ix = $channels[0];
2557         $iy = $channels[1];
2558         $side = $channels[2];
2559 
2560         $DCC = $channels[4] ;
2561         $readout_tower = $channels[5];
2562         $readout_strip = $channels[6];
2563         my $logic_id = sprintf "2121%03d%02d%01d", $DCC, $readout_tower, $readout_strip;
2564 
2565         push @logic_ids, $logic_id;
2566 
2567         #xtal number ids: ix, iy, iz
2568         push @channel_ids, [$side, $ix, $iy];
2569 
2570     }
2571 
2572     return { 
2573         name => $name, maps_to => $maps_to, 
2574         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2575     };
2576 
2577 }
2578 
2579 sub define_EE_readout_tower_to_EE_crystal_number {
2580 
2581 
2582     my $name = "EE_readout_tower";
2583     my $maps_to = "EE_crystal_number";
2584 
2585     my @channel_ids;
2586     my @logic_ids;
2587 
2588     #opening file
2589     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2590     #reading it into an array
2591     my @lines = <FILE>;
2592     #getting the first line out 
2593     shift @lines;
2594 
2595     #temp variables
2596     my $DCC;
2597     my $readout_tower;
2598     my $ix;
2599     my $iy;
2600     my $side;
2601     my $sideIndex;
2602 
2603     my $logic_id;
2604 
2605     foreach my $line (@lines) {
2606 
2607             my @channels = split (/ /, $line);
2608 
2609         $ix = $channels[0];
2610         $iy = $channels[1];
2611         $sideIndex = $channels[2] + 1;
2612 
2613         $DCC = $channels[4] + 600;
2614         $readout_tower = $channels[5];
2615 
2616         #trigger tower ids: DCC, trigger tower
2617         push @channel_ids, [$DCC, $readout_tower];
2618 
2619         #xtal number logic id: 201XYYYZZZ X=sideIndex, YYY=ix YYY=iy
2620         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
2621         push @logic_ids, $logic_id;
2622 
2623     }
2624 
2625     return { 
2626         name => $name, maps_to => $maps_to, 
2627         logic_ids => \@logic_ids, channel_ids => \@channel_ids
2628     };
2629 
2630 }
2631 
2632 
2633 sub define_EE_readout_tower_xyz_to_EE_readout_tower {
2634 
2635     my $name = "EE_readout_tower_xyz";
2636     my $maps_to = "EE_readout_tower";
2637 
2638     my @channel_ids;
2639     my @logic_ids;
2640 
2641         #opening file
2642     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2643         #reading it into an array
2644     my @lines = <FILE>;
2645         #getting the first line out
2646     shift @lines;
2647 
2648         #temp variables
2649     my $ix;
2650     my $iy;
2651     my $iz;
2652 
2653     my $DCC;
2654     my $readout_tower;
2655 
2656     my @ids;
2657     my @ixs;
2658     my @iys;
2659     my @izs;
2660 
2661     my $count=-1;
2662     foreach my $line (@lines) {
2663 
2664     my @channels = split (/ /, $line);
2665 
2666     $ix= $channels[0];
2667     $iy= $channels[1];
2668     $iz= $channels[2];
2669 
2670 
2671                 #id =DCC-600 ROTower;
2672     $DCC=$channels[4]+600;
2673     my $id = sprintf "21100%03d%02d", $DCC,$channels[5];
2674     my $ifound=-1;
2675     for my $i (0..$count) {
2676         if($id== $ids[$i]) {
2677         $ifound=$i;
2678         if( $ix<= $ixs[$i] && $iy<= $iys[$i] ) {
2679             $ixs[$i]=$ix;
2680             $iys[$i]=$iy;
2681         }
2682         last;
2683         }
2684     }
2685     
2686     if($ifound==-1) {
2687         $count++;
2688         push @ids, $id;
2689         push @ixs, $ix;
2690         push @iys, $iy;
2691         push @izs, $iz;
2692     }
2693 
2694     }
2695 
2696     for my $i (0..$count) {
2697     push @logic_ids, $ids[$i];
2698     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
2699     }
2700 
2701     return {        name => $name, maps_to => $maps_to, 
2702         logic_ids => \@logic_ids, channel_ids => \@channel_ids};
2703 }
2704 
2705 sub define_EE_readout_tower_zseccu_to_EE_readout_tower {
2706 
2707     my $name = "EE_readout_tower_zseccu";
2708     my $maps_to = "EE_readout_tower";
2709 
2710     my @channel_ids;
2711     my @logic_ids;
2712 
2713         #opening file
2714     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2715         #reading it into an array
2716     my @lines = <FILE>;
2717         #getting the first line out
2718     shift @lines;
2719 
2720         #temp variables
2721     my $ix;
2722     my $iy;
2723     my $iz;
2724 
2725     my $DCC;
2726     my $readout_tower;
2727 
2728     my @ids;
2729     my @ixs;
2730     my @iys;
2731     my @izs;
2732 
2733     my $count=-1;
2734     foreach my $line (@lines) {
2735 
2736     my @channels = split (/ /, $line);
2737 
2738     $ix= $channels[0];
2739     $iy= $channels[1];
2740     $iz= $channels[2];
2741 
2742 
2743     my $sideIndex = $channels[2] + 1;
2744 
2745         
2746     $DCC=$channels[4];
2747     my $sector=0;
2748     if($DCC<10) {
2749         if($DCC<=3){
2750         $sector=$DCC+6;
2751         } else {
2752         $sector=$DCC-3;
2753         } 
2754     }else {
2755         if($DCC<=48){
2756         $sector=$DCC-39;
2757         } else {
2758         $sector=$DCC-48;
2759         }
2760     } 
2761     my $ccu_id=$channels[13];
2762 
2763 
2764 
2765                 #id =DCC-600 ROTower;
2766     $DCC=$channels[4]+600;
2767     my $id = sprintf "21100%03d%02d", $DCC,$channels[5];
2768     my $ifound=-1;
2769     for my $i (0..$count) {
2770         if($id== $ids[$i]) {
2771         $ifound=$i;
2772         last;
2773         }
2774     }
2775     
2776     if($ifound==-1) {
2777         $count++;
2778         $sideIndex = $iz + 1;
2779 
2780         push @ids, $id;
2781         push @ixs, $sector;
2782         push @iys, $ccu_id;
2783         push @izs, $iz;
2784     }
2785 
2786     }
2787 
2788     for my $i (0..$count) {
2789     push @logic_ids, $ids[$i];
2790     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
2791     }
2792 
2793     return {        name => $name, maps_to => $maps_to, 
2794         logic_ids => \@logic_ids, channel_ids => \@channel_ids};
2795 }
2796 
2797 sub define_EE_readout_tower_xyz_to_EE_HV_channel {
2798 
2799     my $name = "EE_readout_tower_xyz";
2800     my $maps_to = "EE_HV_channel";
2801 
2802     my @channel_ids;
2803     my @logic_ids;
2804 
2805         #opening file
2806     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2807         #reading it into an array
2808     my @lines = <FILE>;
2809         #getting the first line out
2810     shift @lines;
2811 
2812         #temp variables
2813     my $ix;
2814     my $iy;
2815     my $iz;
2816 
2817     my $DCC;
2818     my $readout_tower;
2819 
2820     my @ids;
2821     my @ixs;
2822     my @iys;
2823     my @izs;
2824     my @logi;
2825 
2826     my $count=-1;
2827     foreach my $line (@lines) {
2828 
2829     my @channels = split (/ /, $line);
2830 
2831     $ix= $channels[0];
2832     $iy= $channels[1];
2833     $iz= $channels[2];
2834     my $Dee=$channels[14];
2835     my $hv=$channels[15];
2836 
2837                 #id =DCC-600 ROTower;
2838     $DCC=$channels[4]+600;
2839     my $id = sprintf "20000%03d%02d", $DCC,$channels[5];
2840     my $logic_id = sprintf "20600%02d0%02d", $Dee, $hv;
2841     my $ifound=-1;
2842     for my $i (0..$count) {
2843         if($id== $ids[$i]) {
2844         $ifound=$i;
2845         if( $ix<= $ixs[$i] && $iy<= $iys[$i] ) {
2846             $ixs[$i]=$ix;
2847             $iys[$i]=$iy;
2848         }
2849         last;
2850         }
2851     }
2852     
2853     if($ifound==-1) {
2854         $count++;
2855         push @ids, $id;
2856         push @ixs, $ix;
2857         push @iys, $iy;
2858         push @izs, $iz;
2859         push @logi, $logic_id;
2860     }
2861 
2862     }
2863 
2864     for my $i (0..$count) {
2865     push @logic_ids, $logi[$i];
2866     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
2867     }
2868 
2869     return {        name => $name, maps_to => $maps_to, 
2870         logic_ids => \@logic_ids, channel_ids => \@channel_ids};
2871 }
2872 
2873 sub define_EE_readout_tower_xyz_to_EE_LV_channel {
2874 
2875     my $name = "EE_readout_tower_xyz";
2876     my $maps_to = "EE_LV_channel";
2877 
2878     my @channel_ids;
2879     my @logic_ids;
2880 
2881         #opening file
2882     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2883         #reading it into an array
2884     my @lines = <FILE>;
2885         #getting the first line out
2886     shift @lines;
2887 
2888         #temp variables
2889     my $ix;
2890     my $iy;
2891     my $iz;
2892 
2893     my $DCC;
2894     my $readout_tower;
2895 
2896     my @ids;
2897     my @ixs;
2898     my @iys;
2899     my @izs;
2900     my @logi;
2901 
2902     my $count=-1;
2903     foreach my $line (@lines) {
2904 
2905     my @channels = split (/ /, $line);
2906 
2907     $ix= $channels[0];
2908     $iy= $channels[1];
2909     $iz= $channels[2];
2910     my $Dee=$channels[14];
2911     my $lv=$channels[16];
2912 
2913                 #id =DCC-600 ROTower;
2914     $DCC=$channels[4]+600;
2915     my $id = sprintf "20000%03d%02d", $DCC,$channels[5];
2916     my $logic_id = sprintf "20700%02d0%02d", $Dee, $lv;
2917     my $ifound=-1;
2918     for my $i (0..$count) {
2919         if($id== $ids[$i]) {
2920         $ifound=$i;
2921         if( $ix<= $ixs[$i] && $iy<= $iys[$i] ) {
2922             $ixs[$i]=$ix;
2923             $iys[$i]=$iy;
2924         }
2925         last;
2926         }
2927     }
2928     
2929     if($ifound==-1) {
2930         $count++;
2931         push @ids, $id;
2932         push @ixs, $ix;
2933         push @iys, $iy;
2934         push @izs, $iz;
2935         push @logi, $logic_id;
2936     }
2937 
2938     }
2939 
2940     for my $i (0..$count) {
2941     push @logic_ids, $logi[$i];
2942     push @channel_ids, [$izs[$i], $ixs[$i], $iys[$i]];
2943     }
2944 
2945     return {        name => $name, maps_to => $maps_to, 
2946         logic_ids => \@logic_ids, channel_ids => \@channel_ids};
2947 }
2948 
2949 
2950 
2951 
2952 sub define_EE_crystal_readout_strip_to_EE_crystal_number {
2953 
2954     my $name = "EE_crystal_readout_strip";
2955     my $maps_to = "EE_crystal_number";
2956 
2957     my @channel_ids;
2958     my @logic_ids;
2959 
2960     #opening file
2961     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
2962     #reading it into an array
2963     my @lines = <FILE>;
2964     #getting the first line out 
2965     shift @lines;
2966 
2967     #temp variables
2968     my $DCC;
2969     my $readout_tower;  
2970     my $readout_strip;
2971     my $crystal_readout_strip;
2972     my $crystal_readout_strip_id;
2973     my $ix;
2974     my $iy;
2975     my $side;
2976 
2977     my $logic_id;
2978 
2979     foreach my $line (@lines) {
2980 
2981         my @channels = split (/ /, $line);
2982         $ix = $channels[0];
2983         $iy = $channels[1];
2984         $side = $channels[2];
2985         my $sideIndex = $side + 1;
2986 
2987         #EE_crystal_number logicId
2988         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
2989         push @logic_ids, $logic_id;
2990 
2991             $DCC = $channels[4] + 600;
2992         $readout_tower= $channels[5];
2993         $readout_strip= $channels[6];
2994         $crystal_readout_strip=$channels[7];
2995 
2996         #crystal_readout_strip_id=(strip-1)*5+channel_in_strip
2997         $crystal_readout_strip_id=($readout_strip-1)*5+$crystal_readout_strip;
2998 
2999         #crystal_readout_strip ids: DCC, RO Tower, crystal_readout_strip_id
3000         push @channel_ids, [$DCC, $readout_tower,$crystal_readout_strip_id];
3001 
3002     }
3003 
3004     return { 
3005         name => $name, maps_to => $maps_to, 
3006         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3007     };
3008 
3009 }
3010 
3011 sub define_ECAL_crystal_number_fedccuxt_to_EE_crystal_number {
3012 
3013     my $name = "ECAL_crystal_number_fedccuxt";
3014     my $maps_to = "EE_crystal_number";
3015 
3016     my @channel_ids;
3017     my @logic_ids;
3018 
3019     #opening file
3020     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3021     #reading it into an array
3022     my @lines = <FILE>;
3023     #getting the first line out 
3024     shift @lines;
3025 
3026     #temp variables
3027     my $DCC;
3028     my $readout_tower;  
3029     my $readout_strip;
3030     my $crystal_readout_strip;
3031     my $crystal_readout_strip_id;
3032     my $ix;
3033     my $iy;
3034     my $side;
3035 
3036     my $logic_id;
3037 
3038     foreach my $line (@lines) {
3039 
3040         my @channels = split (/ /, $line);
3041         $ix = $channels[0];
3042         $iy = $channels[1];
3043         $side = $channels[2];
3044         my $sideIndex = $side + 1;
3045 
3046         #EE_crystal_number logicId
3047         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
3048         push @logic_ids, $logic_id;
3049 
3050             $DCC = $channels[4] + 600;
3051         $readout_tower= $channels[5];
3052         $readout_strip= $channels[6];
3053         $crystal_readout_strip=$channels[7];
3054 
3055         #crystal_readout_strip_id=(strip-1)*5+channel_in_strip
3056         $crystal_readout_strip_id=($readout_strip-1)*5+$crystal_readout_strip-1;
3057 
3058         #crystal_readout_strip ids: DCC, RO Tower, crystal_readout_strip_id
3059         push @channel_ids, [$DCC, $readout_tower,$crystal_readout_strip_id];
3060 
3061     }
3062 
3063     return { 
3064         name => $name, maps_to => $maps_to, 
3065         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3066     };
3067 
3068 }
3069 
3070 sub define_EE_crystal_trigger_strip_to_EE_crystal_number {
3071 
3072     my $name = "EE_crystal_trigger_strip";
3073     my $maps_to = "EE_crystal_number";
3074 
3075     my @channel_ids;
3076     my @logic_ids;
3077 
3078     #opening file
3079     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3080     #reading it into an array
3081     my @lines = <FILE>;
3082     #getting the first line out 
3083     shift @lines;
3084 
3085     #temp variables
3086     my $TCC;
3087     my $trigger_tower;
3088     my $trigger_strip;
3089     my $crystal_trigger_strip;
3090     my $crystal_trigger_strip_id;
3091 
3092     my $ix;
3093     my $iy;
3094     my $side;
3095 
3096     my $logic_id;
3097 
3098     my @ids;
3099 
3100     foreach my $line (@lines) {
3101 
3102             my @channels = split (/ /, $line);
3103 
3104         $ix = $channels[0];
3105         $iy = $channels[1];
3106         $side = $channels[2];
3107         my $sideIndex = $side + 1;
3108 
3109         #EE_crystal_number logicId
3110         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
3111         push @logic_ids, $logic_id;
3112 
3113 
3114             $TCC = $channels[8];
3115         $trigger_tower= $channels[9];
3116         $trigger_strip= $channels[10];
3117         $crystal_trigger_strip=$channels[11];
3118 
3119         #crystal_trigger_strip_id=(strip-1)*5+channel_in_strip
3120         $crystal_trigger_strip_id=($trigger_strip-1)*5+$crystal_trigger_strip;
3121 
3122         push @channel_ids, [$TCC, $trigger_tower,$crystal_trigger_strip_id];
3123     }
3124 
3125 
3126     return { 
3127         name => $name, maps_to => $maps_to, 
3128         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3129     };
3130 
3131 }
3132 
3133 
3134 
3135 
3136 sub define_EE_crystal_number_to_EE_HV_channel {
3137 
3138 
3139     my @channel_ids;
3140     my @logic_ids;
3141 
3142     #opening file
3143     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3144     #reading it into an array
3145     my @lines = <FILE>;
3146     #getting the first line out 
3147     shift @lines;
3148 
3149 
3150 
3151     my $name = "EE_crystal_number";
3152     my $maps_to = "EE_HV_channel";
3153 
3154 
3155     #temp variables
3156     my $Dee;
3157     my $hv; 
3158     my $ix;
3159     my $iy;
3160     my $side;
3161 
3162     my $logic_id;
3163 
3164     foreach my $line (@lines) {
3165 
3166         my @channels = split (/ /, $line);
3167         $ix = $channels[0];
3168         $iy = $channels[1];
3169         $side = $channels[2];
3170         $Dee = $channels[14];
3171         $hv = $channels[15];
3172         my $sideIndex = $side + 1;
3173 
3174         #EE_crystal_number logicId
3175         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
3176         push @logic_ids, $logic_id;
3177 
3178         push @channel_ids, [$Dee, $hv];
3179 
3180     }
3181 
3182     return { 
3183         name => $name, maps_to => $maps_to, 
3184         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3185     };
3186 
3187 }
3188 
3189 sub define_EE_crystal_number_to_EE_LV_channel {
3190 
3191 
3192     my @channel_ids;
3193     my @logic_ids;
3194 
3195     #opening file
3196     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3197     #reading it into an array
3198     my @lines = <FILE>;
3199     #getting the first line out 
3200     shift @lines;
3201 
3202 
3203 
3204     my $name = "EE_crystal_number";
3205     my $maps_to = "EE_LV_channel";
3206 
3207 
3208     #temp variables
3209     my $Dee;
3210     my $lv; 
3211     my $ix;
3212     my $iy;
3213     my $side;
3214 
3215     my $logic_id;
3216 
3217     foreach my $line (@lines) {
3218 
3219         my @channels = split (/ /, $line);
3220         $ix = $channels[0];
3221         $iy = $channels[1];
3222         $side = $channels[2];
3223         $Dee = $channels[14];
3224         $lv = $channels[16];
3225         my $sideIndex = $side + 1;
3226 
3227         #EE_crystal_number logicId
3228         my $logic_id = sprintf "201%01d%03d%03d", $sideIndex, $ix, $iy;
3229         push @logic_ids, $logic_id;
3230 
3231         push @channel_ids, [$Dee, $lv];
3232 
3233     }
3234 
3235     return { 
3236         name => $name, maps_to => $maps_to, 
3237         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3238     };
3239 
3240 }
3241 
3242 
3243 
3244 sub define_EE_readout_strip_zseccu_to_EE_readout_strip {
3245 
3246 
3247     my @channel_ids;
3248     my @logic_ids;
3249 
3250     #opening file
3251     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3252     #reading it into an array
3253     my @lines = <FILE>;
3254     #getting the first line out 
3255     shift @lines;
3256 
3257     my @ids;
3258 
3259     my $name = "EE_readout_strip_zseccu";
3260     my $maps_to = "EE_readout_strip";
3261 
3262     my $logic_id;
3263 
3264     foreach my $line (@lines) {
3265 
3266             my @channels = split (/ /, $line);
3267 
3268     
3269         my $DCC=$channels[4];
3270         my $sector=0;
3271         if($DCC<10) {
3272             if($DCC<=3){
3273             $sector=$DCC+6;
3274             } else {
3275             $sector=$DCC-3;
3276             } 
3277         }else {
3278             if($DCC<=48){
3279             $sector=$DCC-39;
3280             } else {
3281             $sector=$DCC-48;
3282             }
3283         } 
3284         my $ccu_id=$channels[13];
3285 
3286         my $sideIndex=$channels[2]+1;
3287         
3288 
3289 
3290             my $id = sprintf "%01d %01d %02d %01d",$sideIndex, $sector, $ccu_id, $channels[6];
3291         
3292         my $logic_id = sprintf "2121%03d%02d%01d", $channels[4],$channels[5],$channels[6];
3293 
3294             push @ids, $id;
3295         push @logic_ids, $logic_id;
3296 
3297 
3298     }
3299 
3300 
3301 
3302     my @logic_ids_uni;
3303 
3304 
3305 
3306     my $i=-1;
3307     for my $id (@logic_ids) {
3308         $i++;
3309         my $ifound=0;
3310 
3311         for my $id_uni (@logic_ids_uni) {
3312         
3313         if($id == $id_uni ) {
3314             $ifound=1;
3315 
3316             last;
3317         }
3318         }
3319         if($ifound==0){
3320 
3321                 push @logic_ids_uni, $logic_ids[$i];
3322 
3323         my @channels = split (/ /, $ids[$i]);
3324         my $sideIndex = $channels[0];
3325         my $sector= $channels[1];
3326         my $ccu_id= $channels[2];
3327         my $strip= $channels[3];
3328         my $side= $sideIndex-1;
3329         $sector=$sector*$side;
3330         push @channel_ids, [$sector, $ccu_id, $strip];
3331         
3332         }
3333     }
3334     
3335 
3336 
3337     return { 
3338         name => $name, maps_to => $maps_to, 
3339         logic_ids => \@logic_ids_uni, channel_ids => \@channel_ids
3340     };
3341 
3342 }
3343 
3344 sub define_EE_crystal_number_to_ECAL_LMR {
3345 
3346     my $name = "EE_crystal_number";
3347     my $maps_to = "ECAL_LMR";
3348 
3349     my @channel_ids;
3350     my @logic_ids;
3351 
3352     #opening file
3353     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3354     #reading it into an array
3355     my @lines = <FILE>;
3356     #getting the first line out 
3357     shift @lines;
3358 
3359     #temp variables
3360 
3361     my $ix;
3362     my $iy;
3363     my $side;
3364     my $logic_id;
3365 
3366 
3367     foreach my $line (@lines) {
3368 
3369             my @channels = split (/ /, $line);
3370 
3371         $ix = $channels[0];
3372         $iy = $channels[1];
3373         $side = $channels[2];
3374         my $sideIndex = $side + 1;
3375         my $DCC = $channels[4];
3376         my $sector=0;
3377         if($DCC<10) {
3378             if($DCC<=3){
3379             $sector=$DCC+6;
3380             } else {
3381             $sector=$DCC-3;
3382             } 
3383         }else {
3384             if($DCC<=48){
3385             $sector=$DCC-39;
3386             } else {
3387             $sector=$DCC-48;
3388             }
3389         } 
3390         my $lmr_chan=0;
3391 
3392         if($side==1){
3393             $lmr_chan=$DCC+600-573;
3394             if(($DCC==53&&$ix>50)||$DCC==54) {
3395             $lmr_chan=$lmr_chan+1;
3396             }
3397         } else {
3398             $lmr_chan=$DCC+600-518;
3399             if(($DCC==8&&$ix>50)||$DCC==9) {
3400             $lmr_chan=$lmr_chan+1;
3401             }
3402         }
3403 
3404         my $logic_id = sprintf "20603000%02d", $lmr_chan;
3405         
3406 
3407         #xtal number ids: side, ix, iy
3408         push @channel_ids, [$side, $ix, $iy];
3409         push @logic_ids, $logic_id;
3410 
3411     }
3412 
3413     return { 
3414         name => $name, maps_to => $maps_to, 
3415         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3416     };
3417 
3418 }
3419 
3420 sub define_EE_crystal_number_to_EE_LMM {
3421 
3422     my $name = "EE_crystal_number";
3423     my $maps_to = "EE_LMM";
3424 
3425     my @channel_ids;
3426     my @logic_ids;
3427 
3428     #opening file
3429     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3430     #reading it into an array
3431     my @lines = <FILE>;
3432     #getting the first line out 
3433     shift @lines;
3434 
3435     #temp variables
3436 
3437     my $ix;
3438     my $iy;
3439     my $side;
3440     my $logic_id;
3441 
3442 
3443     foreach my $line (@lines) {
3444 
3445             my @channels = split (/ /, $line);
3446 
3447         $ix = $channels[0];
3448         $iy = $channels[1];
3449         $side = $channels[2];
3450         my $sideIndex = $side + 1;
3451         my $Dee = $channels[14];
3452         my $lmm = $channels[17];
3453 
3454                 my $logic_id = sprintf "20601%02d0%02d",$Dee, $lmm;
3455 
3456         
3457 
3458         #xtal number ids: side, ix, iy
3459         push @channel_ids, [$side, $ix, $iy];
3460         push @logic_ids, $logic_id;
3461 
3462     }
3463 
3464     return { 
3465         name => $name, maps_to => $maps_to, 
3466         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3467     };
3468 
3469 }
3470 
3471 
3472 
3473 sub define_EE_crystal_number_to_EE_LM_PN {
3474 
3475     my $name = "EE_crystal_number";
3476     my $maps_to = "EE_LM_PN";
3477 
3478     my @channel_ids;
3479     my @logic_ids;
3480 
3481     #opening file
3482     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
3483     #reading it into an array
3484     my @lines = <FILE>;
3485     #getting the first line out 
3486     shift @lines;
3487 
3488     #temp variables
3489 
3490     my $ix;
3491     my $iy;
3492     my $side;
3493     my $logic_id;
3494 
3495 
3496     foreach my $line (@lines) {
3497 
3498             my @channels = split (/ /, $line);
3499 
3500         $ix = $channels[0];
3501         $iy = $channels[1];
3502         $side = $channels[2];
3503         my $sideIndex = $side + 1;
3504         my $Dee = $channels[14];
3505         my $lmm = $channels[17];
3506         my $ccu_id=$channels[13];
3507         my $pns=0;
3508 
3509         if($lmm==1) { $pns=100;}
3510         if($lmm==2)  { $pns=101;}
3511         if($lmm==3)  { $pns=102;}
3512         if($lmm==4)  { $pns=103;}
3513         if($lmm==5)  { $pns=207;}
3514         if($lmm==6)  { $pns=205;}
3515         if($lmm==7)  { $pns=206;}
3516         if($lmm==8)  { $pns=209;}
3517         if($lmm==9)  { $pns=200;}
3518         if($lmm==10) { $pns=201;}
3519         if($lmm==11) { $pns=202;}
3520         if($lmm==12) { $pns=203;}
3521         if($lmm==13) { $pns=204;}
3522         if($lmm==14) { $pns=107;}
3523         if($lmm==15) { $pns=105;}
3524         if($lmm==16) { $pns=106;}
3525         if($lmm==17) { $pns=109;}
3526         if($lmm==18) { $pns=104;}
3527         if($lmm==19) { $pns=108;}
3528 
3529 
3530         my $logic_id = sprintf "20602%02d%03d", $Dee,$pns;
3531         if($lmm==18 && ($ccu_id==37 || $ccu_id==40 ||  $ccu_id==40 ) ){
3532             $logic_id = sprintf "20602%02d%03d", $Dee,209;
3533         }
3534         #xtal number ids: side, ix, iy
3535         push @channel_ids, [$side, $ix, $iy];
3536         push @logic_ids, $logic_id;
3537 
3538     }
3539 
3540     return { 
3541         name => $name, maps_to => $maps_to, 
3542         logic_ids => \@logic_ids, channel_ids => \@channel_ids
3543     };
3544 
3545 }
3546 
3547 
3548 
3549 
3550 
3551 
3552 
3553 sub define_EB_crystal_number_to_EB_trigger_tower {
3554   my $tt_def = define_EB_trigger_tower();
3555   my $tt_logic_ids = $tt_def->{logic_ids};
3556   my $tt_channel_ids = $tt_def->{channel_ids};
3557   my $count = scalar @{$tt_logic_ids};
3558 
3559   my $name = "EB_crystal_number";
3560   my $maps_to = "EB_trigger_tower";
3561 
3562   my @logic_ids;
3563   my @channel_ids;
3564 
3565   foreach my $SM (0..36) {
3566     for my $xtal (1..1700) {
3567       my $i = POSIX::floor(($xtal-1)/20.0);
3568       my $j = ($xtal-1) - 20*$i;
3569 
3570       # calculate the tt channel indexes
3571       my $ttj = POSIX::floor($j/5.0);
3572       my $tti = POSIX::floor($i/5.0);
3573       
3574       # the trigger tower
3575       my $tt = $ttj + 4*$tti + 1;
3576       
3577       # get the logic_id for this tt channel
3578       my $tt_id;
3579       
3580       for my $i (0..$count-1) {
3581     my @ids = @{$$tt_channel_ids[$i]};
3582     if ($ids[0] == $SM && $ids[1] == $tt) {
3583       $tt_id = $$tt_logic_ids[$i];
3584       last;
3585     }
3586       }
3587 
3588       if (!defined $tt_id) {
3589     die "Cannot determine logic_id of TT channel SM=$SM, ch=$tt\n";
3590       }
3591       
3592       # set the mapping
3593       push @logic_ids, $tt_id;
3594       push @channel_ids, [ $SM, $xtal ];
3595 
3596       # print "SM $SM xtal $xtal -> TT $tt\n";
3597     }
3598   }
3599 
3600     return { 
3601       name => $name, maps_to => $maps_to, 
3602       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3603     };
3604 }
3605 
3606 sub define_EB_crystal_number_to_EB_module {
3607   my $M_def = define_EB_module();
3608   my $M_logic_ids = $M_def->{logic_ids};
3609   my $M_channel_ids = $M_def->{channel_ids};
3610   my $count = scalar @{$M_logic_ids};
3611 
3612   my $name = "EB_crystal_number";
3613   my $maps_to = "EB_module";
3614 
3615   my @logic_ids;
3616   my @channel_ids;
3617 
3618   foreach my $SM (0..36) {
3619     for my $xtal (1..1700) {
3620       my $M;
3621       if ($xtal <= 500) {
3622     $M = 1;
3623       } else {
3624     $M = POSIX::floor(($xtal - 1 - 500)/400.0) + 2;
3625       }
3626       
3627       # get the logic_id for this M channel
3628       my $M_id;
3629 
3630       for my $i (0..$count-1) {
3631     my @ids = @{$$M_channel_ids[$i]};
3632     if ($ids[0] == $SM && $ids[1] == $M) {
3633       $M_id = $$M_logic_ids[$i];
3634       last;
3635     }
3636       }
3637 
3638       if (!defined $M_id) {
3639     die "Cannot determine logic_id of M channel SM=$SM, ch=$M\n";
3640       }
3641       
3642       # set the mapping
3643       push @logic_ids, $M_id;
3644       push @channel_ids, [ $SM, $xtal ];
3645 
3646       # print "SM $SM xtal $xtal -> M $M\n";
3647     }
3648   }
3649 
3650   return { 
3651       name => $name, maps_to => $maps_to,
3652       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3653      };
3654 }
3655 
3656 sub define_EB_constr_supermodule_to_EB_supermodule {
3657   my $name = "EB_constr_supermodule";
3658   my $maps_to = "EB_supermodule";
3659 
3660   my @logic_ids;
3661   my @channel_ids;
3662   my @slot_to_constr={-1,12,17,10,1,8,4,27,20,23,25,6,34,35,15,18,30,21,9
3663                   ,24,22,13,31,26,16,2,11,5,0,29,28,14,33,32,3,7,19};
3664   foreach my $SM (1..36) {
3665       my $constSM=$slot_to_constr[$SM];
3666     my $logic_id = sprintf "1041%02d00%02d", $SM, $SM;
3667     push @logic_ids, $logic_id;
3668     push @channel_ids, [ $constSM ];
3669   }
3670 
3671   return {
3672       name => $name, maps_to => $maps_to,
3673       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3674      };
3675 }
3676 
3677 sub define_EB_constr_crystal_number_to_EB_crystal_number {
3678   my $name = "EB_constr_crystal_number";
3679   my $maps_to = "EB_crystal_number";
3680 
3681   my @logic_ids;
3682   my @channel_ids;
3683   my @slot_to_constr={-1,12,17,10,1,8,4,27,20,23,25,6,34,35,15,18,30,21,9
3684                   ,24,22,13,31,26,16,2,11,5,0,29,28,14,33,32,3,7,19};
3685 
3686   foreach my $SM (1..36) {
3687     foreach my $cn (1..1700) {
3688       my $constSM=$slot_to_constr[$SM];
3689       my $logic_id = sprintf "1011%02d%04d", $SM, $cn;
3690 
3691       # set the mapping
3692       push @logic_ids, $logic_id;
3693       push @channel_ids, [$constSM, $cn];          
3694     }
3695   }
3696   
3697   return { 
3698       name => $name, maps_to => $maps_to,
3699       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3700      };
3701 }
3702 
3703 sub define_EB_crystal_number_to_EB_elec_crystal_number {
3704   my $ecn_def = define_EB_elec_crystal_number();
3705   my $ecn_logic_ids = $ecn_def->{logic_ids};
3706   my $ecn_channel_ids = $ecn_def->{channel_ids};
3707   my $count = scalar @{$ecn_logic_ids};
3708 
3709   my $name = "EB_crystal_number";
3710   my $maps_to = "EB_elec_crystal_number";
3711 
3712   my @logic_ids;
3713   my @channel_ids;
3714 
3715   foreach my $SM (0..36) {
3716     foreach my $cn (1..1700) {
3717       my $ecn = cn_to_ecn($cn);
3718       
3719       # get the logic_id for this ecn channel
3720       my $ecn_id;
3721       for my $i (0..$count-1) {
3722     my @ids = @{$$ecn_channel_ids[$i]};
3723     if ($ids[0] == $SM && $ids[1] == $ecn) {
3724       $ecn_id = $$ecn_logic_ids[$i];
3725       last;
3726     }
3727       }
3728       if (!defined $ecn_id) {
3729     die "Cannot determine logic_id of crystal channel SM=$SM, ecn=$ecn\n";
3730       }
3731       
3732       # set the mapping
3733       push @logic_ids, $ecn_id;
3734       push @channel_ids, [$SM, $cn];          
3735     }
3736   }
3737   
3738   return { 
3739       name => $name, maps_to => $maps_to,
3740       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3741      };
3742 }
3743 
3744 
3745 
3746 sub define_EB_crystal_number_to_EB_fe_crystal_number {
3747   my $fecn_def = define_EB_fe_crystal_number();
3748   my $fecn_logic_ids = $fecn_def->{logic_ids};
3749   my $fecn_channel_ids = $fecn_def->{channel_ids};
3750   my $count = scalar @{$fecn_logic_ids};
3751 
3752   my $name = "EB_crystal_number";
3753   my $maps_to = "EB_fe_crystal_number";
3754 
3755   my @logic_ids;
3756   my @channel_ids;
3757 
3758   foreach my $SM (0..36) {
3759     foreach my $cn (1..1700) {
3760       my ($tt, $fecn) = cn_to_fecn($cn);
3761       
3762       # get the logic_id for this fecn channel
3763       my $fecn_id;
3764       for my $i (0..$count-1) {
3765     my @ids = @{$$fecn_channel_ids[$i]};
3766     if ($ids[0] == $SM && $ids[1] == $tt && $ids[2] == $fecn) {
3767       $fecn_id = $$fecn_logic_ids[$i];
3768       last;
3769     }
3770       }
3771       if (!defined $fecn_id) {
3772     die "Cannot determine logic_id of crystal channel SM=$SM, fecn=$fecn\n";
3773       }
3774       
3775       # set the mapping
3776       push @logic_ids, $fecn_id;
3777       push @channel_ids, [$SM, $cn];          
3778     }
3779   }
3780   
3781   return { 
3782       name => $name, maps_to => $maps_to,
3783       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3784      };
3785 }
3786 
3787 
3788 sub define_EB_fe_crystal_number_to_EB_crystal_number {
3789   my $cn_def = define_EB_crystal_number();
3790   my $cn_logic_ids = $cn_def->{logic_ids};
3791   my $cn_channel_ids = $cn_def->{channel_ids};
3792   my $count = scalar @{$cn_logic_ids};
3793 
3794   my $name = "EB_fe_crystal_number";
3795   my $maps_to = "EB_crystal_number";
3796 
3797   my @logic_ids;
3798   my @channel_ids;
3799 
3800   # get the logic_id for this cn channel
3801   my $cn_id;
3802   for my $i (0..$count-1) {
3803     my @ids = @{$$cn_channel_ids[$i]};
3804     my ($SM, $cn) = @ids[0..1];
3805     my ($tt, $fecn) = cn_to_fecn($cn);  
3806     $cn_id = $$cn_logic_ids[$i];
3807     # set the mapping
3808     push @logic_ids, $cn_id;
3809     push @channel_ids, [$SM, $tt, $fecn];          
3810   }
3811   
3812   return { 
3813       name => $name, maps_to => $maps_to,
3814       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3815      };
3816 }
3817 
3818 sub define_ECAL_crystal_number_fedccuxt_to_EB_crystal_number {
3819   my $cn_def = define_EB_crystal_number();
3820   my $cn_logic_ids = $cn_def->{logic_ids};
3821   my $cn_channel_ids = $cn_def->{channel_ids};
3822   my $count = scalar @{$cn_logic_ids};
3823 
3824   my $name = "ECAL_crystal_number_fedccuxt";
3825   my $maps_to = "EB_crystal_number";
3826 
3827   my @logic_ids;
3828   my @channel_ids;
3829 
3830   # get the logic_id for this cn channel
3831   my $cn_id;
3832   for my $i (0..$count-1) {
3833     my @ids = @{$$cn_channel_ids[$i]};
3834     my ($SM, $cn) = @ids[0..1];
3835     my ($tt, $fecn) = cn_to_fecn($cn);  
3836     $cn_id = $$cn_logic_ids[$i];
3837 
3838 
3839 #   now convert SM in FED 
3840     if($SM>0){
3841     my $FED=0; 
3842     if($SM <=18 ){
3843     $FED=627+$SM;
3844     }
3845     if($SM >=19 ){
3846     $FED=609+($SM-18);
3847     }
3848 
3849     # set the mapping
3850     push @logic_ids, $cn_id;
3851     push @channel_ids, [$FED, $tt, $fecn];
3852 }          
3853   }
3854   
3855   return { 
3856       name => $name, maps_to => $maps_to,
3857       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3858      };
3859 }
3860 
3861 
3862 sub define_EB_elec_crystal_number_to_EB_crystal_number {
3863   my $cn_def = define_EB_crystal_number();
3864   my $cn_logic_ids = $cn_def->{logic_ids};
3865   my $cn_channel_ids = $cn_def->{channel_ids};
3866   my $count = scalar @{$cn_logic_ids};
3867 
3868   my $name = "EB_elec_crystal_number";
3869   my $maps_to = "EB_crystal_number";
3870 
3871   my @logic_ids;
3872   my @channel_ids;
3873 
3874   foreach my $SM (0..36) {
3875     foreach my $ecn (0..1699) {
3876       my $cn = ecn_to_cn($ecn);
3877       
3878       # get the logic_id for this ecn channel
3879       my $cn_id;
3880       for my $i (0..$count-1) {
3881     my @ids = @{$$cn_channel_ids[$i]};
3882     if ($ids[0] == $SM && $ids[1] == $cn) {
3883       $cn_id = $$cn_logic_ids[$i];
3884       last;
3885     }
3886       }
3887       if (!defined $cn_id) {
3888     die "Cannot determine logic_id of crystal channel SM=$SM, cn=$cn\n";
3889       }
3890       
3891       # set the mapping
3892       push @logic_ids, $cn_id;
3893       push @channel_ids, [$SM, $ecn];          
3894     }
3895   }
3896   
3897   return { 
3898       name => $name, maps_to => $maps_to,
3899       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3900      };
3901 }
3902 
3903 
3904 
3905 sub define_EB_crystal_number_to_EB_HV_channel {
3906   my $hv_def = define_EB_HV_channel();
3907   my $hv_logic_ids = $hv_def->{logic_ids};
3908   my $hv_channel_ids = $hv_def->{channel_ids};
3909   my $count = scalar @{$hv_logic_ids};
3910 
3911   my $name = "EB_crystal_number";
3912   my $maps_to = "EB_HV_channel";
3913 
3914   my @logic_ids;
3915   my @channel_ids;
3916 
3917   foreach my $SM (0..36) {
3918     for my $xtal (1..1700) {
3919       my $i = POSIX::floor(($xtal-1)/20.0);
3920       my $j = ($xtal-1) - 20*$i;
3921 
3922       # calculate the hv channel indexes
3923       my $hvj = POSIX::floor($j/10.0);
3924       my $hvi = POSIX::floor($i/5.0);
3925       
3926       # the high voltage channel
3927       my $hv = $hvj + 2*$hvi + 1;
3928       
3929       # get the logic_id for this hv channel
3930       my $hv_id;
3931       for my $i (0..$count-1) {
3932     my @ids = @{$$hv_channel_ids[$i]};
3933     if ($ids[0] == $SM && $ids[1] == $hv) {
3934       $hv_id = $$hv_logic_ids[$i];
3935       last;
3936     }
3937       }
3938       if (!defined $hv_id) {
3939     die "Cannot determine logic_id of HV channel SM=$SM, ch=$hv\n";
3940       }
3941       
3942       # set the mapping
3943       push @logic_ids, $hv_id;
3944       push @channel_ids, [$SM, $xtal];
3945 
3946       # print "SM $SM xtal -> HV $hv\n";
3947     }
3948   }
3949 
3950   return { 
3951       name => $name, maps_to => $maps_to,
3952       logic_ids => \@logic_ids, channel_ids => \@channel_ids
3953      };
3954 }
3955 
3956 sub define_EB_crystal_number_to_EB_LV_channel {
3957   my $lv_def = define_EB_LV_channel();
3958   my $lv_logic_ids = $lv_def->{logic_ids};
3959   my $lv_channel_ids = $lv_def->{channel_ids};
3960   my $count = scalar @{$lv_logic_ids};
3961 
3962   my $name = "EB_crystal_number";
3963   my $maps_to = "EB_LV_channel";
3964 
3965   my @logic_ids;
3966   my @channel_ids;
3967 
3968   foreach my $SM (0..36) {
3969     for my $xtal (1..1700) {
3970       my $i = POSIX::floor(($xtal-1)/20.0);
3971       my $j = ($xtal-1) - 20*$i;
3972 
3973       my $lv;
3974       if ($i < 5) {
3975     $lv = 1;
3976       } else {
3977     # calculate the lv channel indexes
3978     my $lvj = POSIX::floor($j/10.0);
3979     my $lvi = POSIX::floor(($i-5)/10.0);
3980       
3981     $lv = (2*$lvi) + $lvj + 2;
3982       }
3983       
3984       # get the logic_id for this lv channel
3985       my $lv_id;
3986       for my $i (0..$count-1) {
3987     my @ids = @{ $$lv_channel_ids[$i] };
3988     if ($ids[0] == $SM && $ids[1] == $lv) {
3989       $lv_id = $$lv_logic_ids[$i];
3990       last;
3991     }
3992       }
3993       if (!defined $lv_id) {
3994     die "Cannot determine logic_id of LV channel SM=$SM, ch=$lv\n";
3995       }
3996       
3997       # set the mapping
3998       push @logic_ids, $lv_id;
3999       push @channel_ids, [ $SM, $xtal ];
4000 
4001       # print "SM $SM xtal $xtal -> LV $lv\n";
4002     }
4003   }
4004   
4005   return { 
4006       name => $name, maps_to => $maps_to,
4007       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4008      };
4009 }
4010 
4011 sub define_EB_crystal_number_to_EB_LM_channel {
4012   my $lm_def = define_EB_LM_channel();
4013   my $lm_logic_ids = $lm_def->{logic_ids};
4014   my $lm_channel_ids = $lm_def->{channel_ids};
4015   my $count = scalar @{$lm_logic_ids};
4016 
4017   my $name = "EB_crystal_number";
4018   my $maps_to = "EB_LM_channel";
4019 
4020   my @logic_ids;
4021   my @channel_ids;
4022 
4023   foreach my $SM (0..36) {
4024     for my $xtal (1..1700) {
4025       my $i = POSIX::floor(($xtal-1)/20.0);
4026       my $j = ($xtal-1) - 20*$i;
4027 
4028       my $lm;
4029       if ($i < 5) {
4030     $lm = 1;
4031       } else {
4032     # calculate the lm channel indexes
4033     my $lmj = POSIX::floor($j/10.0);
4034     my $lmi = POSIX::floor(($i-5)/20.0);
4035       
4036     $lm = (2*$lmi) + $lmj + 2;
4037       }
4038       
4039       # get the logic_id for this lm channel
4040       my $lm_id;
4041       for my $i (0..$count-1) {
4042     my @ids = @{ $$lm_channel_ids[$i] };
4043     if ($ids[0] == $SM && $ids[1] == $lm) {
4044       $lm_id = $$lm_logic_ids[$i];
4045       last;
4046     }
4047       }
4048       if (!defined $lm_id) {
4049     die "Cannot determine logic_id of LM channel SM=$SM, ch=$lm\n";
4050       }
4051 
4052       # set the mapping
4053       push @logic_ids, $lm_id;
4054       push @channel_ids, [ $SM, $xtal ];
4055 
4056       # print "SM $SM xtal $xtal -> LM $lm\n";
4057     }
4058   }
4059 
4060   return { 
4061       name => $name, maps_to => $maps_to,
4062       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4063      };
4064 }
4065 
4066 sub define_EB_crystal_number_to_EB_LM_side {
4067   my $lm_def = define_EB_LM_side();
4068   my $lm_logic_ids = $lm_def->{logic_ids};
4069   my $lm_channel_ids = $lm_def->{channel_ids};
4070   my $count = scalar @{$lm_logic_ids};
4071 
4072   my $name = "EB_crystal_number";
4073   my $maps_to = "EB_LM_side";
4074 
4075   my @logic_ids;
4076   my @channel_ids;
4077 
4078   foreach my $SM (0..36) {
4079     for my $xtal (1..1700) {
4080       my $j = POSIX::floor(($xtal-1)/20.0);
4081       my $i = ($xtal-1) - 20*$j;
4082 
4083       my $lm;
4084       if ($j < 5) {
4085     $lm = 1;
4086       } else {
4087     $lm=1;
4088     if($i<10) 
4089     { 
4090         $lm=0;
4091     }
4092       }
4093 
4094       my $logic_id = sprintf "1132%02d00%02d", $SM, $lm;
4095       
4096       # set the mapping
4097       push @logic_ids, $logic_id;
4098       push @channel_ids, [ $SM, $xtal ];
4099 
4100       # print "SM $SM xtal $xtal -> LM $lm\n";
4101     }
4102   }
4103 
4104   return { 
4105       name => $name, maps_to => $maps_to,
4106       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4107      };
4108 }
4109 
4110 sub define_EB_LM_side_to_EB_crystal_number {
4111   my $maps_to = "EB_crystal_number";
4112   my $name = "EB_LM_side";
4113 
4114   my @logic_ids;
4115   my @channel_ids;
4116 
4117   foreach my $SM (1..36) {
4118     for my $xtal (1..1700) {
4119       my $j = POSIX::floor(($xtal-1)/20.0);
4120       my $i = ($xtal-1) - 20*$j;
4121 
4122       my $lm;
4123       if ($j < 5) {
4124     $lm = 1;
4125       } else {
4126     $lm=1;
4127     if($i<10) 
4128     { 
4129         $lm=0;
4130     }
4131       }
4132 
4133       my $logic_id = sprintf "1011%02d%04d", $SM, $xtal;
4134       push @logic_ids, $logic_id;
4135       push @channel_ids, [ $SM, $lm ];
4136 
4137     }
4138   }
4139 
4140   return { 
4141       name => $name, maps_to => $maps_to,
4142       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4143      };
4144 }
4145 
4146 
4147 
4148 sub define_EB_crystal_number_to_ECAL_LMR {
4149 
4150   my $name = "EB_crystal_number";
4151   my $maps_to = "ECAL_LMR";
4152 
4153   my @logic_ids;
4154   my @channel_ids;
4155 
4156   foreach my $SM (1..36) {
4157     for my $xtal (1..1700) {
4158       my $j = POSIX::floor(($xtal-1)/20.0);
4159       my $i = ($xtal-1) - 20*$j;
4160 
4161       my $lm;
4162       if ($j < 5) {
4163     $lm = 0;
4164       } else {
4165       if($i<10) { 
4166           $lm=1;
4167       }else {
4168           $lm=0;
4169       }
4170       }
4171       my $sm_ord=0;
4172       if($SM>18) {
4173       $sm_ord=$SM-18;
4174       } else {
4175       $sm_ord=$SM+18;
4176       }
4177       
4178       my $chan=($sm_ord-1)*2+$lm+1;
4179 
4180       my $logic_id = sprintf "10603000%02d", $chan;
4181       
4182       # set the mapping
4183       push @logic_ids, $logic_id;
4184       push @channel_ids, [ $SM, $xtal ];
4185 
4186       # print "SM $SM xtal $xtal -> LM $lm\n";
4187     }
4188   }
4189 
4190   return { 
4191       name => $name, maps_to => $maps_to,
4192       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4193      };
4194 }
4195 
4196 sub define_EB_crystal_number_to_EB_LM_PN {
4197   my $pn_def = define_EB_LM_PN();
4198   my $pn_logic_ids = $pn_def->{logic_ids};
4199   my $pn_channel_ids = $pn_def->{channel_ids};
4200   my $count = scalar @{$pn_logic_ids};
4201 
4202   my $name = "EB_crystal_number";
4203   my $maps_to = "EB_LM_PN";
4204 
4205   my @logic_ids;
4206   my @channel_ids;
4207 
4208   foreach my $SM (0..36) {
4209     for my $xtal (1..1700) {
4210       # crystal indexes
4211       my $i = POSIX::floor(($xtal-1)/20.0);
4212       my $j = ($xtal-1) - 20*$i;
4213 
4214       # LM channel
4215       my $lm;
4216       if ($i < 5) {
4217       $lm = 1;
4218       } else {
4219       # calculate the lm channel indexes
4220       my $lmj = POSIX::floor($j/10.0);
4221       my $lmi = POSIX::floor(($i-5)/20.0);
4222       
4223       $lm = (2*$lmi) + $lmj + 2;
4224       }
4225 
4226       # PN channel
4227       my $pn;
4228       if ($lm == 1) { 
4229       if ($j < 10 ) { $pn = 0; }
4230       else          { $pn = 5; }
4231       } else {
4232       if ($lm % 2 == 0) { $pn = $lm/2; }
4233       else              { $pn = (($lm-1)/2) + 5; }
4234       }
4235 
4236       # get the logic_id for this PN
4237       my $pn_id;
4238       for my $n (0..$count-1) {
4239     my @ids = @{ $$pn_channel_ids[$n] };
4240     if ($ids[0] == $SM && $ids[1] == $pn) {
4241       $pn_id = $$pn_logic_ids[$n];
4242       last;
4243     }
4244       }
4245       if (!defined $pn_id) {
4246     die "Cannot determine logic_id of PN SM=$SM, pn=$pn\n";
4247       }
4248 
4249       # set the mapping
4250       push @logic_ids, $pn_id;
4251       push @channel_ids, [ $SM, $xtal ];
4252 
4253       print "SM $SM xtal $xtal -> LM_channel $lm -> PN $pn\n";
4254     }
4255   }
4256 
4257   return {
4258       name => $name, maps_to => $maps_to,
4259       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4260      };
4261 }
4262 
4263 sub define_EB_crystal_number_to_EB_T_capsule {
4264   my $t_def = define_EB_T_capsule();
4265   my $t_logic_ids = $t_def->{logic_ids};
4266   my $t_channel_ids = $t_def->{channel_ids};
4267   my $count = scalar @{$t_logic_ids};
4268 
4269   my $name = "EB_crystal_number";
4270   my $maps_to = "EB_T_capsule";
4271 
4272   my @logic_ids;
4273   my @channel_ids;
4274 
4275   foreach my $SM (0..36) {
4276     for my $xtal (1..1700) {
4277       # crystal indexes
4278       my $i = POSIX::floor(($xtal-1)/20.0);
4279       my $j = ($xtal-1) - 20*$i;
4280 
4281       # T_capsule channel
4282       my $ti = POSIX::floor($i/5.0);
4283       my $tj = POSIX::floor($j/2.0);
4284       my $t = ($ti * 10) + $tj + 1;
4285 
4286       # get the logic_id for this vfe channel
4287       my $t_id;
4288       for my $n (0..$count-1) {
4289     my @ids = @{ $$t_channel_ids[$n] };
4290     if ($ids[0] == $SM && $ids[1] == $t) {
4291       $t_id = $$t_logic_ids[$n];
4292       last;
4293     }
4294       }
4295       if (!defined $t_id) {
4296     die "Cannot determine logic_id of T_capsule channel SM=$SM, t=$t\n";
4297       }
4298 
4299       # set the mapping
4300       push @logic_ids, $t_id;
4301       push @channel_ids, [ $SM, $xtal ];
4302 
4303       print "SM $SM xtal $xtal ($i, $j) -> t $t ($ti, $tj)\n";
4304     }
4305   }
4306 
4307   return {
4308       name => $name, maps_to => $maps_to,
4309       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4310      };
4311 }
4312 
4313 sub define_EB_T_capsule_to_EB_crystal_number {
4314   my $cn_def = define_EB_crystal_number();
4315   my $cn_logic_ids = $cn_def->{logic_ids};
4316   my $cn_channel_ids = $cn_def->{channel_ids};
4317   my $count = scalar @{$cn_logic_ids};
4318 
4319   my $name = "EB_T_capsule";
4320   my $maps_to = "EB_crystal_number";
4321 
4322   my @logic_ids;
4323   my @channel_ids;
4324 
4325   foreach my $SM (0..36) {
4326     for my $tc (1..170) {
4327       # calculate the tc channel indexes
4328       my $tci = POSIX::floor(($tc-1)/10.0);
4329       my $tcj = $tc - ($tci * 10) - 1;
4330 
4331       # calculate the crystal indexes
4332       my $i = ($tci*5) + 2;
4333       my $j = ($tcj*2);
4334 
4335       # calculate the crystal number
4336       my $cn = ($i*20) + $j + 1;
4337 
4338       # get the logic_id for this tc channel
4339       my $cn_id;
4340       for my $n (0..$count-1) {
4341     my @ids = @{ $$cn_channel_ids[$n] };
4342     if ($ids[0] == $SM && $ids[1] == $cn) {
4343       $cn_id = $$cn_logic_ids[$n];
4344       last;
4345     }
4346       }
4347       if (!defined $cn_id) {
4348     die "Cannot determine logic_id of crystal SM=$SM xtal=$cn";
4349       }
4350 
4351       # set the mapping
4352       push @logic_ids, $cn_id;
4353       push @channel_ids, [ $SM, $tc ];
4354 
4355 #      print "SM $SM T_capsule $tc ($tci, $tcj) -> xtal $cn ($i, $j)\n";
4356     }
4357   }
4358 
4359   return {
4360       name => $name, maps_to => $maps_to,
4361       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4362      };
4363 }
4364 
4365 sub define_EB_crystal_number_to_EB_VFE {
4366   my $vfe_def = define_EB_VFE();
4367   my $vfe_logic_ids = $vfe_def->{logic_ids};
4368   my $vfe_channel_ids = $vfe_def->{channel_ids};
4369   my $count = scalar @{$vfe_logic_ids};
4370 
4371   my $name = "EB_crystal_number";
4372   my $maps_to = "EB_VFE";
4373 
4374   my @logic_ids;
4375   my @channel_ids;
4376 
4377   foreach my $SM (0..36) {
4378     for my $xtal (1..1700) {
4379       # crystal indexes
4380       my $i = POSIX::floor(($xtal-1)/20.0);
4381       my $j = ($xtal-1) - 20*$i;
4382 
4383       # calculate the tt channel indexes
4384       my $ttj = POSIX::floor($j/5.0);
4385       my $tti = POSIX::floor($i/5.0);
4386 
4387       # the trigger tower
4388       my $tt = $ttj + 4*$tti + 1;
4389 
4390       # electronics crystal number
4391       my $ecn = &cn_to_ecn($xtal);
4392 
4393       # VFE channel
4394       my $vfe = POSIX::floor(($ecn - 25*($tt-1))/5.0) + 1;
4395 
4396       # get the logic_id for this vfe channel
4397       my $vfe_id;
4398       for my $n (0..$count-1) {
4399     my @ids = @{ $$vfe_channel_ids[$n] };
4400     if ($ids[0] == $SM && $ids[1] == $tt && $ids[2] == $vfe) {
4401       $vfe_id = $$vfe_logic_ids[$n];
4402       last;
4403     }
4404       }
4405       if (!defined $vfe_id) {
4406     die "Cannot determine logic_id of VFE channel SM=$SM, tt=$tt, vfe=$vfe\n";
4407       }
4408 
4409       # set the mapping
4410       push @logic_ids, $vfe_id;
4411       push @channel_ids, [ $SM, $xtal ];
4412 
4413 #      print "SM $SM xtal $xtal -> tt $tt vfe $vfe\n";
4414     }
4415   }
4416 
4417   return {
4418       name => $name, maps_to => $maps_to,
4419       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4420      };
4421 }
4422 
4423 sub define_ECAL_readout_strip_to_EB_VFE {
4424   my $vfe_def = define_EB_VFE();
4425   my $vfe_logic_ids = $vfe_def->{logic_ids};
4426   my $vfe_channel_ids = $vfe_def->{channel_ids};
4427   my $count = scalar @{$vfe_logic_ids};
4428 
4429   my $name = "ECAL_readout_strip";
4430   my $maps_to = "EB_VFE";
4431 
4432   my @logic_ids;
4433   my @channel_ids;
4434 
4435 
4436   for my $n (0..$count-1) {
4437       my @ids = @{ $$vfe_channel_ids[$n] };
4438       
4439       my $SM=$ids[0];
4440       my $tt=$ids[1];
4441       my $vfe=$ids[2] ;
4442       my $vfe_id = $$vfe_logic_ids[$n];
4443       
4444 #   now convert SM in FED 
4445       if($SM>0){
4446       my $FED=0; 
4447       if($SM <=18 ){
4448           $FED=627+$SM;
4449       }
4450       if($SM >=19 ){
4451           $FED=609+($SM-18);
4452       }
4453       
4454       # set the mapping
4455       push @logic_ids, $vfe_id;
4456       push @channel_ids, [ $FED, $tt, $vfe ];
4457       }
4458     }
4459 
4460 
4461 
4462   return {
4463       name => $name, maps_to => $maps_to,
4464       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4465      };
4466 }
4467 sub define_ECAL_readout_strip_to_EE_readout_strip {
4468   my $vfe_def = define_EE_readout_strip();
4469   my $vfe_logic_ids = $vfe_def->{logic_ids};
4470   my $vfe_channel_ids = $vfe_def->{channel_ids};
4471   my $count = scalar @{$vfe_logic_ids};
4472 
4473   my $name = "ECAL_readout_strip";
4474   my $maps_to = "EE_readout_strip";
4475 
4476   my @logic_ids;
4477   my @channel_ids;
4478 
4479 
4480   for my $n (0..$count-1) {
4481       my @ids = @{ $$vfe_channel_ids[$n] };
4482       
4483       my $dcc=$ids[0]+600;
4484       my $tt=$ids[1];
4485       my $vfe=$ids[2] ;
4486       my $vfe_id = $$vfe_logic_ids[$n];
4487       
4488       
4489       # set the mapping
4490       push @logic_ids, $vfe_id;
4491       push @channel_ids, [ $dcc, $tt, $vfe ];
4492 
4493 
4494 
4495   }
4496 
4497   return {
4498       name => $name, maps_to => $maps_to,
4499       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4500      };
4501 }
4502 
4503 sub define_ECAL_readout_strip_to_EE_offline_stripid {
4504 
4505     my $name = "ECAL_readout_strip";    
4506     my $maps_to = "EE_offline_stripid";
4507 
4508     my @channel_ids;
4509     my @logic_ids;
4510 
4511     #opening file
4512     open (FILE , "CMSSW.txt") || die ("could not open EE numbering file");
4513     #reading it into an array
4514     my @lines = <FILE>;
4515     #getting the first line out 
4516     shift @lines;
4517     #temp variables
4518     my $DCC;
4519     my $readout_tower;
4520     my $readout_strip;
4521     my @ids;
4522 
4523     foreach my $line (@lines) {
4524 
4525             my @channels = split (/ /, $line);
4526 
4527             #id =DCC TTower readout_strip;
4528         my $FED=$channels[4]+600;
4529             my $id = sprintf "%03d %02d %02d %06d", $FED,$channels[5],$channels[6],$channels[19];
4530 
4531             push @ids, $id;
4532     }
4533 
4534     #perlish - returns unique entries using internal references AND a hash
4535     #(actually, not an original idea)
4536 
4537     undef my %saw;
4538     my @unique = grep(!$saw{$_}++, @ids);
4539     
4540     foreach my $id (@unique) {
4541 
4542         my @channels = split (/ /, $id);
4543         $DCC = $channels[0];
4544         $readout_tower = $channels[1];
4545         $readout_strip = $channels[2];
4546         my $logic_id = $channels[3];
4547         push @logic_ids, $logic_id;
4548         push @channel_ids, [$DCC, $readout_tower,$readout_strip];
4549     }
4550 
4551 
4552     return { 
4553         name => $name, maps_to => $maps_to, 
4554         logic_ids => \@logic_ids, channel_ids => \@channel_ids
4555     };
4556 
4557 
4558 }
4559 
4560 sub define_ECAL_trigger_tower_to_EB_trigger_tower {
4561   my $vfe_def = define_EB_trigger_tower();
4562   my $vfe_logic_ids = $vfe_def->{logic_ids};
4563   my $vfe_channel_ids = $vfe_def->{channel_ids};
4564   my $count = scalar @{$vfe_logic_ids};
4565 
4566   my $name = "ECAL_trigger_tower";
4567   my $maps_to = "EB_trigger_tower";
4568 
4569   my @logic_ids;
4570   my @channel_ids;
4571 
4572 
4573   for my $n (0..$count-1) {
4574       my @ids = @{ $$vfe_channel_ids[$n] };
4575       
4576       my $SM=$ids[0];
4577       my $tt=$ids[1];
4578       my $vfe_id = $$vfe_logic_ids[$n];
4579       
4580 #   now convert SM in FED 
4581       if($SM>0){
4582       my $TCC=0; 
4583       if($SM <=18 ){
4584           $TCC=54+$SM;
4585       }
4586       if($SM >=19 ){
4587           $TCC=36+($SM-18);
4588       }
4589       
4590       # set the mapping
4591       push @logic_ids, $vfe_id;
4592       push @channel_ids, [ $TCC, $tt ];
4593       }
4594     }
4595 
4596 
4597   return {
4598       name => $name, maps_to => $maps_to,
4599       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4600      };
4601 }
4602 sub define_EB_readout_tower_to_EB_trigger_tower {
4603   my $vfe_def = define_EB_trigger_tower();
4604   my $vfe_logic_ids = $vfe_def->{logic_ids};
4605   my $vfe_channel_ids = $vfe_def->{channel_ids};
4606   my $count = scalar @{$vfe_logic_ids};
4607 
4608   my $name = "EB_readout_tower";
4609   my $maps_to = "EB_trigger_tower";
4610 
4611   my @logic_ids;
4612   my @channel_ids;
4613 
4614 
4615   for my $n (0..$count-1) {
4616       my @ids = @{ $$vfe_channel_ids[$n] };
4617       
4618       my $SM=$ids[0];
4619       my $tt=$ids[1];
4620       my $vfe_id = $$vfe_logic_ids[$n];
4621       
4622 #   now convert SM in FED 
4623       if($SM>0){
4624       my $TCC=0; 
4625       if($SM <=18 ){
4626           $TCC=627+$SM;
4627       }
4628       if($SM >=19 ){
4629           $TCC=609+($SM-18);
4630       }
4631       
4632       # set the mapping
4633       push @logic_ids, $vfe_id;
4634       push @channel_ids, [ $TCC, $tt ];
4635       }
4636     }
4637 
4638 
4639   return {
4640       name => $name, maps_to => $maps_to,
4641       logic_ids => \@logic_ids, channel_ids => \@channel_ids
4642      };
4643 }
4644 
4645 
4646 sub ecn_to_cn {
4647   my $ecn = shift;
4648 
4649   # the trigger tower - 1
4650   my $tt = POSIX::floor($ecn/25.0);
4651   
4652   # the tt indexes
4653   my $tti = POSIX::floor($tt/4.0);
4654   my $ttj = $tt - 4*$tti;
4655   
4656   # the minimum ecn for the trigger tower
4657   my $min_ecn = $tt*25;
4658   # the minimum ecn for first row in the tt column
4659   my $min_ecn_col = $tti*100;
4660   
4661   # determine whether this is a bottom up or a top down tower
4662   my $cn;
4663   my $tt_xtal_col;
4664   if ($tt < 12 ||
4665       $tt >= 20 && $tt < 28 ||
4666       $tt >= 36 && $tt < 44 ||
4667       $tt >= 52 && $tt < 60) {
4668     # bottom up "S" pattern
4669     $tt_xtal_col = 4 - POSIX::floor(($ecn - $min_ecn)/5.0);
4670     if ($tt_xtal_col % 2 == 0) {
4671       # even column
4672       $cn = $min_ecn_col + 5*$ttj + (25 + 15*$tt_xtal_col) - ($ecn - $min_ecn);
4673     } else {
4674       # odd column
4675       $cn = $min_ecn_col + 5*$ttj + $ecn - $min_ecn + 6 + (($tt_xtal_col-1)*25);
4676     }
4677   } else {
4678     # top down "S" pattern
4679     $tt_xtal_col = POSIX::floor(($ecn - $min_ecn)/5.0);
4680     if ($tt_xtal_col % 2 == 0) {
4681       # even column
4682       $cn = $min_ecn_col + 5*$ttj + $ecn - $min_ecn + (15 * $tt_xtal_col) + 1;
4683     } else {
4684       # odd column
4685       $cn = $min_ecn_col + 5*$ttj + 30 + (($tt_xtal_col-1)*25) - ($ecn - $min_ecn);
4686     }
4687   }
4688  # printf "ecn_to_cn %4d -> %4d ... tti %2d ttj %2d tt %2d min_ecn %4d min_ecn_col %4d tt_xtal_col %1d\n", 
4689  #   $ecn, $cn, $tti, $ttj, $tt, $min_ecn, $min_ecn_col, $tt_xtal_col;
4690 
4691   return $cn;
4692 
4693 }
4694 
4695 sub cn_to_ecn {
4696   my $cn = shift;
4697 
4698   # get the tt number
4699   my $i = POSIX::floor(($cn-1)/20.0);
4700   my $j = ($cn-1) - 20*$i;
4701   
4702   # calculate the tt channel indexes
4703   my $ttj = POSIX::floor($j/5.0);
4704   my $tti = POSIX::floor($i/5.0);
4705       
4706   # the trigger tower - 1
4707   my $tt = $ttj + 4*$tti;
4708      
4709   # the minimum ecn for the trigger tower
4710   my $min_ecn = $tt*25;
4711   # the minimum ecn for first row in the tt column
4712   my $min_ecn_col = $tti*100;
4713   # the column within the trigger tower
4714   my $tt_xtal_col = $i - 5*$tti;
4715 
4716   # determine whether this is a bottom up or a top down tower
4717   my $ecn;
4718   if ($tt < 12 ||
4719       $tt >= 20 && $tt < 28 ||
4720       $tt >= 36 && $tt < 44 ||
4721       $tt >= 52 && $tt < 60) {
4722     # bottom up "S" pattern
4723     if ($tt_xtal_col % 2 == 0) {
4724       # even column
4725       $ecn = $min_ecn + ((25 + 15*$tt_xtal_col) - ($cn - $min_ecn_col - 5*$ttj));
4726     } else {
4727       # odd column
4728       $ecn = $min_ecn + ($cn - $min_ecn_col - (5*$ttj) - 6 - (($tt_xtal_col - 1) * 25));
4729     }
4730   } else {
4731     # top down "S" pattern
4732     if ($tt_xtal_col % 2 == 0) {
4733       # even column
4734       $ecn = $min_ecn + ($cn - $min_ecn_col - (5*$ttj) - (15*$tt_xtal_col) - 1);
4735     } else {
4736       # odd column
4737       $ecn = $min_ecn + (30 + (($tt_xtal_col - 1) * 25) - ($cn - $min_ecn_col - (5*$ttj)));
4738     }
4739   }
4740 
4741 # printf "cn_to_ecn %4d -> %4d ... tti %2d ttj %2d tt %2d min_ecn %4d min_ecn_col %4d tt_xtal_col %1d\n", 
4742 #  $cn, $ecn, $tti, $ttj, $tt, $min_ecn, $min_ecn_col, $tt_xtal_col;
4743 
4744   return $ecn;
4745 }
4746 
4747 
4748 
4749 sub cn_to_fecn {
4750   my $cn = shift;
4751 
4752   # get the tt number
4753   my $i = POSIX::floor(($cn-1)/20.0);
4754   my $j = ($cn-1) - 20*$i;
4755   
4756   # calculate the tt channel indexes
4757   my $ttj = POSIX::floor($j/5.0);
4758   my $tti = POSIX::floor($i/5.0);
4759       
4760   # the trigger tower - 1
4761   my $tt = $ttj + 4*$tti;
4762      
4763   # the minimum ecn for the trigger tower
4764   my $min_ecn = $tt*25;
4765   # the minimum ecn for first row in the tt column
4766   my $min_ecn_col = $tti*100;
4767   # the column within the trigger tower
4768   my $tt_xtal_col = $i - 5*$tti;
4769 
4770   # determine whether this is a bottom up or a top down tower
4771   my $ecn;
4772   if ($tt < 12 ||
4773       $tt >= 20 && $tt < 28 ||
4774       $tt >= 36 && $tt < 44 ||
4775       $tt >= 52 && $tt < 60) {
4776     # bottom up "S" pattern
4777     if ($tt_xtal_col % 2 == 0) {
4778       # even column
4779       $ecn = $min_ecn + ((25 + 15*$tt_xtal_col) - ($cn - $min_ecn_col - 5*$ttj));
4780     } else {
4781       # odd column
4782       $ecn = $min_ecn + ($cn - $min_ecn_col - (5*$ttj) - 6 - (($tt_xtal_col - 1) * 25));
4783     }
4784   } else {
4785     # top down "S" pattern
4786     if ($tt_xtal_col % 2 == 0) {
4787       # even column
4788       $ecn = $min_ecn + ($cn - $min_ecn_col - (5*$ttj) - (15*$tt_xtal_col) - 1);
4789     } else {
4790       # odd column
4791       $ecn = $min_ecn + (30 + (($tt_xtal_col - 1) * 25) - ($cn - $min_ecn_col - (5*$ttj)));
4792     }
4793   }
4794 
4795   my $fecn = $ecn - ($tt*25);
4796 
4797 # printf "cn_to_fecn %4d -> %4d ... tti %2d ttj %2d tt %2d min_ecn %4d min_ecn_col %4d tt_xtal_col %1d\n", 
4798 #   $cn, $fecn, $tti, $ttj, $tt, $min_ecn, $min_ecn_col, $tt_xtal_col;
4799 
4800   return ($tt+1, $fecn);
4801 }
4802 
4803 1;