TreeTest.C 3.46 KB
Newer Older
1
//TList* TPlist = new TList;
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
2

3
void TreeEnergyWrite(){
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
4
5


6
7
8
9
  int Ecol1;
  double Ecol2,Ecol3,Ecol4;
  string line_E;
   
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
10
  
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  TFile* Efile = new TFile("Energy_table.root","recreate");
  TTree* ETree = new TTree("ETree","Energy tree");

  ETree->Branch("Ecol1",&Ecol1,"Ecol1/I"); // ECol1 = Energy values = {200,300.....14000}
  ETree->Branch("Ecol4",&Ecol4,"Ecol4/D"); // Voltage values 

  std::ifstream Energy_file("Energy_table.dat");
  if(Energy_file.is_open()){
    while(getline(Energy_file,line_E)){
      istringstream ssE(line_E);
      ssE >> Ecol1 >> Ecol2 >> Ecol3 >> Ecol4;
      ETree->Fill();
    }
  }

  ETree->Write();
  Efile->Close();
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
28
  
29
30
31
32
33
34
35
36
37
38
39
40
}

void TreeTPWrite(int c, int r){

  std::cout << " Looking at pixel " << c << "_" << r << std::endl; 
  int energy, TPcol1, TPcol2, TPcol3;
  double voltage,tot, tot_var, toa, toa_var;
  string line_TP;

  TFile* Efile = new TFile("Energy_table.root");
  TTree* ETree = (TTree*)Efile->Get("ETree");

Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
41
  
42
  TFile* TPfile = new TFile("TP.root","update"); 
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
43
44
  std::string TPtreeName = "TPtree" + std::to_string(c) + "_" + std::to_string(r); 
  TTree* TPtree = new TTree(TPtreeName.c_str()," TP tree");
45
46
  TTree* Mergedtree = new TTree("MergedTree", "Merged trees");
  Mergedtree->SetDirectory(TPfile);
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
47
48
49
50
51
52
53
54
55
  
  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");

56
57
  ETree->SetBranchAddress("Ecol1",&energy);
  ETree->SetBranchAddress("Ecol4",&voltage);
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
58

59
60
61
62
63
64
  for(int i = 0; i < ETree->GetEntries(); i++){
    ETree->GetEntry(i);
    // std:: cout << energy << " " << voltage << std::endl;
    std::string TPfilename = "tp_dat_" + std::to_string(energy) + ".dat";
    //std:: cout << " The TPfilenames are : " <<  TPfilename.c_str() <<std::endl;
    std::ifstream TPdatafile(TPfilename.c_str());
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
65

66
67
68
69
70
71
72
73
74
75
76
77
78
    if(TPdatafile.is_open()){

      std::cout << " The data file " << TPfilename.c_str() << " is opened ..." <<std::endl;

      // make a new branch and connect it to the same variable as the branch address of the energy tree
        auto voltageBranch = TPtree->Branch("voltage", &voltage, "voltage/D");

      // reading through individial tp_dat_ files

      while(getline(TPdatafile,line_TP)){
	if(line_TP.rfind("#", 0) == 0) {
	  continue;
	}
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
79
	
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	istringstream ssTP(line_TP);
	ssTP >> TPcol1 >> TPcol2 >> TPcol3 >> tot >> tot_var >> toa >> toa_var;
	if(TPcol1==c && TPcol2 ==r){
	  TPtree->Fill();
	  voltageBranch->Fill();
	  break;
	}
      } //While loop getline
    }//if statement TPdatafile open
  }//For loop GetEntries

  //Save only the new version of the tree 

  // TPtree->Write("",TObject::kOverwrite);
  // TPtree->Write("",TObject::kWriteDelete);
  TPtree->Write();
  //  TPtree->Scan();

  //Add new trees (incl voltage col) to list and merge the trees
  /* std::cout << " The tree for the pixel " <<  c <<  " and " << r << " is added to the list " << std::endl;
  TList* TPlist = new TList;
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
101
  TPlist->Add(TPtree);
102
103
  Mergedtree = TTree::MergeTrees(TPlist); 
  Mergedtree->Write("",TObject::kWriteDelete);*/
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
104

105
106
107
  TPfile->Close();
  //Efile->Close();
}
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
108
109

void TreeTest(){
110
111

  int col, row ; 
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
112
  
113
114
  TreeEnergyWrite();

Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
115
116
  for(col=0;col<2;col++){
    for(row=0;row<2;row++){ 
117
      TreeTPWrite(col,row);
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
118
119
120
    }
  }

121
122
  // std::cout <<" The size of the tree list is " << TPlist->GetSize() << std::endl;
  //TPfile->Close();
Keerthi Nakkalil's avatar
Keerthi Nakkalil committed
123
124

}