72{
   76 
   77  string inputFile;
   79  string detectorFile;
   80  bool   overwriteDetector;
   81  bool   noInterDU;
   82  int    maxFloors;
   83  string option;
   84  int    idom_ref;
   86 
   87  try { 
   88 
   89    JParser<> zap(
"Program to calculate time offsets from FitL1dtSlices output");
 
   90    
   91    zap[
'f'] = 
make_field(inputFile,         
"input file")                   = 
"fitslices.root";
 
   93    zap[
'a'] = 
make_field(detectorFile,      
"detector file");
 
   94    zap[
'A'] = 
make_field(overwriteDetector, 
"overwrite the input detector file");
 
   95    zap[
'D'] = 
make_field(noInterDU,         
"do not use inter DU information in the calculation");
 
   96    zap[
'r'] = 
make_field(idom_ref,          
"reference DOM set to t=0")     = 9;
 
   97    zap[
'F'] = 
make_field(maxFloors,         
"number of floors used in fit") = 9999;
 
   99 
  100    if (zap.read(argc, argv) != 0) {
  101      return 1;
  102    }
  103  }
  104  catch(const exception &error) {
  105    FATAL(error.what() << endl);
 
  106  }
  107 
  108 
  110 
  111  try {
  113  }
  116  }
  117 
  119  for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  120    DU_IDs.insert(moduleA->getString());
  121    if (!noInterDU) {
  122      break;
  123    }
  124  }
  125 
  126  TFile* in = TFile::Open(inputFile.c_str(), "exist");
  127 
  128  if (in == NULL || !in->IsOpen()) {
  129    FATAL(
"File: " << inputFile << 
" not opened." << endl);
 
  130  }
  131 
  132  
  133  TH2D* h2A = (TH2D*)in->Get("Aij");
  134  TH2D* h2B = (TH2D*)in->Get("Bij");
  135 
  136  if (h2A == NULL || h2B == NULL) {
  137    FATAL(
"File does not contain histogram with matrix. Please use FitL1dtSlices script first." << endl);
 
  138  }
  139 
  141 
  142  h2A->Write();
  143  h2B->Write();
  144 
  148 
  153 
  154    
  155    for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  157      const int ibin = h2A->GetXaxis()->FindBin(TString(Form("%i", moduleA->getID())));
  158 
  159      if (noInterDU && (moduleA->getString() != *DU_ID)) {
  160        continue;
  161      }
  162 
  163      for (JDetector::iterator moduleB = 
detector.begin(); moduleB != 
detector.end(); ++moduleB) {
 
  165        const int jbin = h2A->GetXaxis()->FindBin(TString(Form("%i", moduleB->getID())));
  166 
  167        if (idom == jdom) {
  168          continue;
  169        }
  170 
  171        if (noInterDU && (moduleB->getString() != *DU_ID)) {
  172          continue;
  173        }
  174 
  175        if (fabs(moduleA->getFloor() - moduleB->getFloor()) > maxFloors) {
  176          continue;
  177        }
  178 
  179        double Aij = h2A->GetBinContent(ibin, jbin);
  180        double Bij = h2B->GetBinContent(ibin, jbin);
  181 
  182        
  183        if (Aij >= 0.0) {
  184          continue;
  185        }
  186 
  187        NOTICE(
"  " << idom << 
" " << jdom << 
"  " << moduleA->getID() << 
" " << moduleB->getID() << 
" Aij: " << Aij << 
" Bij: " << Bij << 
" bf: " << -0.5*Bij/Aij << endl);
 
  188 
  189        fitA.at(idom, jdom)  = -4.0*Aij;
  190        fitA.at(idom, idom) +=  4.0*Aij;
  191        fitB[idom]          +=  2.0*Bij; 
  192        diag[idom]          +=  4.0*Aij; 
  193      }
  194    }
  195 
  196    
  197    unsigned int icolumn = 0;
  198    int idom = 0;
  200    while (icolumn < fitA.size()) {
  201      if (fitA.at(icolumn, icolumn) == 0) {
  203        fitB.erase(fitB.begin() + icolumn);
  204 
  205        changet0[idom] = false;
  206      } else {
  207        icolumn++;
  208      }
  209      idom++;
  210    }
  211 
  212    
  213    if (fitA.size() <= 1) {
  214      continue;
  215    }
  216 
  217    
  218    
  219 
  220    TMatrixDSym fit_A(fitA.size());
  221    fit_A.SetTol(1e-26); 
  222    TVectorD fit_B(fitB.size());
  223 
  224    for (unsigned int i = 0; i < fitA.size(); i++) {
  225      for (
unsigned int j = 0; 
j < fitA.size(); 
j++) {
 
  226        fit_A(i,j) = fitA.at(i,j);
  227      }
  228      fit_B(i) = fitB[i];
  229    }
  230 
  231    
  232    for (int i = 0; i < fit_A.GetNrows(); ++i) {
  233      fit_A(idom_ref-1, i) = 0.0;
  234      fit_A(i, idom_ref-1) = 0.0;
  235    }
  236    fit_A(idom_ref-1, idom_ref-1) = 1.0;
  237    fit_B(idom_ref-1) = 0.0;
  238 
  239    if (
debug >= 2) fit_A.Print();
 
  240 
  241    
  242    Double_t fit_A_det = -999; 
  243    fit_A.Invert(&fit_A_det);
  244    if (fit_A_det == 0) {
  245      NOTICE(
"Matrix not invertible." << endl);
 
  246    }
  247 
  248    if (
debug >= 2) fit_A.Print();
 
  249 
  250    
  251    TVectorD dt_i = fit_A * fit_B;
  252 
  253    
  254    int k = 0;
  255    for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  257      if (changet0[idom]) {
  258        dt0[idom] = dt_i(k);
  259        vart0[idom] = TMath::Sqrt(-1 / diag[idom]);
  260        status[idom] = 2;   
  261        if (k == 0) {
  262          status[idom] = 1;
  263        }  
  264        k++;
  265      }
  266    }
  267  }
  268 
  270 
  271  for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  273    NOTICE(
"Changing t0 of DOM " << moduleA->getID() << 
" (S"<<setw(2) << moduleA->getString() << 
"F" << setw(2) << moduleA->getFloor() << 
") by " << setw(13) << dt0[idom] << 
" [ns] ");
 
  274 
  275    if (status[idom] == 0) {
  276      NOTICE(
" ** unable to fit **" << endl);
 
  277    }
  278    else if (moduleA->getFloor() == idom_ref) {
  279      NOTICE(
" ** fixed **" << endl);
 
  280    }
  281    else {
  283    }
  284 
  285    if (overwriteDetector) {
  286      moduleA->add(dt0[idom]); 
  287    }
  288    h_dt0.GetXaxis()->SetBinLabel(idom+1, Form("%i", moduleA->getID()));
  289    h_dt0.SetBinContent(idom+1, dt0[idom]);
  290    h_dt0.SetBinError(idom+1, vart0[idom]);
  291  }
  292 
  293  h_dt0.Write();
  294 
  295  out.Close();
  296 
  297  if (overwriteDetector) {
  298    NOTICE(
"Store calibration data on file " << detectorFile << endl);
 
  299 
  301  }
  302}
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
 
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
 
Utility class to parse command line options.
 
JMatrixNS removeRowColumn(const JMatrixNS &A, int C)
 
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
 
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
 
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).