#include "data_types.h"
#include "cluster_reader.h"
#include <iostream>

#define MAXPH 100000000
using namespace std;

int main(int argc, char* argv[]) {

  int nph, nframes;
  int ok;
  Cluster *clust=new Cluster[MAXPH];
  //Cluster c;
  cout << "Clusters allocated " << sizeof(clust) << endl;
  FILE *fp=fopen("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust","rb");
  //cpp_open_cluster_file("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust",ok);
  cout << "File opened " << endl;
  int nr,ntot;
  int nleft=0;
  while (nr =read_clusters(fp,MAXPH, clust+ntot,&nleft)){
    ntot+=nr;
    printf("** %d ",nr);
  }
  // cpp_read_clusters(clust, nph,nframes,ok);
  cout << "Clusters read " << endl;
  //cpp_close_cluster_file(ok);
  fclose(fp);
  cout << "File closed " << endl;
  int iph=0; 
  printf("[");
  for (int iph=0; iph<ntot; iph+=100000){
  //   int tot=0;
  //   for (int iy=0; iy<3; iy++) {
  //     for (int ix=0; ix<3; ix++) 
  // 	tot+=(clust+iph)->data[iy*3+ix];
  // 	//std::cout << cl.data[iy*3+ix] << "\t";
  //     // std::cout << endl;
  //   }
    
  //   // if (tot>500)
  //   //   std::cout << iph << "\t" << (clust+iph)->x << "\t" << (clust+iph)->y << "\t" << tot << endl;
    printf("(");
    printf("%3d, ",(clust+iph)->x);
    printf("%3d, ",(clust+iph)->y);
 
    printf("[");
    // printf("\n \t");
    for (int iy=0; iy<9; iy++) {
	printf("%3d",(clust+iph)->data[iy]);
	if (iy<8)printf(", "); 
      //printf("\n \t");
    }
    printf("]");
    printf(")");
    printf("\n");
  }
  printf("]");
  cout << ntot <<endl;

  return 0;
}
