Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
wholeIter_Pi0EtaByEta.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file wholeIter_Pi0EtaByEta.C
1 #pragma once
2 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,0)
3 #include <fun4all/SubsysReco.h>
9 //#include <rawwaveformtowerbuilder/RawWaveformTowerBuilder.h>
11 #include <cdbobjects/CDBTTree.h> // for CDBTTree
13 
14 //#include <litecaloeval/LiteCaloEval.h>
15 #include <caloreco/CaloTowerCalib.h>
16 #include <caloreco/RawClusterBuilderTemplate.h>
17 #include <calib_emc_pi0/CaloCalibEmc_Pi0.h>
18 
19 #include <phool/recoConsts.h>
20 
21 #include <fstream>
22 #include <string>
23 #include <TBranch.h>
24 #include <TLeaf.h>
25 #include <fstream>
26 #include <TChain.h>
27 #include <TTree.h>
28 
29 
30 R__LOAD_LIBRARY(libfun4all.so)
31 R__LOAD_LIBRARY(libfun4allraw.so)
32 R__LOAD_LIBRARY(libcdbobjects)
33 R__LOAD_LIBRARY(libcalo_reco.so)
34 //R__LOAD_LIBRARY(libLiteCaloEvalTowSlope.so)
35 R__LOAD_LIBRARY(libcalibCaloEmc_pi0.so)
36 
37 
38 #endif
39 
40 void make_csv(const char * inf, const char * outf);
41 TTree * GetTChainMacro(TString ifs);
42 void make_cemcCDBTree(const char * infct, const char * outfct);
43 
44 
45 // to get files from my local area
47 const int iteration = 1 ,
48 const char *fname = "pi0input.list",
49 const char * inputCDBfnameIn = "inputcdb1.root",
50 const char * outfileloc1 = "./",
51 const char * outfileloc2 = "./")
52 {
53  //, const int runNumber = 0)
54 
55 
56  //outfileloc1 --> $CONDOR_SCRATCH --> throwaway temp files
57  //outfileloc2 --> user defined gfps to watch/monitor iterations or debug
58 
59  gSystem->Load("libg4dst");
60 
61  std::string inputCDBfname = inputCDBfnameIn;
62 
63  const std::string defout1 = outfileloc1;
64  const std::string defout2 = outfileloc2;
65  // filenames, other than input
66  std::string outntupfile = defout1 + "/outntup_iter"+ std::to_string(iteration) + ".root";
67  std::string outloopfile = defout1 + "/outntup_loop" + std::to_string(iteration) + ".root";
68  std::string outfitfile = defout2 + "/outfit_" + std::to_string(iteration) + ".root";
69  std::string outcsvfile = defout1 + "/csvoutfit" + std::to_string(iteration) + ".csv";
70  std::string iterCDBfile = defout2 + "/cdbtree"+ std::to_string(iteration) + ".root";
71 
72  // in other than the first iteration, the previous iteration's corrections
73  // are needed both in CDBTree form (for input to clusters) and in "native" CaloCalibEmc_Pi0 form
74  // (for aggregation of cumulative corrections across each iteration)
75  std::string prevoutfitfile = defout2 + "/outfit_" + std::to_string(iteration-1) + ".root";
76  if (iteration == 1)
77  prevoutfitfile = "";
78 
79  // for iterations other than 1, ignore input cdbfname and use previous iteration's
80  if (iteration > 1)
81  inputCDBfname = defout2 + "/cdbtree" + std::to_string(iteration-1) + ".root";
82 
84 
86 
87  TString infiletstr(fname);
88  if (infiletstr.Contains(".list"))
89  in->AddListFile(fname);
90  else
91  in->fileopen(fname);
92 
93 
94  se->registerInputManager(in);
95 
96  // JF Nov23 ?? still needed ?
97  Fun4AllInputManager *intrue2 = new Fun4AllRunNodeInputManager("DST_GEO");
98  intrue2->AddFile("updated_geo.root");
99  se->registerInputManager(intrue2);
100 
101 
102 
104  rc->set_StringFlag("CDB_GLOBALTAG","ProdA_2023"); // this points to the global tag in the CDB
105  // The calibrations have a validity range set by the beam clock which is not read out of the prdfs as of now
106  rc->set_uint64Flag("TIMESTAMP",0);
107  // rc->set_uint64Flag("TIMESTAMP",runNumber);
108 
109 
110  CaloTowerCalib *calib = new CaloTowerCalib("CEMCCALIB");
111  // calib->setCalibName("cemc_abscalib_cosmic");// these two lines are needed to choose your own calibration
112 
113  // JF to Blair --> below change preferred fieldname for first towerslope iter
114  if (iteration == 1) // blair-defined towerslope CBDtree
115  {
116  calib->setFieldName("cemc_pi0_abs_calib");
117  }
118  else
119  {
120  // leave this, at least at first, using Sijan's defined
121  // cdbtree outputs
122  calib->setFieldName("cemc_pi0_abs_calib");
123  }
124 
125  calib->set_directURL(inputCDBfname);
126 
127  se->registerSubsystem(calib);
128 
129 
130  RawClusterBuilderTemplate *ClusterBuilder = new RawClusterBuilderTemplate("EmcRawClusterBuilderTemplate2");
131  ClusterBuilder->Detector("CEMC");
132  ClusterBuilder->Verbosity(10);
133  ClusterBuilder->set_threshold_energy(0.030); // This threshold should be the same as in CEMCprof_Thresh**.root file below
134  std::string emc_prof = getenv("CALIBRATIONROOT");
135  emc_prof += "/EmcProfile/CEMCprof_Thresh30MeV.root";
136  ClusterBuilder->LoadProfile(emc_prof);
137  ClusterBuilder->set_UseTowerInfo(1); // to use towerinfo objects rather than old RawTower
138 
139  //NOV 23 Blair : new
140  ClusterBuilder->setInputTowerNodeName("TOWERINFO_CALIB_CEMC");
141  ClusterBuilder->setOutputClusterNodeName("CLUSTERINFO2");
142 
143  se->registerSubsystem(ClusterBuilder);
144 
145 
146  CaloCalibEmc_Pi0 *eval_pi2 = new CaloCalibEmc_Pi0("dummy", outntupfile);
147  eval_pi2->set_UseTowerInfo(1); // to use towerinfo objects rather than old RawTower
148  eval_pi2->setInputTowerNodeName("TOWERINFO_CALIB_CEMC");
149  eval_pi2->setInputClusterNodeName("CLUSTERINFO2");
150  se->registerSubsystem(eval_pi2);
151  cout << "successful registration of pi0 " << endl;
152 
153  se->run(nevents);
154  se->End();
155 
156 
157  // loop over ntuple file
158  CaloCalibEmc_Pi0 calo_obj("CaloCalibEmc_Pi0", outloopfile);
159  calo_obj.InitRun(0);
160  // TTree * intree1 = GetTChainMacro(ifile); // if parallelized
161  calo_obj.Loop(nevents, outntupfile);
162  calo_obj.End(0);
163 
164 
165  //now fit:
166  CaloCalibEmc_Pi0 calo_obj2("CaloCalibEmc_Pi0","");
167  //for test using previous
168  // outloopfile = "/sphenix/user/jfrantz/thdatNov22/pimacs/cutsg33_cpi1_21813.root";
169 
170  calo_obj2.Get_Histos(outloopfile.c_str() , outfitfile.c_str());
171  calo_obj2.Fit_Histos_Etas96(prevoutfitfile.c_str()); // see note at beginning during filenames settings
172  calo_obj2.End(0);
173 
174  // now gen CDBTree file using Sijan's methods
175  make_csv(outfitfile.c_str(), outcsvfile.c_str());
176  make_cemcCDBTree(outcsvfile.c_str(), iterCDBfile.c_str());
177 
178 
179  se->PrintTimer();
180 
181  // gSystem->Exit(0);
182 }
183 
184 
185 TTree* GetTChainMacro(TString ifile="")
186 {
187  ifstream inFile;
188  inFile.open(ifile, ios::in); // open to read
189  if (!inFile)
190  {
191  cerr << "Unable to open file:\n ";
192  exit(1);
193  }
194 
195  TChain *pitree = new TChain("_eventTree");
196  string root_file;
197  int lines=0;
198  while (std::getline(inFile, root_file))
199  {
200  pitree->Add(root_file.c_str());
201  lines += 1;
202  }
203  printf("total lines: %d\n",lines);
204  inFile.close();
205 
206  return (TTree *) pitree;
207 
208  }
209 
210 
211 void make_csv( const char * input_filechar, const char * output_filechar)
212 {
213  // input file
214  const string input_file = input_filechar;
215 
216  // output file
217  const string output_file = output_filechar;
218 
219 
220  // Open the ROOT file
221  TFile *file = TFile::Open(input_file.c_str(), "READ");
222 
223  // Create an output CSV file
224  std::ofstream csvFile(output_file.c_str());
225 
226  // Get the TTree
227  TTree *tree = (TTree*)file->Get("nt_corrVals");
228 
229  // Get the number of entries (rows) in the TTree
230  Long64_t nEntries = tree->GetEntries();
231 
232  if (nEntries != 24576) {std::cout << "The number of entries does not match with EMCal. The number is " << nEntries << " ." << std::endl;}
233 
234 
235  // Get the number of branches (columns) in the TTree
236  TObjArray *branches = tree->GetListOfBranches();
237  int nbranches = branches->GetEntries();
238 
239 
240  // write the header for CSV file
241  csvFile << "phibin" << " , " << "etabin" << " , " << "calib-factor" << std::endl;
242 
243  for (int j = 0; j < nEntries; j++) // for each entry in the Branches
244  {
245  tree->GetEntry(j);
246 
247  int ieta, iphi;
248  float agg_corr_val;
249 
250  for (int b = 0; b < nbranches; ++b)
251  {
252  TBranch *branch = (TBranch*)branches->At(b);
253  TLeaf *leaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
254 
255  if (b == 0) {ieta = leaf->GetValue(0);}
256  if (b == 1) {iphi = leaf->GetValue(0);}
257  if (b == 3) {agg_corr_val = leaf->GetValue(0);}
258  }
259  csvFile << iphi << " , " << ieta << " , " << agg_corr_val << "\n";
260  }
261 
262  // Clean up
263  csvFile.close();
264  file->Close();
265  delete file;
266  std::cout << "All Done making csvfile:" << output_file << std::endl;
267  // return 0;
268 }
269 
270 
271 void make_cemcCDBTree(const char * input_file1, // csv file to be supplied
272  const char * output_file1) // output root file having CDB Ttree
273 {
274  // file location, input file and output file
275 
276 
277  const string input_file = input_file1;
278  const string output_file = output_file1;
279 
280  std::cout << "start making CDBTree: " << output_file1 << std::endl;
281 
282  float corrArray[256][96];
283  string line, cell1, cell2, cell3;
284 
285  // Open the CSV file
286  ifstream file(input_file.c_str());
287 
288  // read header row and do nothing
289  std::getline(file, line);
290  while(getline(file, line))
291  {
292  stringstream lineStream(line);
293 
294  getline(lineStream, cell1, ',');
295  //std::cout << "phi: " << std::stoi(cell1) << " ";
296 
297  getline(lineStream, cell2, ',');
298  //std::cout << "eta: " << std::stoi(cell2) << " ";
299 
300  getline(lineStream, cell3, ',');
301  //std::cout << "corr: " << std::stof(cell3) << std::endl;
302 
303  corrArray[std::stoi(cell1)][std::stoi(cell2)] = std::stof(cell3);
304  }
305  file.close();
306 
307  /* to check if my array was correctly filled
308  * --------------------------------------
309  for (int iphi=0; iphi<256; iphi++)
310  {
311  for (int ieta=0; ieta<96; ieta++)
312  {
313  std::cout << iphi << " " << ieta << " " << corrArray[iphi][ieta] << std::endl;
314  }
315  }
316  */
317 
318  CDBTTree *cdbttree = new CDBTTree(output_file.c_str());
319  for(int iphi=0; iphi<256; iphi++){
320  for(int ieta=0; ieta<96; ieta++){
321  int key = iphi + (ieta << 16);
322  cdbttree->SetFloatValue(key, "cemc_pi0_abs_calib", corrArray[iphi][ieta]);
323  }
324  }
325 
326  cdbttree->Commit();
327  //cdbttree->Print();
328  cdbttree->WriteCDBTTree();
329  delete cdbttree;
330  std::cout << "cdb success " << std::endl;
331  // gSystem->Exit(0);
332 }