{

 gROOT->Reset();
   gROOT->ProcessLine(".x lhcbStyle.C");

TCanvas * c1 = new TCanvas("c1","b0",1000,1000);
 c1->Divide(1,7);
Int_t fileorder[7][7] = {{0,41,42,43,44,45,0},{31,15,11,7,17,23,26},{32,14,10,6,16,22,27},{33,3,2,1,4,5,28},{34,18,12,8,20,24,29},{35,19,13,9,21,25,30},{0,36,37,38,39,40,0}};
 Int_t counter = 0;
  TFile *runfile[49];
  TVector * copysumvector_ptr[49];
  TVector * copybeamontime_ptr[49];
  TVector * rowvector_b0_ptr[7];
  TVector * rowvector_b1_ptr[7];
  TVector * rowvector_b2_ptr[7];
  TVector * rowvector_b3_ptr[7];
  TH1F * histo[7];
  TH1F * histo_clone[7];

  TF1 * f1[7];



  // rowvector_b0_ptr->ResizeTo(128);
  // rowvector_b1_ptr->ResizeTo(128); 
  // rowvector_b2_ptr->ResizeTo(128); 
    // rowvector_b3_ptr->ResizeTo(128); 
  for (int i = 0;i<7;i++){
    rowvector_b0_ptr[i] = new TVector(128);
    f1[i] = new TF1(Form("f1[%i]",i),"[0]*(exp(-0.5*(x-[1])^2/(13.7^2))+exp(-0.5*(x-[1]-20/0.8)^2/(13.7^2))+exp(-0.5*(x-[1]-40/0.8)^2/(13.7^2))+exp(-0.5*(x-[1]-60/0.8)^2/(13.7^2))+exp(-0.5*(x-[1]-80/0.8)^2/(13.7^2))  )");
    f1[i]->SetParameters(1E3,20);
  }
  
  for (int i = 0;i<7;i++){
    for (int j = 0;j<7;j++){
      if (fileorder[j][i]==0) {
	counter++;
	continue;
      }
       runfile[counter] = new TFile(Form("/work/leverington/beamprofilemonitor/hitdata/HIT_17_12_2017/root/run%i.root",fileorder[i][j]),"READ");
      runfile[counter]->GetObject("sumvector_b0",copysumvector_ptr[counter]);
      runfile[counter]->GetObject("beamontime",copybeamontime_ptr[counter]);

      //copysumvector_ptr[counter]->Print();
      for (int k = 0; k<128; k++){
	rowvector_b0_ptr[j]->operator[](k) += copysumvector_ptr[counter]->operator[](k)/copybeamontime_ptr[counter]->operator[](1);
      }

	runfile[counter]->Close();
	counter++;
    }
    
    
  }

  TFile * outputfile = new TFile("rowsum.root","RECREATE");
    for (int j = 0;j<7;j++){
      //    rowvector_b0_ptr[j]->Print();

      c1->cd(j+1);
      histo[j] = new TH1F(Form("histo[%i]",j),Form("histo[%i]",j),128,0,128);
      for (int i = 0; i<128;i++){
	histo[j]->SetBinContent(i+1,rowvector_b0_ptr[j]->operator[](i));
      }
      histo[j]->SetMinimum(0);
      histo[j]->GetYaxis()->SetLabelSize(0.1);
      histo[j]->GetXaxis()->SetLabelSize(0.14);
      histo[j]->GetXaxis()->SetNdivisions(15,5,0);
      histo[j]->GetYaxis()->SetNdivisions(10,0,0);
      histo[j]->Draw("");
      // rowvector_b0_ptr[j]->Draw("");
      //     histo[j] = (TH1F*)gDirectory->Get("TVectorF");
     //  histo_clone[j] = (TH1F*)histo[j]->Clone(Form("histoclone[%i]",j));
      histo[j]->Fit(f1[j]);
       histo[j]->Write();
       histo[j]->Fit(f1[j]);
       for (int i = 0; i<128;i++){
	 cout << f1[j]->Eval(i)/histo[j]->GetBinContent(i+1) << ", "  ;
       }

   }
    c1->Update();
    c1->Write();


TCanvas * c2 = new TCanvas("c2","b0",1000,1000);
 c2->Divide(1,7);
for (int j = 0;j<7;j++){
      //    rowvector_b0_ptr[j]->Print();

      c2->cd(j+1);
      histo_clone[j] = (TH1F*)histo[j]->Clone(Form("histo_clone[%i]",j));

      histo_clone[j]->Divide(histo[6]);
      histo_clone[j]->GetYaxis()->SetLabelSize(0.1);
      histo_clone[j]->GetXaxis()->SetLabelSize(0.14);
      histo_clone[j]->GetXaxis()->SetNdivisions(15,5,0);
      histo_clone[j]->GetYaxis()->SetNdivisions(10,0,0);
      histo_clone[j]->SetMinimum(0);
      histo_clone[j]->SetMaximum(1.0);

      histo_clone[j]->Draw();
      
 }

    outputfile->Write();
    //   outputfile->Close();
}
