TreeTest.C 2.94 KB
Newer Older
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


TFile* TPfile = new TFile("TP.root","update"); // Declared globally coz TPfile is closed in ain function after going through all the pixels
TList* TPlist = new TList;

void treeWrite(int c, int r){

  int TPcol1, TPcol2, TPcol3;
  double tot, tot_var, toa, toa_var,Ecol1,Ecol2,Ecol3,Ecol4;
  string line_TP;
  
  std::vector<double>Energy_e = {200,300,400,500,600,700,800,900,1000,1100,1200,
                                1300,1400,1500,1700,2200,2700,3200,4000,5000,6000,
				7000,8000,9000,10000,11000,12000,13000,14000};
  
  //Creating TProot file with a TP tree
  
  //  TFile* TPfile = new TFile("TP.root","update");
  std::string TPtreeName = "TPtree" + std::to_string(c) + "_" + std::to_string(r); 
  TTree* TPtree = new TTree(TPtreeName.c_str()," TP tree");
  // TList* list = new TList;

  
  TPtree->Branch("TPcol1",&TPcol1,"TPcol1/I");
  TPtree->Branch("TPcol2",&TPcol2,"TPcol2/I");
  TPtree->Branch("TPcol3",&TPcol3,"TPcol3/I");
  TPtree->Branch("tot",&tot,"tot/D");
  TPtree->Branch("tot_var",&tot_var,"tot_var/D");
  TPtree->Branch("toa",&toa,"toa/D");
  TPtree->Branch("toa_var",&toa_var,"toa_var/D");

  //Creating second root file(Energy_table.dat) with a different tree
  
  /* TFile* friend = new TFile("TPtreeFriend.root","recreate");
     TTree* Frirendtree = new TTree("Friendtree","friend tree");
     Friendtree->Branch("Ecol4",&Ecol4,"Ecol4/D");*/

  //Filling the trees

  cout << "The tree is filled with values of col " << c << " and row "<< r << std::endl;
  
  for(int i= 0; i < Energy_e.size(); i++){
    stringstream Sfile;
    Sfile << Energy_e.at(i) << ".dat";
    std::vector<std::string>Filenames = {std::string("tp_dat_" + Sfile.str())};

    for(int j = 0; j < Filenames.size(); j++){ 
      std::ifstream TPdata_file(Filenames.at(j).c_str());
      
      if(TPdata_file.is_open()){
	std::cout << " The data file" <<  Filenames.at(j) << " is opened ..." <<std::endl;
	
	while(getline(TPdata_file,line_TP)){
	  if(line_TP.rfind("#", 0) == 0) {
	    continue;
	  }
	  istringstream ssTP(line_TP);
	  ssTP >> TPcol1 >> TPcol2 >> TPcol3 >> tot >> tot_var >> toa >> toa_var;
	  if(TPcol1==c && TPcol2 ==r){
	    TPtree->Fill();
	    break;
	  }
	}  //While loop getline
      }   //if statement TPdatafile open
    }    // for loop j filenames
  }     // if statement energy_e.size()

  // TPtree->Scan(); //Displays the data corresponding to all energy values of each pixel 
  TPlist->Add(TPtree);
  // list->Print(); //Prints the contents of the list
  // std::cout <<" The size of the tree list is " << list->GetSize() << std::endl; // list size = 1, not growing!! 
  TTree* newtree = TTree::MergeTrees(TPlist); 
  newtree->SetName("MergedTree");
  newtree->Write("",TObject::kWriteDelete);
  //TPfile->Close();
}


void TreeTest(){
  
  int col, row ;
  
  for(col=0;col<2;col++){
    for(row=0;row<2;row++){ 
      treeWrite(col,row);
    }
  }

  std::cout <<" The size of the tree list is " << TPlist->GetSize() << std::endl;
  TPfile->Close();
  //  treeRead();

}