Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:24

0001 #!/usr/bin/perl -s
0002 #
0003 # Perl script to retrieve TIB/TID survey data
0004 #
0005 # Data is stored in the format defined by the relevant DetId constructors.
0006 #
0007 # TIB:
0008 # Layer# | Z-/Z+ | int/ext | string# | module # | data
0009 #
0010 # TID:
0011 # Z-/Z+ | Disk# | Ring# | Fw/Bw | module# | dummy | data
0012 #
0013 # where data contains:
0014 # - the global position vector;
0015 # - the 3 orientation vectors w, u, v (in the global frame);
0016 # - the radius vector.
0017 #
0018 # Author: F. Ronga
0019 # Data: February 13, 2007
0020 #
0021 
0022 use strict;
0023 
0024 # Do not bufferize output
0025 $|=1;
0026 
0027 ### Files to be excluded
0028 my %TIDexclude = ( "TID+_Ring3_Disk3_back" => 1,
0029                    "TID-_Ring3_Disk3_back" => 1 );
0030 
0031 ### Global variables
0032 my $baseUrl = "http://hep.fi.infn.it/CMS/software/CMS_Geometry/Programs/";
0033 my $nTibLayers = 4; # Number of TIB layers
0034 my $nTidDisks  = 3; # Number of TID disks
0035 my $nTidRings  = 3; # Number of TID rings
0036 
0037 # Output file
0038 my $outputFile = "Survey.dat";
0039 
0040 ### Initialise http agent
0041 require LWP::UserAgent;
0042 my $ua = LWP::UserAgent->new;
0043 
0044 ### 1. Retrieve TIB data
0045 my @strSides = ( "int", "ext" );
0046 my @zSides = ( "-", "+" );
0047 
0048 
0049 my $TIBoutput = "TIB".$outputFile;
0050 open( OUTPUT,">$TIBoutput" ) or die "Couldn't open $TIBoutput: $!";
0051 
0052 print "Writing output to $TIBoutput\n";
0053 # Loop on all TIB files
0054 for ( my $iLayer = 1; $iLayer <= $nTibLayers; $iLayer++ ) {
0055   foreach my $zSide ( @zSides ) {
0056     foreach my $strSide ( @strSides )  {
0057       # Form file and directory name
0058       my $name = "TIB".$zSide."_Layer".$iLayer."_".$strSide;
0059 
0060       print "  Processing $name...";
0061 
0062       # Geometry data
0063       my $geomUrl = $baseUrl."/".$name."/datafiles/".$name."_geometry.dat";
0064       my $gResponse = $ua->get($geomUrl);
0065       die $gResponse->status_line if ( !$gResponse->is_success );
0066       my $geometry = $gResponse->content();
0067 
0068       # Survey data
0069       my $surveyUrl = $baseUrl."/".$name."/datafiles/".$name."_survey.dat";
0070       my $sResponse = $ua->get($surveyUrl);
0071       die $sResponse->status_line if ( !$sResponse->is_success );
0072       my $survey = $sResponse->content();
0073 
0074       my @geomData = &extractData( $geometry );
0075       my @surveyData = &extractData( $survey );
0076       my @id = ( $iLayer, ($zSide=~/-/?0:1), ($strSide=~/int/?0:1) ); # See TIBDetId
0077 
0078       &printOut( \@id, \@geomData, \@surveyData );
0079 
0080       print " done\n";
0081     }
0082   }
0083 }
0084 
0085 close(OUTPUT) or die "Couldn't close $TIBoutput: $!";
0086 print $TIBoutput." all done\n";
0087 
0088 
0089 # 2. Retrieve TID data
0090 my @dSides = ( "back", "front" );
0091 
0092 my $TIDoutput = "TID".$outputFile;
0093 open ( OUTPUT,">$TIDoutput" ) or die "Couldn't open $TIDoutput: $!";
0094 print "Writing output to $TIDoutput\n";
0095 
0096 # Loop on all TID files
0097 for ( my $iRing = 1; $iRing<=$nTidRings; $iRing++ ) {
0098   for ( my $iDisk = 1; $iDisk<=$nTidDisks; $iDisk++ ) {
0099     foreach my $zSide ( @zSides ) {
0100       foreach my $dSide ( @dSides ) {
0101 
0102         # Form file and directory name
0103         my $name = "TID".$zSide."_Ring".$iRing."_Disk".$iDisk."_".$dSide;
0104    
0105         # Check exclusion
0106         if ( $TIDexclude{$name} ) {
0107            print "  SKIPPING $name\n";
0108            next;
0109         }
0110 
0111         print "  Processing $name...";
0112 
0113         # Geometry data
0114         my $geomUrl = $baseUrl."/".$name."/datafiles/".$name."_geometry.dat";
0115         my $gResponse = $ua->get($geomUrl);
0116         die $gResponse->status_line if ( !$gResponse->is_success );
0117         my $geometry = $gResponse->content();
0118 
0119         # Survey data
0120         my $surveyUrl = $baseUrl."/".$name."/datafiles/".$name."_survey.dat";
0121         my $sResponse = $ua->get($surveyUrl);
0122         die $sResponse->status_line if ( !$sResponse->is_success );
0123         my $survey = $sResponse->content();
0124 
0125         my @geomData = &extractData( $geometry );
0126         my @surveyData = &extractData( $survey );
0127         my @id = ( ($zSide=~/-/?1:2), $iDisk, $iRing, ($dSide=~/back/?0:1) ); # See TIDDetId
0128 
0129         &printOut( \@id, \@geomData, \@surveyData );
0130 
0131         print " done\n";
0132       }
0133     }
0134   }
0135 }
0136 
0137 close(OUTPUT) or die "Couldn't close $TIDoutput: $!";
0138 print $TIDoutput." all done\n";
0139 
0140 #_______________________________________________________________________________
0141 # Extract data from input text, sort it according to string and module
0142 # Return sorted array
0143 sub extractData {
0144 
0145   my $data   = shift;
0146 
0147   my @lines = split( "\n", $data );
0148 
0149   shift @lines; # Remove first line
0150 
0151   return sort byStringMod @lines;
0152 
0153 }
0154 
0155 
0156 #_______________________________________________________________________________
0157 # Sort entries by string and module (first two fields)
0158 sub byStringMod {
0159 
0160   my @afields = split( /\s+/, $a );
0161   my @bfields = split( /\s+/, $b );
0162 
0163   if ( $afields[0] != $bfields[0] ) { # String number
0164     return $afields[0] <=> $bfields[0];
0165   } else { # Module number
0166     return $afields[1] <=> $bfields[1];
0167   }
0168 
0169 }
0170 
0171 
0172 #_______________________________________________________________________________
0173 # Print out the final numbers
0174 sub printOut {
0175 
0176   my $id = shift;
0177   my $geometry = shift;
0178   my $survey   = shift;
0179 
0180   # Check length of data
0181   if ( @$geometry != @$survey )
0182     {
0183       print " *** Warning: missing data in this file. Skipping.";
0184       return;
0185     }
0186 
0187   my $count = 0;
0188   foreach my $gLine ( @$geometry )
0189     {
0190       foreach my $field ( @$id ) {
0191         printf OUTPUT "%2d ", $field;
0192       }
0193 
0194       my @gFields = split( /\s+/, $gLine );
0195       printf OUTPUT "%3d %2d ", shift @gFields, shift @gFields;
0196       print OUTPUT join( " ", @gFields);
0197       print OUTPUT " ";
0198 
0199       my @sFields = split( /\s+/, $$survey[$count] );
0200       shift @sFields; shift @sFields;
0201       print OUTPUT join( " ", @sFields);
0202       print OUTPUT "\n";
0203 
0204       ++$count;
0205     }
0206 
0207 }