#include "TROOT.h"
#include "TSystem.h"
#include "TChain.h"
#include "TFile.h"
#include "TString.h"
#include <iostream>

const Char_t *defaultInFile = "st_physics.list";
const Char_t *defaultOutFile = "oMuDstQA.root";

class StFemtoDstPionTnT;
StFemtoDstPionTnT *femtoDstIncSel;


void pau2015QAnia(const Char_t *inFileList = defaultInFile,
                  const Char_t *outFileName = defaultOutFile,
                  const Bool_t  isAuAu = true) {
  std::cout << "**************************************" << std::endl
            << "*                                    *" << std::endl
            << "*  Hello, this is quality assurance  *" << std::endl
            << "*   for the MuDst                    *" << std::endl
            << "*                                    *" << std::endl
            << "**************************************" << std::endl;
  float trackDcaGlobal[2] = {-999., 999.};
  float trackP[2]         = {0.1, 2.1};
  int   trackNHitsFit[2]  = {15, 50};
  float trackEta[2]       = {-1, 1};
  short trackFlag[2]      = {0, 900}; // flag > 0 and < max_short

  //
  // Load libraries
  //
  std::cout << "Loading libraries..." << std::endl;
  gROOT->LoadMacro("$STAR/StRoot/StMuDSTMaker/COMMON/macros/loadSharedLibraries.C");
  loadSharedLibraries();
  gSystem->Load("libMinuit");
  gSystem->Load("StMuDSTMaker");
  gSystem->Load("StChain");
  gSystem->Load("StarClassLibrary");
  gSystem->Load("StMuDstQAMaker");
  std::cout << "Libraries have been successfully loaded" << std::endl;

  //
  // Create chain
  //
  StChain *chain = new StChain("StChain");
  chain->SetDebug(0);
  StMuDebug::setLevel(0);

  //
  // Create MuDst reader
  //
  int nMaxFileToRead = 1e9;
  std::cout << Form("Try to read file: %s", inFileList) << std::endl;
  StMuDstMaker *muDstMaker = new StMuDstMaker(0, 0, "", inFileList, "MuDst", nMaxFileToRead);
  muDstMaker->SetStatus("*",0);
  muDstMaker->SetStatus("Event*",1);
  muDstMaker->SetStatus("MuEvent*",1);
  muDstMaker->SetStatus("PrimaryVertices*",1);
  muDstMaker->SetStatus("PrimaryTracks*",1);
  muDstMaker->SetStatus("GlobalTracks*",1);
  muDstMaker->SetStatus("BTofHeader*",1);
  TChain *currentTree = muDstMaker->chain();
  Long64_t nEvents = currentTree->GetEntries();
  std::cout << Form("Number of events in TTree: %d", nEvents) << std::endl;

  //
  // Create StMuDstQAMaker
  //
  StMuDstQAMaker *QAmaker = new StMuDstQAMaker(muDstMaker, outFileName);

  QAmaker->SetP(trackP[0], trackP[1]);
  QAmaker->SetTrackNHitsFit(trackNHitsFit[0], trackNHitsFit[1]);
  QAmaker->SetTrackEta(trackEta[0], trackEta[1]);
  QAmaker->SetTrackDCA(trackDcaGlobal[0], trackDcaGlobal[1]);
  QAmaker->SetTrackFlag(trackFlag[0], trackFlag[1]);
  // Pion
  QAmaker->SetPionPionNSigma(-2., 2.);
  QAmaker->SetPionKaonNSigma(-3., 3.);
  QAmaker->SetPionProtonNSigma(-3., 3.);
  // Kaon
  QAmaker->SetKaonPionNSigma(-3., 3.);
  QAmaker->SetKaonKaonNSigma(-3., 3.);
  QAmaker->SetKaonProtonNSigma(-3., 3.);
  // Proton
  QAmaker->SetProtonPionNSigma(-3., 3.);
  QAmaker->SetProtonKaonNSigma(-3., 3.);
  QAmaker->SetProtonProtonNSigma(-3., 3.);

  QAmaker->SetPionMassSqr(-0.02, 0.062);
  QAmaker->SetKaonMassSqr(0.2, 0.32);
  QAmaker->SetProtonMassSqr(0.7, 1.1);

  chain->Init();
  Int_t iReturn = 0;
  Int_t nEventsProcessed = 0;
  Float_t percentCounter = 0.01;
  Float_t progress = 0.;

  for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
    progress = (Float_t)iEvent/(Float_t)nEvents;
    nEventsProcessed++;

    if(progress >= percentCounter) {
      percentCounter += 0.01;
      std::cout << Form("Processing progress: %4.2f%%", percentCounter*100.)
          << std::endl;
    }

    chain->Clear();
    iReturn = chain->Make(iEvent);
    if (iReturn != 0) {
      std::cout << "Error has been occured. Event processing has been stopped"
          << std::endl;
      break;
    }
  }

  chain->Finish();

  delete chain;

  std::cout << "*************************" << std::endl
      << "*        Finish         *" << std::endl
      << "*************************" << std::endl;
}
