LongTermGains.C

Go to the documentation of this file.
00001 #include <vector>
00002 #include <iostream>
00003 #include <iomanip>
00004 #include "emcDataManager.h"
00005 #include "emcCalFEMFactory.h"
00006 #include "emcTracedFEM.h"
00007 #include "PHTimeStamp.h"
00008 
00038 //_____________________________________________________________________________
00039 int trim(const std::vector<float>& x, const float& alpha,
00040          float& tmean, float& wmean,
00041          float& tvar, float& wvar,
00042          int& k,
00043          std::vector<float>& sx)
00044 {
00045   //
00046   // Calculates the trimmed (tmean) and Winsorized (wmean) means
00047   // of a sample (x) and estimates the variances of the two means.
00048   //
00049 
00050   // First check input parameters
00051 
00052   unsigned int n = x.size(); // number of observations
00053   if ( n < 2 )
00054     {
00055       return -1;
00056     }
00057 
00058   if ( alpha < 0 || alpha >= 0.5 )
00059     // proportion of observations
00060     // to be trimmed at each end of the sorted sample
00061     {
00062       return -2;
00063     }
00064 
00065   // Input parameters are good. Let's move on.
00066 
00067   // Insure we use a sample sorted into ascending order.
00068   sx = x;
00069   std::sort(sx.begin(), sx.end());
00070 
00071   // Number of observations trimmed at each end.
00072   k = static_cast<int>(floorf(alpha * n));
00073 
00074   assert(n - k - 1 >= 0);
00075 
00076   float sum = 0.0;
00077 
00078   for ( size_t i = k; i < n - k; ++i )
00079     {
00080       sum += sx[i];
00081     }
00082 
00083   tmean = sum / (n - 2 * k);
00084   wmean = (sum + k * sx[k] + k * sx[n - k - 1]) / n;
00085 
00086   float t2 = 0.0;
00087   float w2 = 0.0;
00088 
00089   for ( size_t i = k; i < n - k; ++i )
00090     {
00091       t2 += (sx[i] - tmean) * (sx[i] - tmean);
00092       w2 += (sx[i] - wmean) * (sx[i] - wmean);
00093     }
00094 
00095   tvar = (
00096           t2 +
00097           k * (sx[k] - tmean) * (sx[k] - tmean) +
00098           k * (sx[n - k - 1] - tmean) * (sx[n - k - 1] - tmean)
00099           ) / (n * n);
00100 
00101   wvar = (
00102           w2 +
00103           k * (sx[k] - wmean) * (sx[k] - wmean) +
00104           k * (sx[n - k - 1] - wmean) * (sx[n - k - 1] - wmean)
00105           ) / (n * n);
00106 
00107   return 0;
00108 }
00109 
00110 //_____________________________________________________________________________
00111 void inspect(const emcTracedFEM& g, float tmean, float tvar)
00112 {
00113   //
00114   // Output the number of items per channel
00115   // Channel with a number of items > tmean + 6*sigma
00116   // are indicated with "**" preceding the number of items.
00117   //
00118   std::string flag;
00119 
00120   float sigma = sqrt(tvar);
00121 
00122   for ( size_t i = 0; i < g.GetNumberOfChannels(); ++i ) 
00123     {
00124       if ( g.GetNumberOfItems(i)*1.0 > tmean+6*sigma )
00125         {
00126           flag ="**";
00127         }
00128       else
00129         {
00130           flag ="  ";
00131         }
00132 
00133       std::cout << "CH" << std::setw(3) << i << " "
00134                 << flag << std::setw(5)
00135                 << g.GetNumberOfItems(i)
00136                 << " ";
00137       if ( (i+1) % 5 == 0 )
00138         {
00139           std::cout << std::endl;
00140         }
00141     } 
00142   std::cout << std::endl;
00143 }
00144 
00145 //_____________________________________________________________________________
00146 void LongTermGains(int ifem=65, 
00147                    const float varthreshold=3,
00148                    const char* inputdir="/phenix/u/aphecetc/Run4Gains")
00149 {
00150   emcDataManager* dm = emcDataManager::GetInstance();
00151   dm->SetSourceDir(inputdir);
00152 
00153   emcTracedFEM* g = (emcTracedFEM*)emcCalFEMFactory::Create("Gains", ifem);
00154   g->SetSource(emcManageable::kFile_ASCII);
00155 
00156   PHTimeStamp tdummy;
00157 
00158   bool ok = dm->Read(*g, tdummy);
00159   if (!ok)
00160     {
00161       std::cerr << "Could not read gains for fem=" << ifem
00162                 << std::endl;
00163       return;
00164     }
00165 
00166   std::vector<float> nitems;
00167 
00168   for ( size_t i = 0; i < g->GetNumberOfChannels(); ++i )
00169     {
00170       float f = g->GetNumberOfItems(i)*1.0;
00171       nitems.push_back(f);
00172     }
00173 
00174   std::vector<float> sx;
00175   float tmean, wmean;
00176   float tvar, wvar;
00177   int k;
00178   bool dump = false;
00179   const float alphamax=0.3;
00180 
00181   for ( float alpha = 0.0; alpha < alphamax; alpha+=0.1 )
00182     {
00183       trim(nitems,alpha,tmean,wmean,tvar,wvar,k,sx);
00184 
00185       printf("alpha=%7.2f tmean=%7.2f wmean=%7.2f tvar=%7.2f wvar=%7.2f k=%d\n",
00186              alpha,tmean,wmean,tvar,wvar,k);
00187 
00188       if ( !alpha && tvar > varthreshold )
00189         {
00190           dump = true;
00191         }
00192     }
00193         
00194   if ( dump )
00195     {
00196       inspect(*g,tmean,tvar);
00197     }
00198   else
00199     {
00200       std::cout << "FEM looks OK at this varthreshold value" 
00201                 << std::endl;
00202     }
00203 }