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 = "NewSurvey.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=~/-/?1:2), ($strSide=~/int/?1:2) ); # 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 
0095 # Loop on all TID files
0096 for ( my $iRing = 1; $iRing<=$nTidRings; $iRing++ ) {
0097   for ( my $iDisk = 1; $iDisk<=$nTidDisks; $iDisk++ ) {
0098     foreach my $zSide ( @zSides ) {
0099       foreach my $dSide ( @dSides ) {
0100 
0101         # Form file and directory name
0102         my $name = "TID".$zSide."_Ring".$iRing."_Disk".$iDisk."_".$dSide;
0103    
0104         # Check exclusion
0105         # if ( $TIDexclude{$name} ) {
0106         #   print "  SKIPPING $name\n";
0107         #   next;
0108         #}
0109 
0110         print "  Processing $name...";
0111 
0112         # Geometry data
0113         my $geomUrl = $baseUrl."/".$name."/datafiles/".$name."_geometry.dat";
0114         my $gResponse = $ua->get($geomUrl);
0115         die $gResponse->status_line if ( !$gResponse->is_success );
0116         my $geometry = $gResponse->content();
0117 
0118         # Survey data
0119         my $surveyUrl = $baseUrl."/".$name."/datafiles/".$name."_survey.dat";
0120         my $sResponse = $ua->get($surveyUrl);
0121         die $sResponse->status_line if ( !$sResponse->is_success );
0122         my $survey = $sResponse->content();
0123 
0124         my @geomData = &extractData( $geometry );
0125         my @surveyData = &extractData( $survey );
0126         my @id = ( ($zSide=~/-/?1:2), $iDisk, $iRing, ($dSide=~/back/?1:2) ); # See TIDDetId
0127 
0128         &printOut( \@id, \@geomData, \@surveyData );
0129 
0130         print " done\n";
0131       }
0132     }
0133   }
0134 }
0135 
0136 #_______________________________________________________________________________
0137 # Extract data from input text, sort it according to string and module
0138 # Return sorted array
0139 sub extractData {
0140 
0141   my $data   = shift;
0142 
0143   my @lines = split( "\n", $data );
0144 
0145   shift @lines; # Remove first line
0146 
0147   return sort byStringMod @lines;
0148 
0149 }
0150 
0151 
0152 #_______________________________________________________________________________
0153 # Sort entries by string and module (first two fields)
0154 sub byStringMod {
0155 
0156   my @afields = split( /\s+/, $a );
0157   my @bfields = split( /\s+/, $b );
0158 
0159   if ( $afields[0] != $bfields[0] ) { # String number
0160     return $afields[0] <=> $bfields[0];
0161   } else { # Module number
0162     return $afields[1] <=> $bfields[1];
0163   }
0164 
0165 }
0166 
0167 
0168 #_______________________________________________________________________________
0169 # Print out the final numbers
0170 sub printOut {
0171 
0172   my $id = shift;
0173   my $geometry = shift;
0174   my $survey   = shift;
0175 
0176   # Check length of data
0177   if ( @$geometry != @$survey )
0178     {
0179       print " *** Warning: missing data in this file. Skipping.";
0180       return;
0181     }
0182 
0183   my $count = 0;
0184   foreach my $gLine ( @$geometry )
0185     {
0186 
0187       my @gFields = split( /\s+/, $gLine );
0188       printf OUTPUT "%9d", shift @gFields;  # DetId first (check only)
0189       print OUTPUT " ";
0190 
0191       foreach my $field ( @$id ) {
0192         printf OUTPUT "%2d ", $field;   # Subdet specific stuff
0193       }
0194 
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; shift @sFields; 
0201       print OUTPUT join( " ", @sFields);
0202       print OUTPUT "\n";
0203 
0204       ++$count;
0205     }
0206 
0207 }