Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-18 09:22:11

0001 
0002 float getTempRun(int);
0003 
0004 TProfile2D* pr2d_temp = new TProfile2D("pr2d_temp","",24,0,24,64,0,64,15.0,30.0,"s");
0005 
0006 void ana_hits() {
0007     //string parantDir = "../output_cosmic_tsc_temp/";
0008     string parantDir = "../cos_only_tsc_spec_output//";
0009     TSystemDirectory dir(parantDir.c_str(),parantDir.c_str()); // Adjust the directory path
0010     TList *files = dir.GetListOfFiles();
0011 
0012     if (!files) {
0013         std::cerr << "No files found in the directory" << std::endl;
0014         return;
0015     }
0016 
0017     std::vector<TString> fileNames;
0018     std::vector<int> runNumbers;
0019 
0020     // Collect the file names and run numbers
0021     TIter next(files);
0022     TObject *fileObj;
0023     while ((fileObj = next())) {
0024         TString fileName = fileObj->GetName();
0025         // Only add files that match the pattern (out_<RUNNUMBER>.root)
0026         if (fileName.Contains("out_") && fileName.EndsWith(".root")) {
0027             fileNames.push_back(fileName);
0028 
0029             // Extract the run number (assumes the filename format is out_<RUNNUMBER>.root)
0030             TString runStr = fileName(4, fileName.Index(".root") - 4); // Extract the number after 'out_'
0031             int runNumber = runStr.Atoi();
0032             runNumbers.push_back(runNumber);
0033         }
0034     }
0035 
0036     // Declare a histogram with the number of bins equal to the number of files
0037     int numFiles = fileNames.size();
0038     if (numFiles == 0) {
0039         std::cerr << "No matching files found" << std::endl;
0040         return;
0041     }
0042 
0043     //////////////////////////////////////////
0044     //  Make run list
0045     ///////////////////////////////////////////
0046     std::ofstream outFile("runList.txt");
0047 
0048     // Check if the file is open
0049     if (!outFile.is_open()) {
0050         std::cerr << "Unable to open the file for writing!" << std::endl;
0051         return ; // Return error if the file could not be opened
0052     }
0053 
0054     // Loop over the vector and write each run number to the file
0055     for (const int& runNumber : runNumbers) {
0056         outFile << runNumber << std::endl; // Write each run number on a new line
0057     }
0058 
0059     // Close the file after writing
0060     outFile.close();
0061 
0062 
0063     /////////////////////////////////////////
0064     //  analyze rooot files
0065     //////////////////////////////////////////
0066 
0067     TH1F *entryCountHist = new TH1F("h_hits", "Entries in h", numFiles, 0, numFiles);
0068     TH1F *h_events_run =  new TH1F("h_events_run", "Entries in h", numFiles, 0, numFiles);
0069     TH1F *h_towerHits_run =  new TH1F("h_towerHits_run", "Entries in h", numFiles, 0, numFiles);
0070     TH1F *runNumberHist = new TH1F("runNumberHist", "Run Numbers for Each File", numFiles, 0, numFiles);
0071     TH1F *h_temp_run = new TH1F("h_temp_run", "", numFiles, 0, numFiles);
0072 
0073     // Loop over all the files
0074     for (int i = 0; i < numFiles; ++i) {
0075         TString fileName = parantDir.c_str()  + fileNames[i];
0076         TFile *file = TFile::Open(fileName);
0077 
0078         if (file && !file->IsZombie()) {
0079             TH1F *h = (TH1F*)file->Get("hcalout_eta_20");
0080             TH1F *h_event = (TH1F*) file->Get("h_event");
0081 
0082             if (h) {
0083                 float eLow = 1;
0084                 float eHigh= 5;
0085                 int entries = h->Integral(h->FindBin(eLow),h->FindBin(eHigh));
0086                 entryCountHist->SetBinContent(i + 1, entries); // Set the number of entries in the corresponding bin
0087                 h_events_run->SetBinContent(i+1,h_event->GetEntries());
0088                 runNumberHist->SetBinContent(i + 1, runNumbers[i]); // Set the run number in the corresponding bin
0089 
0090                 std::cout << "File: " << fileName << " has " <<  h_event->GetEntries() << " events" << std::endl;
0091                 float temp = getTempRun(runNumbers[i]);
0092                  h_temp_run->SetBinContent(i+1,temp);
0093             } else {
0094                 std::cout << "Histogram 'h' not found in file " << fileName << std::endl;
0095             }
0096             file->Close();
0097         } else {
0098             std::cout << "File " << fileName << " not found or unable to open" << std::endl;
0099         }
0100     }
0101     TFile *outputFile = new TFile("output/anaOut.root", "RECREATE");
0102     entryCountHist->Write();
0103     h_events_run->Write();
0104     runNumberHist->Write();
0105     h_temp_run->Write();
0106     pr2d_temp->Write();
0107 
0108 
0109     outputFile->Close();
0110     std::cout << "Histogram saved to entry_count_histograms.root" << std::endl;
0111 }
0112 
0113 
0114 
0115 float getTempRun(int runnumber){
0116 
0117   TFile* f = new TFile(Form("../tempHists/ohcal_temp_hist_%d.root",runnumber));
0118   if (!f){
0119      cout << "could not find temp file" << endl;
0120      return 0;
0121 
0122     }
0123 
0124 
0125   TH2F* h = (TH2F*) f->Get("h_ohcal_temp");
0126 
0127   if (!h){
0128      cout << "no temp hist" << endl;
0129      return 0;
0130 
0131    }
0132 
0133   float temp = 0;
0134   int tempC = 0;
0135   for(int ix=1; ix<h->GetXaxis()->GetNbins()+1; ix++){
0136     for(int iy=1;iy<h->GetYaxis()->GetNbins()+1; iy++){
0137         float t = h->GetBinContent(ix,iy);
0138        pr2d_temp->Fill(ix-1,iy-1,t);
0139        if (t > 15 && t < 30){
0140           temp += t;
0141           tempC++;
0142        }
0143     }
0144   }
0145 
0146   delete h;
0147   delete f;
0148   if (tempC < 3) return 0;
0149   return temp/tempC;    
0150 
0151 }