Esempio n. 1
0
const double* AmFitter::findmin() {

  double wdeg = _wdeg;

  // A is LNB gain -- "gain"
  // B is "slope"
  // C is "offset"

  // OLD STUFF FOR ROOT-BASED MINIMIZER
  double A_start = 66.0;
  double A_step = 0.01;

  double B_start = 0.025; //0.025;
  double B_step = 0.0001;

  double C_start = -63.0;
  double C_step = 0.01;

  double n_start = 0.0E-10;
  double n_step = 1.0E-12;

  double t_shift_start = 0.0;
  double t_shift_step = 1.0;

  double X_start = -126.9;
  double X_step = A_step;

  // FOR THE PARAMETER SCAN METHOD
  // i = 'initial', f = 'final', s = 'stepsize'
  /*double Bi = 0.01;
  double Bf = 0.045;
  double Bs = 0.001;

  double Xi = -160.0;
  double Xf = -110.0;
  double Xs = 0.1;

  double ni = 5.0e-10;
  double nf = 15.0e-10;
  double ns = 1.0e-11;

  double ti = 0.0;
  double tf = 0.0;
  double ts = 1.0;

  double Xpars[3];
  Xpars[0] = Xi;
  Xpars[1] = Xf;
  Xpars[2] = Xs;

  double npars[3];
  npars[0] = ni;
  npars[1] = nf;
  npars[2] = ns;

  double tpars[3];
  tpars[0] = ti;
  tpars[1] = tf;
  tpars[2] = ts;

  double* minpars[10];
  for(int i=0;i<10;i++) {
    minpars[i] = new double[5];
  }*/


  double val = -1;

  double minval = -1;
  
  // PARAMETER SCAN
  // burn in a non-NaN value
  /*for(double B = Bi; B <= Bf; B = B + Bs) {
    for(double X = Xi; X <= Xf; X = X + Xs) {
      for(double n = ni; n <= nf; n = n + ns) {
        for(double t = ti; t <= tf; t = t + ts) {
          val = chi_sq_sum(B,X,n,t);
          if(val == val) {
            minval = val;
            n = nf;
            X = Xf;
            B = Bf;
            break;
          }
        }
      }
    }
  }*/
          

  double* ret = new double[4];

  /*for(double B = 0.01; B <= 0.05; B = B + 0.01) {
    for(double X = -160.0; X <= -110.0; X = X + 1.0) {
      for(double n = 5.0e-10; n <= 15.0e-10; n = n + 1.0e-10) {
        for(double t = -10; t <= 10; t = t + 1.0) {
          val = chi_sq_sum(B,X,n,t);
          //cout << " " << B << " " << X << " " << n << " " << t << " " << val << endl;
          if(val<=minval) {
            minval = val;
            ret[0] = B;
            ret[1] = X;
            ret[2] = n;
            ret[3] = t;
          }
        }
      }
    }
  }*/

  // PARAMETER SCAN
  /*boost::thread* scanthread[10];

  for(int i=0; i<10; i++) {
    //scanfunc(0,minval,minpars[0],Xpars,npars,tpars);
    scanthread[i] = new boost::thread(&AmFitter::scanfunc, this, i, minval, minpars[i], Xpars, npars, tpars);
  }

  for(int i=0; i<10; i++) {
    scanthread[i]->join();
  }

  minval = minpars[0][0];

  for(int i=0; i<10; i++ ) {
    if(minpars[i][0] <= minval) {
      minval = minpars[i][0];
      ret[0] = minpars[i][1];
      ret[1] = minpars[i][2];
      ret[2] = minpars[i][3];
      ret[3] = minpars[i][4];
    }
  }

  for(int i=0; i<10; i++ ) {
    delete scanthread[i];
  }*/

  // UNCOMMENT FOR MORE SUBTLE ROOT MINIMIZATION
  // not exactly as per tutorial -- didn't do 'algoName'                                                                                      
  double noise = 0.0;

  minval = chi_sq_sum(B_start,X_start,noise,t_shift_start);

  while(true) {
    /*if( !(val == val) && (_file_id != -1)) {
      noise -= 0.01e-10;
      noise -= 0.01e-10;
    }*/

    ROOT::Math::Minimizer* min = ROOT::Math::Factory::CreateMinimizer("Minuit2","Migrad");
    
    // just as in tutorial                                                                                                                      
    //min->SetMaxFunctionCalls(5000);
    //min->SetMaxFunctionCalls(1);
    min->SetMaxIterations(10000000);
    min->SetTolerance(0.0001);
    min->SetPrintLevel(0);
    
    // wrap up the function                                                                                                                     
    ROOT::Math::Functor f(this,&AmFitter::minfunc,4); // tricky, tricky                                                                      
    
    min->SetFunction(f);

    //min->SetFixedVariable(0,"B",0.02462491128); //0.02462491128
    //min->SetFixedVariable(1,"X",-128.3733503); //-128.3733503
    //min->SetVariable(0,"B",B_start,B_step);
    //min->SetLimitedVariable(0,"B",B_start,B_step,0.022,0.032); // used to find reasonable fits for everything but C4
    min->SetLimitedVariable(0,"B",B_start,B_step,0.022,0.045); 
    min->SetVariable(1,"X",X_start,X_step);
    //min->SetLimitedVariable(2,"n",n_start,n_step,0.0,15.0E-10);
    min->SetFixedVariable(2,"n",noise); // 7.611546967e-10
    //min->SetVariable(2,"n",noise,1.0e-12); // 7.611546967e-10
    min->SetLimitedVariable(3,"t_shift",t_shift_start,t_shift_step,-400.0,400.0);
    //min->SetFixedVariable(3,"t_shift",0.0);

    min->Minimize();

    val = chi_sq_sum(min->X()[0],min->X()[1],min->X()[2],min->X()[3]);
    
    if( !(val == val) /*&& (_file_id != -1)*/ ) {
      delete min;
      break;
    }

    if(val <= minval) {
      minval = val;
      ret[0] = min->X()[0];
      ret[1] = min->X()[1];
      ret[2] = min->X()[2];
      ret[3] = min->X()[3];
    }

    if( _file_id == -1 ) {
      //cout << "curr_val: " << val << " " << noise << endl;
    }
    
    noise += 0.01e-10;

    delete min;
  }

  // now let's check to see if this fit was pathological
  /*double B = ret[0]; //min->X()[0];
  double X = ret[1]; //min->X()[1];
  double n = ret[2]; //min->X()[2];
  double t_shift = ret[3]; //min->X()[3];

  double y1[216];
  double y2[216];
  double x[216];

  // sum that we'll test against for the blacklist
  double testsum = 0;

  for(int i=0;i<215;i++) {
    y1[i-0] = 10*log10((5.0E8)*(1.38065E-23)*(temps_func(time_arr[0][i]))/.001);
    //y2[i] = 10*log((A*exp(amfitter.hsk_func(amfitter.time_arr[0][i])*B+C) - n)/1);
    y2[i-0] = 10*log10( (pow(10,0.1*(hsk_func(time_arr[0][i]+t_shift)*B+X)) - n)/1);
    x[i-0] = time_arr[0][i];

    testsum += fabs( y1[i-0] - y2[i-0] );

    if( (i>100) && (i<120) ) {
      //cout << x[i-0] << " " << y1[i-0] << " " << y2[i-0] << endl;
    }
  }*/
  //cout << endl;

  if( false ) { //8.0
    _blacklist[_file_id] = 1;
  }
  else if(_blacklist[_file_id] == 1) {
    _blacklist[_file_id] = 1;
  }
  else {
    _blacklist[_file_id] = 0;
  }

  cout << "Minimizer run completed " << ret[0]<< " " << ret[1] << " " << ret[2]<< " " << ret[3]<< " " << endl;

  return ret;
  //return min->X();
}
int main(int argc, char** argv)
{
//    ROOT::Math::Minimizer* minuit = ROOT::Math::Factory::CreateMinimizer("Genetic");  //---> next ROOT  release!
 ROOT::Math::Minimizer* minuit = ROOT::Math::Factory::CreateMinimizer("Minuit", "Migrad2");
 ROOT::Math::Functor functorChi2(&Chi2Func,6); 
 
 TString genCut; //==== (eleFBrem<0.8&&eleCharge>0)

 bool traslationX ;
 bool traslationY ;
 bool traslationZ ;
 bool rotationPhi ;
 bool rotationTheta ;
 bool rotationPsi ;
 
 std::string fileName (argv[1]) ;
 boost::shared_ptr<edm::ParameterSet> parameterSet = edm::readConfig(fileName) ;

 edm::ParameterSet subPSetInput = parameterSet->getParameter<edm::ParameterSet> ("inputTree") ;
 std::vector<std::string> nameFileIn = subPSetInput.getParameter<std::vector<std::string> > ("inputFiles") ;
 std::string nameTree = subPSetInput.getParameter<std::string> ("nameTree") ;
 std::string selection = subPSetInput.getParameter<std::string> ("selection") ;
 genCut = Form("%s",selection.c_str());
 traslationX = subPSetInput.getParameter<bool> ("traslationX") ;
 traslationY = subPSetInput.getParameter<bool> ("traslationY") ;
 traslationZ = subPSetInput.getParameter<bool> ("traslationZ") ;
 rotationPhi   = subPSetInput.getParameter<bool> ("rotationPhi") ;
 rotationTheta = subPSetInput.getParameter<bool> ("rotationTheta") ;
 rotationPsi   = subPSetInput.getParameter<bool> ("rotationPsi") ;
     
 double setRotationPhi     = subPSetInput.getUntrackedParameter<double> ("setRotationPhi",0) ;
 double setRotationTheta   = subPSetInput.getUntrackedParameter<double> ("setRotationTheta",0) ;
 double setRotationPsi     = subPSetInput.getUntrackedParameter<double> ("setRotationPsi",0) ;

 
 even = subPSetInput.getUntrackedParameter<bool> ("even",true) ;
 odd = subPSetInput.getUntrackedParameter<bool> ("odd",true) ;

 edm::ParameterSet subPSetOutput = parameterSet->getParameter<edm::ParameterSet> ("outputTree") ;
 std::string nameFileOut = subPSetOutput.getParameter<std::string> ("outputFile") ;

 //==== plot input/output ====
 std::cout << " nameFileIn = ";
 for (unsigned int i=0; i<nameFileIn.size(); i++ ) std::cout << "    " << nameFileIn.at(i) << std::endl;
 std::cout << " nameFileOut = " << nameFileOut << std::endl;
 std::cout << " genCut = " << genCut.Data() << std::endl;
  
 ///==== input DATA ==== 
 myTree = new TChain(nameTree.c_str());
 int numberInput = 0;
 for (std::vector<std::string>::const_iterator listIt = nameFileIn.begin () ; listIt != nameFileIn.end () ; ++listIt) {
  numberInput++;
  myTree->Add (listIt->c_str ()) ;
  if (numberInput%4 == 0) std::cerr << "Input number " << numberInput << " ... " << listIt->c_str () << std::endl;
 }


 ///==== bias functions ==== 
 
 std::string FunctionDetaName = subPSetInput.getUntrackedParameter<std::string> ("DetaBias","0") ; // x = eta, y = charge
 std::string FunctionDphiName = subPSetInput.getUntrackedParameter<std::string> ("DphiBias","0") ;

 FunctionDeta = new TF2 ("DetaBias",FunctionDetaName.c_str(),-5,5,-2,2);
 FunctionDphi = new TF2 ("DphiBias",FunctionDphiName.c_str(),-5,5,-2,2);

 //==== output DATA ====
 
 ///==== Input ECAL position ====
 std::string inputFilesPosition = subPSetInput.getUntrackedParameter<std::string> ("inputFilesPosition","") ;

 ///==== Build variables ====
 
 double DX_SC_Mean[4];
 double DX_SC_RMS[4];
 double DY_SC_Mean[4];
 double DY_SC_RMS[4];
 double DZ_SC_Mean[4];
 double DZ_SC_RMS[4];
 
 double DTHETAe_SC_Mean[4];
 double DTHETAe_SC_RMS[4];
 double DPSIe_SC_Mean[4];
 double DPSIe_SC_RMS[4];
 double DPHIe_SC_Mean[4];
 double DPHIe_SC_RMS[4];
 
 
 std::ifstream* file;
 if (inputFilesPosition != ""){
  file = new std::ifstream(inputFilesPosition.c_str());
  if(!file->is_open())
  {
   return false;
  }
 }
 
 for (int iSC = 0; iSC<4; iSC++){
  TString cut;
  cut = Form("%s && iDetEB < -10 && iDetEE == %d",genCut.Data(),iSC);
  std::cout << "  cut = " << cut.Data() << std::endl;
  
  ///===========================
  ///==== Chi2 minimization ====
  
  double inputDX = 0;
  double inputDY = 0;
  double inputDZ = 0;
  double inputDPHIe = 0;
  double inputDTHETAe = 0;
  double inputDPSIe = 0;
  if (inputFilesPosition != ""){
   std::string buffer;
   getline(*file,buffer);
   std::stringstream line( buffer );
   line >> inputDPHIe;
   line >> inputDTHETAe;
   line >> inputDPSIe;
   line >> inputDX; inputDX/=100;
   line >> inputDY; inputDY/=100;
   line >> inputDZ; inputDZ/=100; 
  }
  
  std::cerr << " inputDPHIe   = " << inputDPHIe << std::endl;
  std::cerr << " inputDTHETAe = " << inputDTHETAe << std::endl;
  std::cerr << " inputDPSIe   = " << inputDPSIe << std::endl;
  std::cerr << " inputDX      = " << inputDX << std::endl;
  std::cerr << " inputDY      = " << inputDY << std::endl;
  std::cerr << " inputDZ      = " << inputDZ << std::endl;

  
  std::cout << " Chi2 minimization " << std::endl;
  
  globalCut = cut;
  //   unsigned int iNoSteps = 1000;
  //   unsigned int iPar_NoBG = 0;
  minuit->SetFunction(functorChi2);
  minuit->SetMaxFunctionCalls(100000);
  minuit->SetMaxIterations(10000);
  minuit->SetTolerance(0.000002);
  
  if (traslationX)  minuit->SetLimitedVariable(0,"DX",inputDX, 0.00001,-0.050,0.050);
  else  minuit->SetFixedVariable(0,"DX",0);

  if (traslationY)  minuit->SetLimitedVariable(1,"DY",inputDY, 0.00001,-0.050,0.050);
  else  minuit->SetFixedVariable(1,"DY",0);

  if (traslationZ)  minuit->SetLimitedVariable(2,"DZ",inputDZ, 0.00001,-0.050,0.050);
  else  minuit->SetFixedVariable(2,"DZ",0);


  if (rotationPhi)  minuit->SetLimitedVariable(3,"DPHIe",inputDPHIe, 0.00001,-3.15,3.15);
  else  minuit->SetFixedVariable(3,"DPHIe",inputDPHIe);

  if (rotationTheta)  minuit->SetLimitedVariable(4,"DTHETAe",inputDTHETAe, 0.00001,-3.15,3.15);
  else  minuit->SetFixedVariable(4,"DTHETAe",inputDTHETAe);

  if (rotationPsi)  minuit->SetLimitedVariable(5,"DPSIe",inputDPSIe, 0.00001,-3.15,3.15);
  else  minuit->SetFixedVariable(5,"DPSIe",inputDPSIe);

 
  minuit->Minimize();
  minuit->PrintResults();
  
  DX_SC_Mean[iSC] = (minuit->X()[0]);
  DY_SC_Mean[iSC] = (minuit->X()[1]);
  DZ_SC_Mean[iSC] = (minuit->X()[2]);
  
  DX_SC_RMS[iSC] = (minuit->Errors()[0]);
  DY_SC_RMS[iSC] = (minuit->Errors()[1]);
  DZ_SC_RMS[iSC] = (minuit->Errors()[2]);
  
  DPHIe_SC_Mean[iSC] = (minuit->X()[3]);
  DTHETAe_SC_Mean[iSC] = (minuit->X()[4]);
  DPSIe_SC_Mean[iSC] = (minuit->X()[5]);
  
  DPHIe_SC_RMS[iSC] = (minuit->Errors()[3]);
  DTHETAe_SC_RMS[iSC] = (minuit->Errors()[4]);
  DPSIe_SC_RMS[iSC] = (minuit->Errors()[5]);
  
  ///==== end Chi2 minimization ====
  
  std::cout << " iSC = " << iSC << " DPhi =   " << DPHIe_SC_Mean[iSC]   << " +/- " << DPHIe_SC_RMS[iSC]   << std::endl;
  std::cout << " iSC = " << iSC << " DTheta = " << DTHETAe_SC_Mean[iSC] << " +/- " << DTHETAe_SC_RMS[iSC] << std::endl;
  std::cout << " iSC = " << iSC << " DPsi =   " << DPSIe_SC_Mean[iSC]   << " +/- " << DPSIe_SC_RMS[iSC]   << std::endl;
  std::cout << " iSC = " << iSC << " DX =     " << DX_SC_Mean[iSC]      << " +/- " << DX_SC_RMS[iSC]      << std::endl;
  std::cout << " iSC = " << iSC << " DY =     " << DY_SC_Mean[iSC]      << " +/- " << DY_SC_RMS[iSC]      << std::endl;
  std::cout << " iSC = " << iSC << " DZ =     " << DZ_SC_Mean[iSC]      << " +/- " << DZ_SC_RMS[iSC]      << std::endl;
  std::cout << "============================================================================" << std::endl;
  std::cout << "============================================================================" << std::endl;
  std::cout << "============================================================================" << std::endl;
  
 }