#include <stdio.h>
#include <TH1F.h>
#include <math.h>
#include <TFile.h>


#define BFSZ 0xFF


void oscar97QA(const char *inF19     = "f19.list",
	       const char *oFileName = "o97QA.root") {
  //
  // Creating histograms and output file
  //
  TFile *oFile = new TFile(oFileName, "RECREATE");
  
  TH1F *hImpPar = new TH1F("hImpPar",
			   "impact parameter;imp (fm/c);#",
			   1700, 0., 17.);
  TH1F *hRefMult = new TH1F("hRefMult",
			    "reference multiplicity;RefMult;#",
			    10000, 0., 10000.);
  TH1F *hRefMultA = new TH1F("hRefMultA",
			     "reference multiplicity (p_{t}>0.09 (GeV/c)"
			     "and |#eta|<1.0);RefMult;#",
			     10000, 0., 10000.);
  TH1F *hPt = new TH1F("hPt",
		       ";p_{t} (GeV/c);#",
		       1000, 0., 3.0);
  TH2F *hM2vsPt = new TH2F("hM2vsPt",
			   ";p_{t}*Q (GeV/c);m^{2} (GeV^{2}/c^{4})",
			   1000, -3., 3.,
			   1000, 0., 2.);
  TH1F *hEta = new TH1F("hEta",
			";#eta;#",
			1000, -5, 5.);
  
  FILE *fList = fopen(inF19, "r");

  char buf[BFSZ] = {0};

  //
  // Read data
  //
  int evNum, nTracks;
  float imp, planeRot;
  int trkRd, refMultA;
  bool readEvent = true;
  while (!feof(fList)) {
    fgets(buf, BFSZ, fList);
    buf[strlen(buf) - 1] = '\0';
    printf("File: %s\n", buf);
    FILE *f19 = fopen(buf, "r");
    if (!f19) {
      printf("f19 == NULL\n");
      break;
    }
    //
    // skip three useless lines
    //
    fgets(buf, BFSZ, f19);
    fgets(buf, BFSZ, f19);
    fgets(buf, BFSZ, f19);
    //
    // Read events and tracks
    //
    while (!feof(f19)) {
      if (readEvent) {
	int ret = fscanf(f19, "%i %i %f %f", &evNum, &nTracks, &imp, &planeRot);
	if (ret == 4) {
	  /*
	  printf("EVENT INFO:\n\t ID = %i, NTRACKS = %i, IMP =  %f, PLANEROT = %f\n",
		 evNum, nTracks, imp, planeRot);
	  */
	  hImpPar->Fill(imp);
	  hRefMult->Fill(nTracks);
	  trkRd = 0;
	  refMultA = 0;
	  readEvent = false;
	}
	else // if can't read something like event, skip that line
	  fgets(buf, BFSZ, f19);
      }
      else {
	//
	// Read tracks from an event
	//
	int trkId, trkPdg;
	float trkPx, trkPy, trkPz, trkE, trkM,
	  trkXfr, trkYfr, trkZfr, trkTfr,
	  trkPt, trkP, trkEta;
	int ret1 = fscanf(f19,
			  "%i %i %f %f %f %f",
			  &trkId, &trkPdg, &trkPx, &trkPy, &trkPz, &trkE);
	int ret2 = fscanf(f19,
			  "%f %f %f %f %f",
			  &trkM, &trkXfr, &trkYfr, &trkZfr, &trkTfr);
	if (ret1 + ret2 == 11) {
	  char charge = trkPdg/abs(trkPdg);
	  trkP   = sqrt(trkPx*trkPx + trkPy*trkPy + trkPz*trkPz);
	  trkPt  = sqrt(trkPx*trkPx + trkPy*trkPy);
	  trkEta = 0.5*log((trkP + trkPz)/(trkP - trkPz));
	  hPt->Fill(trkPt);
	  hEta->Fill(trkEta);
	  hM2vsPt->Fill(trkPt*charge, trkM*trkM);
	  if (trkPt > 0.9 && trkEta < 1.)    refMultA++;
	  trkRd++;
	  if (trkRd == nTracks)    readEvent = true;
	}
	else // if can't read something like track, skip that line
	  fgets(buf, BFSZ, f19);
      }
    }
    fclose(f19);
  }
  
  hImpPar->Write();
  hRefMult->Write();
  hRefMultA->Write();
  hPt->Write();
  hM2vsPt->Write();
  hEta->Write();
  oFile->Close();
  
  fclose(fList);
}
