File indexing completed on 2025-08-05 08:19:41
0001 #ifndef FUN4ALL_EMCAL_C
0002 #define FUN4ALL_EMCAL_C
0003
0004
0005 #include <fstream>
0006 #include <iostream>
0007 #include <string>
0008 #include <memory>
0009 #include <format>
0010 #include <vector>
0011
0012
0013 #include <TF1.h>
0014 #include <TFile.h>
0015 #include <TROOT.h>
0016 #include <TSystem.h>
0017
0018 #include <caloreco/CaloTowerCalib.h>
0019 #include <caloreco/CaloTowerStatus.h>
0020 #include <caloreco/RawClusterBuilderTemplate.h>
0021 #include <caloreco/RawClusterPositionCorrection.h>
0022
0023 #include <ffamodules/FlagHandler.h>
0024 #include <ffamodules/HeadReco.h>
0025 #include <ffamodules/SyncReco.h>
0026
0027 #include <fun4all/Fun4AllDstInputManager.h>
0028 #include <fun4all/Fun4AllInputManager.h>
0029 #include <fun4all/Fun4AllRunNodeInputManager.h>
0030 #include <fun4all/Fun4AllServer.h>
0031 #include <fun4all/Fun4AllUtils.h>
0032 #include <fun4all/SubsysReco.h>
0033
0034 #include <phool/recoConsts.h>
0035 #include <phool/RunnumberRange.h>
0036
0037 #include <ffamodules/CDBInterface.h>
0038
0039 #include <calib_emc_pi0/pi0EtaByEta.h>
0040 #include <litecaloeval/LiteCaloEval.h>
0041
0042 #include <globalvertex/GlobalVertexReco.h>
0043 #include <mbd/MbdReco.h>
0044 #include <mbd/MbdEvent.h>
0045
0046 R__LOAD_LIBRARY(libcdbobjects)
0047 R__LOAD_LIBRARY(libfun4all.so)
0048 R__LOAD_LIBRARY(libfun4allraw.so)
0049 R__LOAD_LIBRARY(libcalo_reco.so)
0050 R__LOAD_LIBRARY(libffamodules.so)
0051 R__LOAD_LIBRARY(libLiteCaloEvalTowSlope.so)
0052 R__LOAD_LIBRARY(libcalibCaloEmc_pi0.so)
0053 R__LOAD_LIBRARY(libglobalvertex.so)
0054 R__LOAD_LIBRARY(libg4mbd.so)
0055 R__LOAD_LIBRARY(libmbd_io.so)
0056 R__LOAD_LIBRARY(libmbd.so)
0057
0058 void createLocalEMCalCalibFile(const std::string &fname, int runNumber);
0059
0060 void Fun4All_EMCal(int nevents = 1e2,
0061 const std::string &fname = "inputdata.txt",
0062 int iter = 0,
0063 const std::string &calib_fname = "local_calib_copy.root",
0064 const std::string &fieldname = "CEMC_calib_ADC_to_ETower")
0065 {
0066 Fun4AllServer *se = Fun4AllServer::instance();
0067 se->Verbosity(0);
0068 CDBInterface::instance()->Verbosity(1);
0069
0070 recoConsts *rc = recoConsts::instance();
0071
0072 std::ifstream file(fname);
0073 std::string first_file;
0074 getline(file, first_file);
0075
0076 std::string m_fieldname = fieldname;
0077
0078
0079
0080
0081 std::pair<int, int> runseg = Fun4AllUtils::GetRunSegment(first_file);
0082 int runnumber = runseg.first;
0083 std::cout << "run number = " << runnumber << std::endl;
0084
0085 rc->set_StringFlag("CDB_GLOBALTAG", "newcdbtag");
0086 rc->set_uint64Flag("TIMESTAMP", runnumber);
0087
0088 std::unique_ptr<Fun4AllInputManager> in = std::make_unique<Fun4AllDstInputManager>("DST_TOWERS");
0089 in->AddListFile(fname);
0090 se->registerInputManager(in.release());
0091
0092 std::string filename = first_file.substr(first_file.find_last_of("/\\") + 1);
0093 std::string OutFile = std::format("OUTHIST_iter{}_{}", iter, filename);
0094
0095 if (iter == 0)
0096 {
0097 createLocalEMCalCalibFile(calib_fname, runnumber);
0098 std::cout << "creating " << calib_fname << " and exiting" << std::endl;
0099 gSystem->Exit(0);
0100 }
0101
0102
0103
0104
0105 std::unique_ptr<MbdReco> mbdreco = std::make_unique<MbdReco>();
0106 se->registerSubsystem(mbdreco.release());
0107
0108
0109 std::unique_ptr<GlobalVertexReco> gvertex = std::make_unique<GlobalVertexReco>();
0110 se->registerSubsystem(gvertex.release());
0111
0112
0113
0114 std::cout << "Adding Geometry file" << std::endl;
0115 std::unique_ptr<Fun4AllInputManager> intrue2 = std::make_unique<Fun4AllRunNodeInputManager>("DST_GEO");
0116 std::string geoLocation = CDBInterface::instance()->getUrl("calo_geo");
0117 intrue2->AddFile(geoLocation);
0118 se->registerInputManager(intrue2.release());
0119
0120
0121
0122 std::unique_ptr<CaloTowerStatus> statusEMC = std::make_unique<CaloTowerStatus>("CEMCSTATUS");
0123 statusEMC->set_detector_type(CaloTowerDefs::CEMC);
0124
0125 statusEMC->set_directURL_hotMap("/sphenix/u/bseidlitz/work/forChris/caloStatusCDB_y2/moreMaps/EMCalHotMap_new_2024p006-48837cdb.root");
0126 se->registerSubsystem(statusEMC.release());
0127
0128 std::unique_ptr<CaloTowerCalib> calibEMC = std::make_unique<CaloTowerCalib>("CEMCCALIB");
0129 calibEMC->set_detector_type(CaloTowerDefs::CEMC);
0130 calibEMC->set_directURL(calib_fname);
0131 calibEMC->setFieldName(fieldname);
0132 se->registerSubsystem(calibEMC.release());
0133
0134
0135
0136 std::cout << "Building clusters" << std::endl;
0137 std::unique_ptr<RawClusterBuilderTemplate> ClusterBuilder = std::make_unique<RawClusterBuilderTemplate>("EmcRawClusterBuilderTemplate");
0138 ClusterBuilder->Detector("CEMC");
0139 ClusterBuilder->set_threshold_energy(0.07);
0140 std::string emc_prof = getenv("CALIBRATIONROOT");
0141 emc_prof += "/EmcProfile/CEMCprof_Thresh30MeV.root";
0142 ClusterBuilder->LoadProfile(emc_prof);
0143 ClusterBuilder->set_UseTowerInfo(1);
0144 ClusterBuilder->setOutputClusterNodeName("CLUSTERINFO_CEMC");
0145 ClusterBuilder->set_UseAltZVertex(1);
0146 se->registerSubsystem(ClusterBuilder.release());
0147
0148
0149
0150 if (iter == 1 || iter == 2 || iter == 3)
0151 {
0152 std::unique_ptr<LiteCaloEval> eval7e = std::make_unique<LiteCaloEval>("CEMCEVALUATOR2", "CEMC", OutFile);
0153 eval7e->CaloType(LiteCaloEval::CEMC);
0154 eval7e->set_reqMinBias(false);
0155 eval7e->setInputTowerNodeName("TOWERINFO_CALIB_CEMC");
0156 se->registerSubsystem(eval7e.release());
0157 }
0158
0159 if (iter > 3)
0160 {
0161 std::unique_ptr<pi0EtaByEta> ca = std::make_unique<pi0EtaByEta>("calomodulename", OutFile);
0162 ca->set_timing_cut_width(16);
0163 ca->apply_vertex_cut(true);
0164 ca->set_vertex_cut(20.);
0165 ca->set_pt1BaseClusCut(1.3);
0166 ca->set_pt2BaseClusCut(0.7);
0167 ca->set_NclusDeptFac(1.4);
0168 ca->set_RunTowByTow(true);
0169 ca->set_reqTrig(false);
0170 ca->set_GlobalVertexType(GlobalVertex::MBD);
0171 ca->set_requireVertex(true);
0172 ca->set_calib_fieldname(m_fieldname);
0173 se->registerSubsystem(ca.release());
0174 }
0175
0176 se->run(nevents);
0177 se->End();
0178 se->PrintTimer();
0179 delete se;
0180
0181 std::cout << "All done!" << std::endl;
0182 gSystem->Exit(0);
0183 }
0184
0185 void createLocalEMCalCalibFile(const std::string &fname, int runNumber)
0186 {
0187 std::string default_time_independent_calib = (runNumber >= RunnumberRange::RUN3AUAU_FIRST) ? "CEMC_calib_ADC_to_ETower_default" : "cemc_pi0_twrSlope_v1_default";
0188 std::string m_calibName = "getdefault";
0189
0190 std::string calibdir = CDBInterface::instance()->getUrl(m_calibName);
0191 std::string filePath;
0192
0193 if (!calibdir.empty())
0194 {
0195 filePath = calibdir;
0196 }
0197 else
0198 {
0199 calibdir = CDBInterface::instance()->getUrl(default_time_independent_calib);
0200
0201 if (calibdir.empty())
0202 {
0203 std::cout << "No EMCal Calibration NOT even a default" << std::endl;
0204 exit(1);
0205 }
0206 filePath = calibdir;
0207 std::cout << "No specific file for " << m_calibName << " found, using default calib " << default_time_independent_calib << std::endl;
0208 }
0209
0210 std::unique_ptr<TFile> f_cdb = std::make_unique<TFile>(filePath.c_str());
0211 f_cdb->Cp(fname.c_str());
0212 f_cdb->Cp("initial_calib.root");
0213
0214 std::cout << "created local Calib file for run " << runNumber << " named " << fname << std::endl;
0215 }
0216 #endif
0217
0218 #ifndef __CINT__
0219 int main(int argc, const char *const argv[])
0220 {
0221 const std::vector<std::string> args(argv, argv + argc);
0222
0223 if (args.size() > 6)
0224 {
0225 std::cout << "usage: " << args[0] << " [events] [fname] [iter] [calib_fname] [fieldname]" << std::endl;
0226 return 1;
0227 }
0228
0229 int events = 1e2;
0230 std::string fname = "inputdata.txt";
0231 int iter = 0;
0232 std::string calib_fname = "local_calib_copy.root";
0233 std::string fieldname = "CEMC_calib_ADC_to_ETower";
0234
0235 if (args.size() >= 2)
0236 {
0237 events = std::stoi(args[1]);
0238 }
0239 if (args.size() >= 3)
0240 {
0241 fname = args[2];
0242 }
0243 if (args.size() >= 4)
0244 {
0245 iter = std::stoi(args[3]);
0246 }
0247 if (args.size() >= 5)
0248 {
0249 calib_fname = args[4];
0250 }
0251 if (args.size() >= 6)
0252 {
0253 fieldname = args[5];
0254 }
0255
0256 Fun4All_EMCal(events, fname, iter, calib_fname, fieldname);
0257
0258 std::cout << "done" << std::endl;
0259 return 0;
0260 }
0261 #endif