{
  gROOT->Reset();
  gROOT->ProcessLine(".x lhcbStyle.C");
  gStyle->SetOptTitle(1);
  gStyle->SetOptStat(1);

  TCanvas * c1 = new TCanvas("c1","b0",1200,1200);
  c1->Divide(7,7);

 TCanvas * c2 = new TCanvas("c2","b1",1200,1200);
  c2->Divide(7,7);

TCanvas * c3 = new TCanvas("c3","b2",1200,1200);
  c3->Divide(7,7);

TCanvas * c4 = new TCanvas("c4","b3",1200,1200);
  c4->Divide(7,7);
  // run map in 20 mm steps
  //           y 
  //   0,41,42,43,44,45, 0  
  //  31,15,11, 7,17,23,26
  //  32,14,10, 6,16,22,27
  //x 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 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}};
  TFile * f1 = new TFile("diffout_calib.root","RECREATE");
   TObjArray *myhists = new TObjArray();
  TFile *runfile[49];
  TTree *tree[49];
  TH1D *h1[49];
  TH1D *h2[49];
  TH1D *h3[49];
  TH1D *h4[49];
  TLatex * label[49];
  char histname[10];
  char histtitle[10];

  Int_t counter = 0;
  TDirectory *where = gDirectory;

  for (int i = 0;i<7;i++){
    for (int j = 0;j<7;j++){
      if (fileorder[j][i]==0) {
	counter++;
	continue;
      }
      //cout << counter << endl;
      where->cd();
      sprintf(histname,"h1[%i]",counter);
      sprintf(histtitle,"Run_%i",fileorder[i][j]);

      h1[counter] = new TH1D(histname,histtitle,100,-5,5);
      myhists->AddLast(h1[counter]);
      
      runfile[counter] = new TFile(Form("/work/leverington/beamprofilemonitor/hitdata/HIT_17_12_2017/root/run%i.root",fileorder[i][j]),"");
      tree[counter] = (TTree*)runfile[counter]->Get("t");
      
      
      c1->cd(counter+1);
      where->cd();
      tree[counter]->Project(histname,"beamPosX_b0-beamPosX_b1","beamSignal_b0>7000&&beamSignal_b1>7000");
      h1[counter]->Draw();
      // label[counter]->DrawLatexNDC(0.5,0.95,Form("Run%i",fileorder[i][j]));
      
       
      
      c2->cd(counter+1);
      where->cd();
      sprintf(histname,"h2[%i]",counter);
      h2[counter] = new TH1D(histname,histtitle,100,-5,5);
      myhists->AddLast(h2[counter]);
      tree[counter]->Project(histname,"beamPosX_b2-beamPosX_b3","beamSignal_b2>7000&&beamSignal_b3>7000");
      h2[counter]->Draw();
      //  label[counter]->DrawLatexNDC(0.5,0.95,Form("Run%i",fileorder[i][j]));

      c3->cd(counter+1);
      where->cd();
      sprintf(histname,"h3[%i]",counter);
      h3[counter] = new TH1D(histname,histtitle,100,-5,5);
      myhists->AddLast(h3[counter]);
      tree[counter]->Project(histname,"beamFocusX_b0-beamFocusX_b1","beamSignal_b0>7000&&beamSignal_b1>7000");
      h3[counter]->Draw();
      //  label[counter]->DrawLatexNDC(0.5,0.95,Form("Run%i",fileorder[i][j]));


      c4->cd(counter+1);
      where->cd();
      sprintf(histname,"h4[%i]",counter);
      h4[counter] = new TH1D(histname,histtitle,100,-5,5);
      myhists->AddLast(h4[counter]);
      tree[counter]->Project(histname,"beamFocusX_b2-beamFocusX_b3","beamSignal_b2>7000&&beamSignal_b3>7000");
      h4[counter]->Draw();
      //  label[counter]->DrawLatexNDC(0.5,0.95,Form("Run%i",fileorder[i][j]));
       

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

   myhists->Write();
   // f1->Close();

}

