Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
writeTimeOrderedDistortions.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file writeTimeOrderedDistortions.C
1 //basic framework to read and compare events in a time ordered distortion file
2 //these files contain a TTree with xingnum, and three distortion map branches
3 #include "TTree.h" //this prevents a lazy binding issue and/or is a magic spell.
4 #include "TCanvas.h" //this prevents a lazy binding issue and/or is a magic spell.
5 #include "TH3F.h"
6 #include "TH2F.h"
7 #include "TH1F.h"
8 
9 void writeTimeOrderedDistortions(bool subtractFirst=false, char *filename="/sphenix/user/rcorliss/distortion_maps/2022.07/TimeOrderedDistortions.root", char *inputpattern="/sphenix/user/rcorliss/distortion_maps/2022.07/*.distortion_map.hist.root", bool debug=false){
10 
11  TFile *treefile=TFile::Open(filename,"RECREATE");
12  //TTree *tree=(TTree*)(file->Get("TimeDists"));
13 
14  int nhists=8;
15  TH3F *basehist[nhists];
16  TH3F *temphist[nhists];
17  int xingnum=0;
18  //note that we match the histogram RPhi to the branch P, and the histogram P to the branch Phi, to maintain backwards compatibility with older sets... for now.
19  std::string branchname[]={"hIntDistortionP_negz",
20  "hIntDistortionPhi_negz",
21  "hIntDistortionR_negz",
22  "hIntDistortionZ_negz",
23  "hIntDistortionP_posz",
24  "hIntDistortionPhi_posz",
25  "hIntDistortionR_posz",
26  "hIntDistortionZ_posz"};
27  std::string histname[]={"hIntDistortionRPhi_negz",
28  "hIntDistortionP_negz",
29  "hIntDistortionR_negz",
30  "hIntDistortionZ_negz",
31  "hIntDistortionRPhi_posz",
32  "hIntDistortionP_posz",
33  "hIntDistortionR_posz",
34  "hIntDistortionZ_posz"};
35  TTree *tree=new TTree("TimeDists", "TimeDists");
36  tree->Branch("xingnum",&xingnum);
37  for (int i=0;i<nhists;i++){
38  temphist[i]=new TH3F(Form("temphist%d",i),Form("temphist%d",i),10,0,10,20,0,20,30,0,30);
39  basehist[i]=new TH3F(Form("basehist%d",i),Form("basehist%d",i),10,0,10,20,0,20,30,0,30);
40  tree->Branch(branchname[i].c_str(),&(temphist[i]));
41  }
42  if (debug) printf("histograms built and branched.\n");
43 
44  //find all files that match the input string
45  TFileCollection *filelist=new TFileCollection();
46  filelist->Add(inputpattern);
47  filelist->Print();
48  printf("found %d files like: %s\n",filelist->GetNFiles(),((TFileInfo*)(filelist->GetList()->At(0)))->GetCurrentUrl()->GetFile());//Title());//Print();
49 
50  //return;
51  TFile *infile;
52  bool fileIsValid=true;
53  bool isFirst=true;
54  int nMaps=0;
55 
56 
57  for (int i=0;i<filelist->GetNFiles() && i<100;i++){
58  //for each file, find all histograms in that file.
59  infile=TFile::Open(((TFileInfo*)(filelist->GetList()->At(i)))->GetCurrentUrl()->GetUrl(),"READ");//gross.
60  fileIsValid=true;
61  if (debug) printf("=====> Trying File %d\n",i);
62  if (!infile->IsOpen()) {
63  printf("=====> File %d is NOT openable <=======\n",i);
64  continue; //file didn't open right. move on to the next one.
65  }
66  //TList *keys=infile->GetListOfKeys();
67  for (int j=0;j<nhists;j++){
68  temphist[j]=NULL;
69  temphist[j]=infile->Get<TH3F>(histname[j].c_str());
70  if (!temphist[j]){
71  fileIsValid=false; //histogram doesn't exist. don't bother loading the other hists.
72  break;
73  }
74  int nbins=temphist[j]->GetNcells();
75  if (debug) printf("=======> \"%s\" has %d cells\n",histname[j].c_str(),nbins);
76 
77  }
78  if (!fileIsValid) {
79  infile->Close();
80  printf("=====> File %d is NOT valid <=======\n",i);
81 
82  continue; //didn't get all our hists. move on to the next file.
83  }
84  if (debug) printf("=====> File %d is valid\n",i);
85  xingnum=i;//temporary fix to paste something in there.
86  if(subtractFirst){
87  if (isFirst){
88  for (int j=0;j<nhists;j++){
89  treefile->cd();
90  basehist[j]=(TH3F*)(temphist[j]->Clone());
91  //temphist[j]->Copy(*(basehist[j]));
92  }
93  isFirst=false;
94  }
95  for (int j=0;j<nhists;j++){
96  printf("ptr j=%d: b:%p\tt:%p\n",j,basehist[j],temphist[j]);
97  int nbins=temphist[j]->GetNcells();
98  for (int k=0;k<nbins;k++){
99  double b=basehist[j]->GetBinContent(k);
100  double t=temphist[j]->GetBinContent(k);
101  double diff=t-b;
102  temphist[j]->SetBinContent(k,diff);
103  //temphist[j]->Add(basehist[j],-1);
104  }
105  if (debug) printf("=======> \"%s\" has %d cells when writing diff\n",histname[j].c_str(),nbins);
106 
107  }
108  }
109 
110  tree->Fill();
111  nMaps++;
112  infile->Close();
113  }
114  printf("finished tree %s has nMaps=%d\n",filename, nMaps);
115 
116  treefile->cd();
117  tree->Write();
118  treefile->Close();
119  return;
120 }