81int main(
int argc, 
char **argv)
 
   99    JParser<> zap(
"Program to calculate log-likelihood distributions between DOM pairs and fit a poly2 to find the shape, used in FitL1dt to find time offsets");
 
  101    zap[
'f'] = 
make_field(inputFile,    
"input file")                                        = 
"monitor.root";
 
  102    zap[
'm'] = 
make_field(modelFile,    
"model file");
 
  104    zap[
'a'] = 
make_field(detectorFile, 
"detector file");
 
  105    zap[
'T'] = 
make_field(TMax_ns,      
"scan range around 0 in data-model comparison [ns]") = 50.0;
 
  106    zap[
't'] = 
make_field(dt_fitrange,  
"fitrange of polynomial to quality [ns]")            = 5.0;
 
  107    zap[
'r'] = 
make_field(rebin,        
"rebin the histograms")                              = 1;
 
  108    zap[
'N'] = 
make_field(normaliseMC,  
"normalize the MC histogram");
 
  111    if (zap.
read(argc, argv) != 0) {
 
  115  catch(
const exception &error) {
 
  116    FATAL(error.what() << endl);
 
  120  const double TSignal_ns = 750.0; 
 
  132  TFile* in = TFile::Open(inputFile.c_str(), 
"exist");
 
  133  TFile* in_model = TFile::Open(modelFile.c_str(), 
"exist");
 
  135  if (in == NULL || !in->IsOpen()) {
 
  136    FATAL(
"File: " << inputFile << 
" not opened." << endl);
 
  138  if (in_model == NULL || !in_model->IsOpen()) {
 
  139    FATAL(
"File: " << modelFile << 
" not opened." << endl);
 
  151  for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  153    TString label = Form(
"%i", moduleA->getID());
 
  154    h2A.GetXaxis()->SetBinLabel(idom+1, label);
 
  155    h2A.GetYaxis()->SetBinLabel(idom+1, label);
 
  156    h2B.GetXaxis()->SetBinLabel(idom+1, label);
 
  157    h2B.GetYaxis()->SetBinLabel(idom+1, label);
 
  158    h2C.GetXaxis()->SetBinLabel(idom+1, label);
 
  159    h2C.GetYaxis()->SetBinLabel(idom+1, label);
 
  161    h2Bbare.GetXaxis()->SetBinLabel(idom+1, label);
 
  162    h2Bbare.GetYaxis()->SetBinLabel(idom+1, label);
 
  165  for (JDetector::iterator moduleA = 
detector.begin(); moduleA != 
detector.end(); ++moduleA) {
 
  167    DEBUG(
"Module " << moduleA->getID() << endl);
 
  172    TH2D* d2s = (TH2D*) in->Get((title + 
".2S").c_str());
 
  173    TH1D* d1l = (TH1D*) in->Get((title + 
".1L").c_str());
 
  175    if (d2s == NULL || d1l == NULL) {
 
  176      WARNING(
"No data in data histogram " << title << endl);
 
  181    TH2D* m2s = (TH2D*) in_model->Get((title + 
".2S").c_str());
 
  182    TH1D* m1l = (TH1D*) in_model->Get((title + 
".1L").c_str());
 
  184    if (m2s == NULL || m1l == NULL) {
 
  185      WARNING(
"No mata in model histogram " << title << endl);
 
  190    for (JDetector::iterator moduleB = moduleA; moduleB != 
detector.end(); ++moduleB) {
 
  193      if (moduleB->getID() == moduleA->getID()) { 
 
  196      if (moduleB->getString() != moduleA->getString()) { 
 
  200      TH1D* d1s = d2s->ProjectionY((title + Form(
".%i.d1S", moduleB->getID())).c_str(), 
 
  201                                   d2s->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))), 
 
  202                                   d2s->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))), 
"e");
 
  203      TH1D* m1s = m2s->ProjectionY((title + Form(
".%i.m1S", moduleB->getID())).c_str(), 
 
  204                                   m2s->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))), 
 
  205                                   m2s->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))), 
"e");
 
  207      if (d1s->Integral() <= 0.0 || m1s->Integral() <= 0.0) { 
 
  212      double binCenterMaxBin = d1s->GetXaxis()->GetBinCenter(d1s->GetMaximumBin());
 
  213      double backgroundrate_s = 0.0;
 
  215      for (
int j = 1; j <= d1s->GetXaxis()->GetNbins(); ++j) {
 
  216        if (fabs(d1s->GetXaxis()->GetBinCenter(j) - binCenterMaxBin) > TSignal_ns ) { 
 
  217          backgroundrate_s += d1s->GetBinContent(j); 
 
  221      backgroundrate_s /= nbins;
 
  224      binCenterMaxBin = m1s->GetXaxis()->GetBinCenter(m1s->GetMaximumBin());
 
  225      double backgroundrate_m = 0.0;
 
  227      for (
int j = 1; j <= m1s->GetXaxis()->GetNbins(); ++j) {
 
  228        if (fabs(m1s->GetXaxis()->GetBinCenter(j) - binCenterMaxBin) > TSignal_ns ) { 
 
  229          backgroundrate_m += m1s->GetBinContent(j); 
 
  233      backgroundrate_m /= nbins;
 
  236      const double Ld = d1l->GetBinContent(d1l->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))));
 
  237      const double Lm = m1l->GetBinContent(m1l->GetXaxis()->FindBin(TString(Form(
"%i", moduleB->getID()))));
 
  239      for (
int j = 1; j <= d1s->GetXaxis()->GetNbins(); ++j) {
 
  240        const double y  = d1s->GetBinContent(j);
 
  241        const double dy = d1s->GetBinError(j);
 
  243        d1s->SetBinContent(j, y);
 
  244        d1s->SetBinError(j, dy);
 
  248      for (
int j = 1; j <= m1s->GetXaxis()->GetNbins(); ++j) {
 
  249        const double y  = m1s->GetBinContent(j);
 
  250        const double dy = m1s->GetBinError(j);
 
  252        m1s->SetBinContent(j, y * Ld/Lm);
 
  253        m1s->SetBinError(j, dy*Ld/Lm);
 
  258        const double scalefactor = d1s->Integral()/m1s->Integral();
 
  259        m1s->Scale(scalefactor);
 
  271      int ddt_min = -0.5*TMax_ns;  
 
  272      int ddt_max =  0.5*TMax_ns;  
 
  274      TH1D q1((title + Form(
".%i.q1", moduleB->getID())).c_str(), (title + Form(
".%i.q1", moduleB->getID())).c_str(), (ddt_max-ddt_min)/rebin+1, ddt_min-0.5*rebin, ddt_max+0.5*rebin);
 
  276      for (
int di = 1; di <= q1.GetNbinsX(); di++) {
 
  277        q1.SetBinContent(di, getLogQuality(d1s, m1s, (
int)(q1.GetBinCenter(di)/rebin), 0.0, 0.0));
 
  281      const double dt_fitmid = q1.GetBinCenter(q1.GetMaximumBin());
 
  283      TF1 q1f((title + Form(
".%i.q1f", moduleB->getID())).c_str(), 
"[0]*x*x + [1]*x + [2]", dt_fitmid - dt_fitrange, dt_fitmid + dt_fitrange);
 
  284      q1f.SetParLimits(0, -1e5, 0.0); 
 
  286      string fitoptions = 
"R";
 
  290      q1.Fit(&q1f, fitoptions.c_str());
 
  296      const double Aij = q1f.GetParameter(0);
 
  297      const double Bij = q1f.GetParameter(1);
 
  298      const double Cij = q1f.GetParameter(2);
 
  300      double bareBij = Bij / Aij;
 
  301      if (Aij == 0) bareBij = 0;
 
  303      if (fabs(-0.5*Bij/Aij - dt_fitmid) > 3 || fabs(-0.5*Bij/Aij) > dt_fitrange) {
 
  304        WARNING(
"  Please check fit of pair " << idom << 
", " << jdom << 
" : " << moduleA->getID() << 
", " << moduleB->getID() << endl); 
 
  305        WARNING(
"  DOMpair " << idom << 
"; " << jdom << 
"  A=" << Aij << 
" B=" << Bij << 
" , max at: " << dt_fitmid << 
" [ns], best fit at " << -0.5*Bij/Aij << 
" ns " << endl);
 
  307      DEBUG(
"DOMpair " << idom << 
"; " << jdom << 
"  A=" << Aij << 
" B=" << Bij << 
" , max at: " << dt_fitmid << 
" [ns], best fit at " << -0.5*Bij/Aij << 
" ns " << endl);
 
  309      h2A.SetBinContent(idom+1, jdom+1, Aij);
 
  310      h2A.SetBinContent(jdom+1, idom+1, Aij);
 
  311      h2B.SetBinContent(idom+1, jdom+1, Bij);
 
  312      h2B.SetBinContent(jdom+1, idom+1,-Bij);
 
  313      h2C.SetBinContent(idom+1, jdom+1, Cij);
 
  314      h2C.SetBinContent(jdom+1, idom+1, Cij);
 
  316      h2Bbare.SetBinContent(idom+1, jdom+1, bareBij);
 
  317      h2Bbare.SetBinContent(jdom+1, idom+1, 0);