/* ======================================================================== *\
!
! *
! * This file is part of MARS, the MAGIC Analysis and Reconstruction
! * Software. It is distributed to you in the hope that it can be a useful
! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
! * It is distributed WITHOUT ANY WARRANTY.
! *
! * Permission to use, copy, modify and distribute this software and its
! * documentation for any purpose is hereby granted without fee,
! * provided that the above copyright notice appear in all copies and
! * that both that copyright notice and this permission notice appear
! * in supporting documentation. It is provided "as is" without express
! * or implied warranty.
! *
!
!
!   Author(s): Javier López, 04/2004 <mailto:jlopez@ifae.es>
!
!   Copyright: MAGIC Software Development, 2000-2004
!
!
\* ======================================================================== */


Bool_t HandleInput()
{
    TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
    while (1)
    {
        //
        // While reading the input process gui events asynchronously
        //
        timer.TurnOn();
        TString input = Getline("Type 'q' to exit, <return> to go on: ");
        timer.TurnOff();

        if (input=="q\n")
            return kFALSE;

        if (input=="\n")
            return kTRUE;
    };

    return kFALSE;
}


void hvnotnominal(const TString filename="20040319_20821_D_Mrk421_S.root", const TString directory="/nfs/magic/CaCodata/2004_03_19/", Float_t percent = 0.01)
{

  //
  // Create a empty Parameter List and an empty Task List
  // The tasklist is identified in the eventloop by its name
  //
  MParList  plist;
  
  MTaskList tlist;
  plist.AddToList(&tlist);


  MGeomCamMagic geomcam;
  MBadPixelsCam badcam;
  MCameraHV     hvcam;

  // this line is needed because the CC rep rootified file don't have run header,
  // then the MGeomApply can not initialitaze the containers with the number of 
  // pixels because this is done in the reinit().
  badcam.InitSize(geomcam.GetNumPixels());
  
  plist.AddToList(&geomcam);
  plist.AddToList(&badcam);
  plist.AddToList(&hvcam);

  //
  // Now setup the tasks and tasklist:
  // ---------------------------------
  //

  // Reads the trees of the root file and the analysed branches
  MReadReports read;
  read.AddTree("Camera"); 
  read.AddFile(directory+filename);     // after the reading of the trees!!!
  read.AddToBranchList("MReportCamera.*");

  MGeomApply geomapl;
  MFHVNotNominal fHVNominal;
  TString hvnominal = "/mnt/users/jlopez/Mars/Files4Mars/Config/HVSettings_FF35q.conf";
  fHVNominal.SetHVNominalValues(hvnominal);
  //  fHVNominal.SetMaxNumPixelsDeviated(10);
  MContinue cont_hv(&fHVNominal);

  tlist.AddToList(&read);
  tlist.AddToList(&geomapl);
  tlist.AddToList(&cont_hv);

  //
  // Create and setup the eventloop
  //
  MEvtLoop evtloop;
  evtloop.SetParList(&plist);
     
  //
  // Execute your analysis
  //

  MHCamera hvnotnominal(geomcam);
  MHCamera lowhvnotnominal(geomcam);
  MHCamera uphvnotnominal(geomcam);

  if (!evtloop.PreProcess())
    return;

  TArrayD nominal = fHVNominal.GetHVNominal();
  TArrayD lownominal = nominal;
  TArrayD upnominal = nominal;
  
  for (UInt_t pix=0; pix<nominal.GetSize(); pix++)
    {
      lownominal[pix] *= (1-percent);
      upnominal[pix]  *= (1+percent);
    }
  
  
  while(tlist.Process())
    {
      
      hvnotnominal.CntCamContent(hvcam,lownominal,0,kFALSE);
      hvnotnominal.CntCamContent(hvcam,upnominal,0,kTRUE);
      
      lowhvnotnominal.CntCamContent(hvcam,lownominal,0,kFALSE);
      uphvnotnominal.CntCamContent(hvcam,upnominal,0,kTRUE);
    }
  

  evtloop.PostProcess();
  
  tlist.PrintStatistics();
  
  TCanvas c1;
  c1.Divide(2,1);
  c1.cd(1);
  lowhvnotnominal.SetPrettyPalette();
  lowhvnotnominal.Draw();
  gPad->cd(1);
  gPad->Modified();
  gPad->Update();
  c1.cd(2);
  uphvnotnominal.SetPrettyPalette();
  uphvnotnominal.Draw();
  gPad->cd(2);
  gPad->Modified();
  gPad->Update();

  TCanvas c2;
  hvnotnominal.SetPrettyPalette();
  hvnotnominal.Draw();
  gPad->cd(1);
  gPad->Modified();
  gPad->Update();

  if (!HandleInput())
    break;

}


