
#include <iostream>
#include <iomanip>

crystal2apd(const char* filename,// name of the EMC-crystal dat-file [output = filename.apd !! rename it !!]
            double width = 0.04,//width of the APD
            double dist = 3.0,//distance between the front-face of the apd and the endface of the crystal(?)
            int initrownum = 50){//the rownumber will start from here
   // for example: .x crystal2apd.C("barrel_3ring_apd.dat", 0.04, 3.0)


   ifstream ifile(filename);
   if(!ifile.is_open()){
     cout << "[ERROR]: The file " << filename << " cant be opened\n";
     return;
   }

   char ofilename[64];
   sprintf(ofilename, "%s.apd_below", filename);
   ofstream ofile(ofilename);
   if(!ofile.is_open()){
      cout << "[ERROR]: The file " << ofilename << " cant be opened\n";
      return;
   }

   // The first two lines are not numbers
   char module[16], row[16], crystal[16],
      theta[16], phi[16], tau[16],
      posX[16], posY[16], posZ[16],
      pDz[16],      
      pTheta[16], pPhi[16],
      pDy1[16], 
      pDx1[16], pDx2[16],
      pAlp1[16],
      pDy2[16],  pDx3[16],
      pDx4[16],
      pAlp2[16];

   double PosXin, PosYin, PosZin;

   TVector3 Vector;

   int i = 0;

   while(1){
      ifile >> module >> row >> crystal >> theta >> phi 
            >> tau >> posX >> posY >> posZ >> pDz 
            >> pTheta >> pPhi >> pDy1 >> pDx1 >> pDx2 
            >> pAlp1 >> pDy2 >> pDx3 >> pDx4 >> pAlp2;

      // the formatting is little bit primitive, but ... it works ...
      if(TString(module).IsDigit() == false){                                //true, when its a numeral                         
         ofile << setw(10) << module << "\t" << setw(10) << row << "\t" 
               << setw(10) << crystal << "\t" << setw(10) << theta << "\t" 
               << setw(10) << phi << "\t" << setw(10) << tau << "\t" 
               << setw(10) << posX << "\t" << setw(10) << posY << "\t" 
               << setw(10) << posZ << "\t" << setw(10) << pDz << "\t" 
               << setw(10) << pTheta << "\t" << setw(10) << pPhi << "\t" 
               << setw(10) << pDy1 << "\t" << setw(10) << pDx1 << "\t" 
               << setw(10) << pDx2 << "\t" << setw(10) << pAlp1 << "\t" 
               << setw(10) << pDy2 << "\t" << setw(10) << pDx3 << "\t" 
               << setw(10) << pDx4 << "\t" << setw(10) << pAlp2 << endl;
      }
      else
	{

         PosXin = atof(posX);
         PosYin = atof(posY);
         PosZin = atof(posZ);
	 // Row = atof(row);

         Vector = TVector3(PosXin, PosYin, PosZin);

	 //  double mag = Vector.Mag() + 100. + width/2. + dist;   // coordinate transformation above crystals
	 double mag = Vector.Mag() -( 100. + width/2. + dist )-( 100. + width/2. + dist );   // coordinate transformation below crystals

         Vector.SetMag(mag);
         
                             


       


         ofile << setw(10) << module << "\t" << setw(10) << row << "\t" 
               << setw(10) << crystal << "\t" << setw(10) << theta << "\t" 
               << setw(10) << phi << "\t" << setw(10) << tau << "\t" 
               << setw(10) << Vector.X() << "\t" << setw(10) << Vector.Y() << "\t" 
               << setw(10) << Vector.Z() << "\t" << setw(10) << width/2 << "\t" 
               << setw(10) << pTheta << "\t" << setw(10) << pPhi << "\t" 
               << setw(10) << 10 << "\t" << setw(10) << 5 << "\t" 
               << setw(10) << 5 << "\t" << setw(10) << pAlp1 << "\t" 
               << setw(10) << 10 << "\t" << setw(10) << 5 << "\t" 
               << setw(10) << 5 << "\t" << setw(10) << pAlp2 << endl;


	     

	



	}
      if(!ifile.good())
         break;

   }
}

