Mapping a file and display the output

I am trying to read a .txt file and one of the branch from a .root file, I would like to display for each energy values the corresponding chip and channel. The txt file contains chip, channel and i j k values and the root file has tree with ahc_hitEnergy as one of the branch. My script gives i j k values for each energy but not the corresponding chip and channel.
Kindly do give me corrections in my script.
Thank you for your time and consideration.
Dorothea


#include <iostream>
#include <vector>
#include <map>
#include "TFile.h"
#include "TTree.h"
#include "TChain.h"
#include "TH1.h"
#include "TH2.h"
#include <fstream>
#include "TCanvas.h"
#include "TStyle.h"
#include "TMath.h"
#include "TString.h"
//#include "TSytem.h"
#include "TSystemDirectory.h"
#include <stdlib.h>
#include <sstream>
#include "TFitResult.h"
#include "TF1.h"
#include "TGraphErrors.h"
#include "TMultiGraph.h"
#include "TLegend.h"
#include "TAxis.h"
#include <TProfile.h>
#include <algorithm>
#include "math.h"

using namespace std;



void test_macro(){


TFile* outputf = new TFile("outputtest.root","RECREATE");


TH1D *hist1[25][25][3];

char name[30], title[70];

for(int i =0; i< 25; i++){
for(int j=0; j< 25; j++){
for(int k=0; k< 3; k++){

sprintf(title," ",i,j,k);
sprintf(name,"energyDistribution_I_%d_J_%d_K_%d",i+1,j+1,k+1);
hist1[i][j][k]=new TH1D(name, "Energy Distribution", 600, 0,600);
hist1[i][j][k]->SetXTitle("Hit Energy[ADC]");
hist1[i][j][k]->SetYTitle("# of entires");
}
}
}


// To have the mapping

/*

map<int,int> mapChipChnIJK;

void MakeChipChnIJKMap()
{

string line;
ifstream myfile ("/nfs/dust/ilc/user/opinto/Cosmics/Cosmics/MapChipChan2IJK_CosmicNov2017.txt");

if (myfile.is_open())
{
while ( getline (myfile,line) )
{
//cout << line << '\n';
if(line[0] == '#') continue;
std::istringstream iss(line);

int Layer;
int Chip;
int Chn;
int I;
int J;
int K;

iss >> Layer >> Chip >> Chn >> I >> J >> K;

int indexChipChn = Chip*100 + Chn;
int indexIJK = I + J*100 + K*10000;

mapChipChnIJK.insert(make_pair(indexIJK,indexChipChn));
}
cout << "Map HBU done" << endl;

myfile.close();
}

else{
cout << "Unable to open file" << endl;
}
}

*/

cout << " before input file " << endl;
TFile *inputf = TFile::Open("test.root");

cout << " input file declared " << endl;

TTree* bigtree = (TTree*) inputf->Get("bigtree");
//const int nchn = 10000;

/* Float_t hitEnergy[nchn];
Int_t nhits;
Int_t hitI[nchn];
Int_t hitJ[nchn];
Int_t hitK[nchn];
*/
//Int_t runNumber;
//Int_t eventNumber;
//Long64_t eventTime;
//Int_t ahc_iEvt;
Int_t ahc_nHits;

Int_t ahc_hitI[178]; //[ahc_nHits]
Int_t ahc_hitJ[178]; //[ahc_nHits]
Int_t ahc_hitK[178]; //[ahc_nHits]
Float_t ahc_hitEnergy[178]; //[ahc_nHits]
// Float_t ahc_hitTime[178]; //[ahc_nHits]
//Int_t ahc_hitType[178]; //[ahc_nHits]

// List of branches
//TBranch *b_runNumber; //!
//TBranch *b_eventNumber; //!
//TBranch *b_eventTime; //!
//TBranch *b_ahc_iEvt; //!
TBranch *b_ahc_nHits; //!
TBranch *b_ahc_hitI; //!
TBranch *b_ahc_hitJ; //!
TBranch *b_ahc_hitK; //!
TBranch *b_ahc_hitEnergy; //!
//TBranch *b_ahc_hitTime; //!
//TBranch *b_ahc_hitType; //!


bigtree->SetBranchStatus("*",0);

bigtree->SetBranchStatus("ahc_hitEnergy",1);
bigtree->SetBranchStatus("ahc_nHits",1);
bigtree->SetBranchStatus("ahc_hitI",1);
bigtree->SetBranchStatus("ahc_hitJ",1);
bigtree->SetBranchStatus("ahc_hitK",1);

bigtree->SetBranchAddress("ahc_hitEnergy",&ahc_hitEnergy);
bigtree->SetBranchAddress("ahc_nHits",&ahc_nHits);
bigtree->SetBranchAddress("ahc_hitI",&ahc_hitI);
bigtree->SetBranchAddress("ahc_hitJ",&ahc_hitJ);
bigtree->SetBranchAddress("ahc_hitK",&ahc_hitK);

outputf->cd();


for(int i=0;i<bigtree->GetEntries();i++)
{
bigtree->GetEntry(i);

int nHits = ahc_nHits;
// int Chn;
//int Chip;

for(int ee = 0; ee < ahc_nHits; ee++)
{
// to have IJK ->chip,chn
/*
if(it != mapChipChnIJK.end())
{

int value = it->second;
Chn = (int)value%100;
Chip = (int)value/100;
}

*/

float energy = ahc_hitEnergy[ee];

cout <<"Energy:"<< energy << endl;
int Ivalue = ahc_hitI[ee];
int Jvalue = ahc_hitJ[ee];
int Kvalue = ahc_hitK[ee];

cout << Ivalue << " " << Jvalue << " " << Kvalue << endl;

hist1[Ivalue-1][Jvalue-1][Kvalue-1]->Fill(energy);
}



}// end entries


for(int i =0; i< 25; i++){
for(int j=0; j< 25; j++){
for(int k=0; k< 3; k++){
hist1[i][j][k]->Write();
}
}
}

outputf->Close();



}// END TEST MACRO



int main(){

test_macro();

// MakeChipChnIJKMap();
}

Last edited on
Topic archived. No new replies allowed.