CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CalibCalorimetry/EcalLaserAnalyzer/src/ME.cc

Go to the documentation of this file.
00001 #include <cassert>
00002 #include <cstdio>
00003 #include <cstdlib>
00004 #include <iostream>
00005 #include <time.h>
00006 
00007 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/ME.h"
00008 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MEGeom.h"
00009 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MEChannel.h"
00010 
00011 //GHM ClassImp(ME)
00012 
00013 using namespace std;
00014 
00015 const ME::TimeStamp ME::kLowMask=0xFFFFFFFF;
00016 
00017 TString ME::granularity[ME::iSizeG] = { 
00018   "R", "SM", "LMR", "LMM", "SC", "C" 
00019 };
00020 
00021 TString ME::APDPrimVar[ME::iSizeAPD] = {
00022   "FLAG", "MEAN", "RMS", "M3", 
00023   "APD_OVER_PNA_MEAN", "APD_OVER_PNA_RMS", "APD_OVER_PNA_M3",
00024   "APD_OVER_PNB_MEAN", "APD_OVER_PNB_RMS", "APD_OVER_PNB_M3",
00025   "APD_OVER_PN_MEAN", "APD_OVER_PN_RMS", "APD_OVER_PN_M3",
00026   "SHAPE_COR", "ALPHA", "BETA",
00027   "TIME_MEAN", "TIME_RMS", "TIME_M3", "TIME_NEVT" 
00028 };
00029 
00030 TString ME::PNPrimVar[ME::iSizePN] = {
00031   "FLAG", "MEAN", "RMS", "M3", 
00032   "PNA_OVER_PNB_MEAN", "PNA_OVER_PNB_RMS", "PNA_OVER_PNB_M3",
00033 };
00034 
00035 TString ME::MTQPrimVar[ME::iSizeMTQ] = {
00036   "FIT_METHOD", 
00037   "MTQ_AMPL", "MTQ_TIME", "MTQ_RISE", 
00038   "MTQ_FWHM", "MTQ_FW20", "MTQ_FW80", "MTQ_SLIDING"
00039 };
00040 
00041 TString ME::TPAPDPrimVar[ME::iSizeTPAPD] = {
00042   "FLAG", "MEAN", "RMS", "M3", "NEVT"
00043 };
00044 
00045 TString ME::TPPNPrimVar[ME::iSizeTPPN] = {
00046   "GAIN", "MEAN", "RMS", "M3"
00047 };
00048 
00049 TString ME::type[ME::iSizeT] = { 
00050   "Laser", "TestPulse"
00051 };
00052 
00053 TString ME::color[ME::iSizeC] = { 
00054   "Blue", "Green", "Red", "IRed", "LED1", "LED2" 
00055 };
00056 
00057 std::vector<MEChannel*> ME::_trees = std::vector<MEChannel*>(4,(MEChannel*)0); 
00058 
00059 bool ME::useElectronicNumbering = false;
00060 
00061 TString
00062 ME::lmdataPath( int lmr )
00063 {
00064   TString out_(getenv("MELMDAT"));
00065   out_ += "/";
00066   out_ += ME::smName(lmr);
00067   out_ += "/";
00068   return out_;
00069 }
00070 
00071 TString
00072 ME::primPath( int lmr )
00073 {
00074   TString out_(getenv("MESTORE"));
00075   out_ += "/";
00076   out_ += ME::smName(lmr);
00077   out_ += "/";
00078   return out_;
00079 }
00080 
00081 TString 
00082 ME::path()
00083 {
00084   return TString(getenv("MUSECAL"))+"/";
00085 }
00086 
00087 TString
00088 ME::rootFileName( ME::Header header, ME::Settings settings )
00089 {
00090   // get the laser monitoring region and super-module
00091   int lmr_   = ME::lmr( header.dcc, header.side );
00092   TString outfile_ = primPath( lmr_ );
00093   outfile_ += "LMF_";
00094   outfile_ += ME::smName( lmr_ );
00095   outfile_ += "_"; outfile_ += header.side; 
00096   if( settings.type==ME::iLaser )
00097     {
00098       switch( settings.wavelength )
00099         {
00100         case iBlue:   outfile_ += "_BlueLaser"; break;
00101         case iGreen:  outfile_ += "_GreenLaser";  break;
00102         case iRed:    outfile_ += "_RedLaser";  break;
00103         case iIRed:   outfile_ += "_IRedLaser";  break;
00104         default: break;
00105         }
00106     }
00107   else if( settings.type==ME::iTestPulse )
00108     {
00109       outfile_ += "_testPulse"; 
00110     }
00111   outfile_ += "_";      outfile_ += header.rundir.c_str();
00112   outfile_ += "_TS";    outfile_ += header.ts_beg;
00113   outfile_ += ".root";
00114   return outfile_;
00115 }
00116 
00117 TString
00118 ME::runListName( int lmr, int type, int color )
00119 {
00120   TString outfile_ = primPath( lmr ); 
00121   if( type==iLaser )
00122     {
00123       outfile_ += "runlist_";
00124       switch( color ) 
00125         {
00126         case ME::iBlue:   outfile_+="Blue_"; break;
00127         case ME::iGreen:  outfile_+="Red_";  break;
00128         case ME::iRed:    outfile_+="Red_";  break;
00129         case ME::iIRed:   outfile_+="IRed_";  break;
00130         default: abort();
00131         }
00132       outfile_ += "Laser";
00133     }
00134   else if( type==iTestPulse )
00135     {
00136       outfile_  += "runlist_Test_Pulse";
00137     }
00138   return outfile_;
00139 }
00140 
00141 std::vector< ME::Time >
00142 ME::timeDiff( Time t1, Time t2, short int& sign )
00143 {
00144   sign = 1;
00145   //  Time t1 = time_high( T1 );
00146   //  Time t2 = time_high( T2 );
00147   Time dt_s(0);
00148   if( t1>t2 )
00149     { 
00150       dt_s = t1-t2;
00151     }
00152   else
00153     { 
00154       sign = -1;
00155       dt_s = t2-t1;
00156     }
00157   Time dt_min = dt_s/60;
00158   Time n_s    = dt_s-dt_min*60;
00159   Time dt_h   = dt_min/60;
00160   Time n_min  = dt_min-dt_h*60;
00161   Time dt_day = dt_h/24;
00162   Time n_h    = dt_h-dt_day*24;
00163   Time n_day  = dt_day;
00164   
00165 
00166   std::vector< Time > vec_;
00167   vec_.push_back( n_day );
00168   vec_.push_back( n_h   );
00169   vec_.push_back( n_min );
00170   vec_.push_back( n_s   );
00171 
00172   return vec_;
00173 }
00174 
00175 float
00176 ME::timeDiff( Time t1, Time t0, int tunit )
00177 {
00178   float sign = 1.;
00179   Time dt(0);
00180   if( t1>t0 )
00181     { 
00182       dt = t1-t0;
00183     }
00184   else
00185     { 
00186       sign = -1.;
00187       dt = t0-t1;
00188     }
00189   float dt_f = ((float) dt)*sign;
00190   switch( tunit )
00191     {
00192     case iDay:
00193       return dt_f/86400.;
00194       break;
00195     case iHour:
00196       return dt_f/3600.;
00197       break;
00198     case iMinute:
00199       return dt_f/60.;
00200       break;
00201     default:
00202       return dt_f;
00203     };
00204   return 0;
00205 }
00206 
00207 ME::Time
00208 ME::time( float dt, Time t0, int tunit )
00209 {
00210   short int sign = 1;
00211   if( dt<0 ) sign = -1;
00212   float t_ = sign*dt;
00213   switch( tunit )
00214     {
00215     case iDay:
00216       t_*=86400;
00217       break;
00218     case iHour:
00219       t_*=3600;
00220       break;
00221     case iMinute:
00222       t_*=60;
00223       break;
00224     };
00225   ME::Time it_ = static_cast<ME::Time>(t_);
00226   std::cout << "dt/it/t0/ " << dt << "/" << it_ << "/" << t0 << std::endl; 
00227   if( sign==1 ) return t0+it_;
00228   else          return t0-it_;
00229 }
00230 
00231 ME::Time
00232 ME::time_low( TimeStamp t )
00233 {
00234   return static_cast<Time>(kLowMask&t);
00235 }
00236 
00237 ME::Time
00238 ME::time_high( TimeStamp t )
00239 {
00240   return static_cast<Time>(t>>32);
00241 }
00242 
00243 TString 
00244 ME::region[ME::iSizeE] = { 
00245   "EE-", "EB-", "EB+", "EE+" 
00246 };
00247 
00248 int 
00249 ME::ecalRegion( int ilmr )
00250 {
00251   assert( ilmr>0 && ilmr<=92 );
00252   if( ilmr<=36 ) return iEBM;
00253   ilmr-=36;
00254   if( ilmr<=36 ) return iEBP;
00255   ilmr-=36;
00256   if( ilmr<=10 ) return iEEP;
00257   return iEEM;
00258 }
00259 
00260 int
00261 ME::lmr( int idcc, int side )
00262 {
00263   int ilmr=0;
00264   
00265   assert( side==0 || side==1 );
00266 
00267   if( idcc>600 ) idcc-=600;
00268   assert( idcc>=1 && idcc<=54 );
00269   int ireg; 
00270   if( idcc<=9 ) ireg = iEEM;
00271   else 
00272     {
00273       idcc -= 9;
00274       if( idcc<=18 ) ireg = iEBM;
00275       else
00276         {
00277           idcc -= 18;
00278           if( idcc<=18 ) ireg = iEBP;
00279           else
00280             {
00281               idcc -= 18;
00282               if( idcc<=9 ) ireg = iEEP;
00283               else
00284                 abort();
00285             }
00286         }
00287     }
00288   if( ireg==iEEM || ireg==iEEP )
00289     {
00290       if( side==1 && idcc!=8 ) 
00291         {
00292           return -1;      
00293         }
00294       ilmr = idcc;
00295       if( idcc==9 ) ilmr++;
00296       if( idcc==8 && side==1 ) ilmr++;
00297     }
00298   else if( ireg==iEBM || ireg==iEBP )
00299     {
00300       ilmr = 2*(idcc-1) + side + 1;
00301     }
00302   else
00303     abort();
00304 
00305   if( ireg==iEBP ) ilmr+=36; 
00306   else if( ireg==iEEP ) ilmr+=72; 
00307   else if( ireg==iEEM ) ilmr+=82; 
00308 
00309   return ilmr;
00310 }
00311 
00312 std::pair< int, int > 
00313 ME::dccAndSide( int ilmr )
00314 {
00315   int idcc=0;
00316   int side=0;
00317 
00318   int ireg = ecalRegion( ilmr );
00319   if( ireg==iEEM ) ilmr-=82; 
00320   else if( ireg==iEBP ) ilmr-=36; 
00321   else if( ireg==iEEP ) ilmr-=72; 
00322 
00323   if( ireg==iEEM || ireg==iEEP )
00324     {
00325       assert( ilmr>=1 && ilmr<=10 );
00326       side = 0;
00327       idcc = ilmr;
00328       if( ilmr>=9 ) idcc--; 
00329       if( ilmr==9 ) side=1;
00330     }
00331   else 
00332     {
00333       assert( ilmr>=1 && ilmr<=36 );
00334       idcc = (ilmr-1)/2+1;
00335       side = (ilmr-1)%2;
00336     }
00337 
00338   if( ireg>iEEM ) idcc+=9; 
00339   if( ireg>iEBM ) idcc+=18; 
00340   if( ireg>iEBP ) idcc+=18; 
00341   
00342   //  idcc += 600;
00343 
00344   return std::pair< int,int >( idcc, side );
00345 }
00346 
00347 void
00348 ME::regionAndSector( int ilmr, int& ireg, int& ism, int& idcc, int& side )
00349 {
00350   ireg = ecalRegion( ilmr );
00351 
00352   std::pair< int, int > ipair_ = dccAndSide( ilmr );
00353   idcc = ipair_.first;
00354   side = ipair_.second;
00355 
00356   ism = 0;
00357   if( ireg==iEEM || ireg==iEEP )
00358     {
00359       if( idcc>600 ) idcc-=600;  // also works with FEDids
00360       if( idcc>=1 && idcc<=9 )
00361         {
00362           ism = 6+idcc;
00363           if( ism>9 ) ism-=9;
00364           ism+=9;
00365         }
00366       else if( idcc>=46 && idcc<=54 )
00367         {
00368           ism = idcc-46+7;
00369           if( ism>9 ) ism-=9;
00370         }
00371       else
00372         abort();
00373     }
00374   else if( ireg==iEBM || ireg==iEBP )
00375     {
00376       if( idcc>600 ) idcc-=600;  // also works with FEDids
00377       assert( idcc>=10 && idcc<=45 );
00378       ism=idcc-9;
00379       if( ism>18 ) ism-=18;
00380       else         ism+=18;
00381     }
00382   else
00383     abort();
00384 }
00385 
00386 TString
00387 ME::smName( int ireg, int ism )
00388 {
00389   TString out;
00390   if( ireg==ME::iEEM || ireg==ME::iEEP )
00391     {
00392       assert( ism>=1 && ism<=18 );
00393       out = "EE+";
00394       if( ireg==ME::iEEM ) out = "EE-";
00395       if( ism>9 )          ism -= 9;
00396       out += ism;
00397      }
00398   else if( ireg==ME::iEBM || ireg==ME::iEBP ) 
00399     {
00400       assert( ism>=1 && ism<=36 );
00401       out = "EB+";
00402       if( ism>18 )
00403         {
00404           out = "EB-";
00405           ism -= 18;
00406         }
00407       out += ism;
00408     }
00409   else
00410     abort();
00411   return out;
00412 }
00413 
00414 TString
00415 ME::smName( int ilmr )
00416 {
00417   TString out;
00418   int reg_(0);
00419   int sm_(0);
00420   int dcc_(0);
00421   int side_(0);
00422   ME::regionAndSector( ilmr, reg_, sm_, dcc_, side_ );
00423   out = smName( reg_, sm_ );
00424   return out;
00425 }
00426 
00427 TString
00428 ME::smNameFromDcc( int idcc )
00429 {
00430   int ilmr = lmr( idcc,0 );
00431   return smName( ilmr );
00432 }
00433 
00434 MEChannel* 
00435 ME::lmrTree( int ilmr )
00436 {
00437   return regTree( ecalRegion( ilmr ) )->getDescendant( iLMRegion, ilmr );
00438 }
00439 
00440 MEChannel* 
00441 ME::regTree( int ireg )
00442 {
00443   assert( ireg>=iEEM && ireg<=iEEP );
00444   if( _trees[ireg]!=0 ) return _trees[ireg];
00445 
00446   int iEcalRegion_      = ireg;
00447   int iSector_          = 0;
00448   int iLMRegion_        = 0;
00449   int iLMModule_        = 0;
00450   int iSuperCrystal_    = 0;
00451   int iCrystal_         = 0;
00452   MEChannel* leaf_(0);
00453   MEChannel* tree_(0);
00454 
00455   if( iEcalRegion_==iEBM || iEcalRegion_==iEBP )
00456     {
00457       for( int isect=1; isect<=18; isect++ )
00458         {
00459           iSector_ = isect;
00460           if( iEcalRegion_==iEBM ) iSector_+=18; 
00461           if( _trees[iEcalRegion_]==0 )
00462             {
00463               //              std::cout << "Building the tree of crystals -- " 
00464               //                   << ME::region[iEcalRegion_];  
00465               _trees[iEcalRegion_] = new MEChannel( 0, 0, iEcalRegion_, 0 ); 
00466             }
00467           tree_=_trees[iEcalRegion_]; 
00468           for( int iX=0; iX<17; iX++ )
00469             {
00470               for( int iY=0; iY<4; iY++ )
00471                 {
00472                   iSuperCrystal_ = MEEBGeom::tt_channel( iX, iY ); 
00473                   iLMModule_     = MEEBGeom::lm_channel( iX, iY );
00474                   for( int jx=0; jx<5; jx++ )
00475                     {
00476                       for( int jy=0; jy<5; jy++ )
00477                         {
00478                           int ix=5*iX+jx;
00479                           int iy=5*iY+jy;
00480                           if( useElectronicNumbering )
00481                             {
00482                               iCrystal_ = MEEBGeom::electronic_channel(ix,iy);
00483                             }
00484                           else
00485                             {
00486                               iCrystal_ = MEEBGeom::crystal_channel(ix,iy);
00487                             }
00488                           MEEBGeom::EtaPhiCoord globalCoord = 
00489                             MEEBGeom::globalCoord( iSector_, ix, iy );
00490                           int ieta = globalCoord.first;
00491                           int iphi = globalCoord.second;
00492                           iLMRegion_ = MEEBGeom::lmr( ieta, iphi );
00493                           leaf_ = tree_;                  
00494                           leaf_ = leaf_->getDaughter( ieta, iphi, iSector_       );
00495                           leaf_ = leaf_->getDaughter( ieta, iphi, iLMRegion_     );
00496                           leaf_ = leaf_->getDaughter( ieta, iphi, iLMModule_     );
00497                           leaf_ = leaf_->getDaughter( ieta, iphi, iSuperCrystal_ );
00498                           leaf_ = leaf_->getDaughter( ieta, iphi, iCrystal_      );
00499                         }
00500                     }
00501                 }
00502             }
00503         }
00504     }
00505   else if( iEcalRegion_==iEEM || iEcalRegion_==iEEP )
00506     {
00507       int iz=1;
00508       if( iEcalRegion_==iEEM ) iz=-1;
00509       if( _trees[iEcalRegion_]==0 )
00510         {
00511           //      std::cout << "Building the tree of crystals -- " 
00512           //           << ME::region[iEcalRegion_];  
00513           _trees[iEcalRegion_] = new MEChannel( 0, 0, iEcalRegion_, 0 ); 
00514         }
00515       tree_=_trees[iEcalRegion_]; 
00516 
00517       for( int ilmr=72; ilmr<=92; ilmr++ )  // force the order of Monitoring Regions
00518         {
00519           if( ecalRegion( ilmr )!=iEcalRegion_ ) continue;
00520           for( int ilmm=1; ilmm<=19; ilmm++ ) // force the order of Monitoring Modules
00521             {
00522               for( int iXX=1; iXX<=10; iXX++ )
00523                 {
00524                   for( int iside=1; iside<=2; iside++ )  // symmetrize wrt y-axis
00525                     {
00526                       int iX=iXX;
00527                       if( iside==2 ) iX = 20-iXX+1;
00528                       for( int iY=1; iY<=20; iY++ )
00529                         {
00530                           //int iSector_   = MEEEGeom::sector( iX, iY );
00531                           int iSector_   = MEEEGeom::sm( iX, iY, iz );
00532                           if( iSector_<0 ) continue;
00533                           iLMRegion_ = MEEEGeom::lmr( iX, iY, iz );
00534                           if( iLMRegion_!=ilmr ) continue;
00535                           iLMModule_ = MEEEGeom::lmmod( iX, iY );
00536                           if( iLMModule_!=ilmm ) continue;
00537                           iSuperCrystal_ = MEEEGeom::sc( iX, iY );
00538 
00539                           for( int jxx=1; jxx<=5; jxx++ )
00540                             {
00541                               int jx = jxx;  // symmetrize...
00542                               if( iside==2 ) jx = 5-jxx+1;
00543 
00544                               for( int jy=1; jy<=5; jy++ )
00545                                 {
00546                                   int ix = 5*(iX-1)+jx;
00547                                   int iy = 5*(iY-1)+jy;
00548                                   iCrystal_ = MEEEGeom::crystal( ix, iy );
00549                                   if( iCrystal_<0 ) continue;
00550                                   leaf_ = tree_;                  
00551                                   leaf_ = leaf_->getDaughter( ix, iy, iSector_       );
00552                                   leaf_ = leaf_->getDaughter( ix, iy, iLMRegion_     );
00553                                   leaf_ = leaf_->getDaughter( ix, iy, iLMModule_     );
00554                                   leaf_ = leaf_->getDaughter( ix, iy, iSuperCrystal_ );
00555                                   leaf_ = leaf_->getDaughter( ix, iy, iCrystal_      );
00556                                 }
00557                             }
00558                         }
00559                     }
00560                 }
00561             }
00562         }
00563     }
00564   //  std::cout << ".... done" << std::endl;  
00565   return _trees[iEcalRegion_];
00566 }
00567 
00568 bool
00569 ME::isBarrel( int ilmr )
00570 {
00571   int reg_ = ecalRegion( ilmr );
00572   if     ( reg_==iEEM || reg_==iEEP ) return false;
00573   else if( reg_==iEBM || reg_==iEBP ) return true;
00574   else abort();
00575   return true;
00576 }
00577 
00578 std::pair<int,int>
00579 ME::memFromLmr( int ilmr )
00580 {
00581   if( isBarrel( ilmr ) )
00582     return MEEBGeom::memFromLmr( ilmr );
00583   else
00584     return MEEEGeom::memFromLmr( ilmr );
00585   return std::pair<int,int>();
00586 }
00587 std::vector< int>
00588 ME::apdRefChannels( int ilmmod , int ilmr)
00589 {
00590   if( isBarrel( ilmr ) )
00591     return MEEBGeom::apdRefChannels( ilmmod );
00592   else
00593     return MEEEGeom::apdRefChannels( ilmmod );
00594   return std::vector< int>();
00595 }
00596 
00597 std::vector<int>
00598 ME::lmmodFromLmr( int ilmr )
00599 {
00600   if( isBarrel(ilmr) )
00601     return MEEBGeom::lmmodFromLmr( ilmr );
00602   else
00603     return MEEEGeom::lmmodFromLmr( ilmr );
00604   return std::vector<int>();
00605 }
00606 
00607 std::vector<int>
00608 ME::memFromDcc( int idcc )
00609 {
00610   std::vector<int> vec;
00611   for( int iside=0; iside<=1;  iside++ )
00612     {
00613       int ilmr = lmr( idcc, iside );
00614       if( ilmr<0 ) continue;
00615       std::pair< int, int > mem_ = memFromLmr( ilmr );
00616       vec.push_back( mem_.first  );
00617       vec.push_back( mem_.second );
00618     }
00619   return vec;
00620 }
00621 
00622 std::vector<int>
00623 ME::lmmodFromDcc( int idcc )
00624 {  
00625   std::vector<int> vec;
00626   for( int iside=0; iside<=1;  iside++ )
00627     {
00628       int ilmr = lmr( idcc, iside );
00629       if( ilmr<0 ) continue;
00630       bool isBarrel_ = isBarrel( ilmr );
00631       std::vector< int > vec_ = lmmodFromLmr( ilmr );
00632       for( unsigned ii=0; ii<vec_.size(); ii++ )
00633         {
00634           int ilmmod_ = vec_[ii];
00635           if( !isBarrel_ )
00636             {
00637               // special case for Julie
00638               if( ilmmod_==18 && iside==1 ) ilmmod_=20;
00639               if( ilmmod_==19 && iside==1 ) ilmmod_=21;
00640             }
00641           vec.push_back( ilmmod_ );
00642         }
00643     }
00644   return vec;
00645 }
00646 
00647 std::pair<int,int> 
00648 ME::pn( int ilmr, int ilmmod, ME::PN ipn )
00649 {
00650   std::pair<int,int> pnpair_(0,0);
00651   std::pair<int,int> mempair_ = memFromLmr(ilmr);
00652   if( isBarrel( ilmr ) )
00653     {
00654       pnpair_=MEEBGeom::pn( ilmmod );
00655     }
00656   else
00657     {
00658       int dee_ = MEEEGeom::dee( ilmr );
00659       pnpair_=MEEEGeom::pn( dee_, ilmmod );
00660     }
00661   int mem_(0);
00662   int pn_(0);
00663   if( ipn==iPNA )  
00664     {
00665       mem_=mempair_.first;
00666       pn_=pnpair_.first;
00667     }
00668   else
00669     {
00670       mem_=mempair_.second;
00671       pn_=pnpair_.second;
00672     }      
00673   return std::pair<int,int>(mem_,pn_);
00674 }
00675