#include <TFile.h>
#include "TROOT.h"
#include "TSystem.h"
#include "TChain.h"
#include "TTree.h"

//_____________________
void FemtoDstQA_pp200_y2015(const char *flist = "test.list",
                            const char *ofile = "femtoQA_pp200_y2015.root") {
  std::cout << "Input file list = " << flist << '\n';
  if (!flist) {
    std::cout << "Input file list = NULL!\n";
    return;
  }

  //
  // Load libraries
  //
  gROOT->LoadMacro("$STAR/StRoot/StMuDSTMaker/COMMON/macros/loadSharedLibraries.C");
  loadSharedLibraries();
  gSystem->Load("libMinuit");
  gSystem->Load("StMuDSTMaker");
  gSystem->Load("StChain");
  gSystem->Load("StRefMultCorr");
  gSystem->Load("StFlowMaker");
  gSystem->Load("StFlowAnalysisMaker");
  gSystem->Load("StHbtMaker");
  gSystem->Load("StPicoDstMakerRun11");
  gSystem->Load("StFemtoDstMaker");

  StChain           *chain = 0;
  StFemtoDstQAMaker *maker = 0;

  //
  // List of member links in the chain
  //
  chain = new StChain("StChain");
  maker = new StFemtoDstQAMaker("", flist, ".", 22000);
  maker->SetOutFileName(ofile);
  maker->SetRunIdParameters(997033, 15339001, 16336024); // pp200 - Run16
  maker->AddTrigId(480006); // trigger selection for pp200 y2015 P16id VPDMB
  maker->AddTrigId(470001);
  maker->AddTrigId(470004);
  maker->AddTrigId(470007);
  maker->AddTrigId(470014);
  maker->AddTrigId(470017);
  maker->AddTrigId(470904);
  maker->AddTrigId(470914);
  maker->AddTrigId(490004);
  maker->AddTrigId(490007);
  maker->AddTrigId(490904);
  maker->AddTrigId(470401); // JP2
  maker->AddTrigId(470402); // JP2-bsmd
  maker->AddTrigId(470404); // JP1
  maker->AddTrigId(470201); // BHT0*VPDMB-30
  maker->AddTrigId(470202); // BHT1*VPDMB-30
  maker->AddTrigId(470206); // BHT1*VPDMB-30-nobsmd
  maker->AddTrigId(470211); // BHT0*VPDMB-5
  maker->AddTrigId(480004); // VPDMB-novtx
  maker->AddTrigId(480007); // VPDMB-5-trgonly
  maker->AddTrigId(480904); // VPDMB-30
  maker->AddTrigId(480401); // JP2
  maker->AddTrigId(480404); // JP1
  maker->AddTrigId(480411); // JP2
  maker->AddTrigId(480414); // JP1
  maker->AddTrigId(480201); // BHT0*VPDMB-5
  maker->AddTrigId(480202); // BHT1*VPDMB-30

  //
  // Turn on only min-bias triggers
  //
  maker->SetTrigger(480006, 1);
  maker->SetTrigger(470001, 1);
  maker->SetTrigger(470004, 1);
  maker->SetTrigger(470007, 1);
  maker->SetTrigger(470014, 1);
  maker->SetTrigger(470017, 1);
  maker->SetTrigger(470904, 1);
  maker->SetTrigger(470914, 1);
  maker->SetTrigger(490004, 1);
  maker->SetTrigger(490007, 1);
  maker->SetTrigger(490904, 1);
  maker->SetTrigger(480004, 1);
  maker->SetTrigger(480007, 1);
  maker->SetTrigger(480904, 1);

  chain->Init();

  //
  // Loop over the links in the chain
  //
  int iret = 0;
  int nEvents = maker->GetNEvents();
  std::cout << "Entries in chain = " << nEvents << std::endl;

  for(int iev = 0; iev < nEvents; iev++) {
    chain->Clear();
    iret = chain->Make(iev);
    if(iret) {
      std::cout << "Error in Make occured. Error code: " << iret << std::endl;
      std::cout << "iev = " << iev << '\n';
      break;
    } 
  }
  chain->Finish();
  //Cleanup
  delete maker;
  delete chain;
}
