Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:54

0001 
0002 #include <vector>
0003 #include <iostream>
0004 #include <string>
0005 
0006 bool chamberExists(const unsigned int x, const unsigned int y) {
0007 
0008   if (y == 1) return false;
0009 
0010   if (y == 3 || y == 5 || (y >= 7 && y <= 12) || y == 14 || y == 16) {
0011     if (x >= 1 && x <= 36) return true;
0012     return false;
0013   }
0014 
0015   if (y == 2 || y == 4 || y == 6 || y == 13 || y == 15 || y == 17) {
0016     if (x >= 1 && x <= 18) return true;
0017     return false;
0018   }
0019 
0020   if (y == 18) {
0021     if (x >= 9 && x <= 13) return true;
0022     return false;
0023   }
0024 
0025   return false;
0026 
0027 }
0028 
0029 void splitString(const std::string& str, const std::string& delim, std::vector<std::string>& results) {
0030   std::string lstr = str;
0031   unsigned int cutAt;
0032   while ((cutAt = lstr.find_first_of(delim)) != lstr.npos) {
0033     if(cutAt > 0) {
0034       results.push_back(lstr.substr(0, cutAt));
0035     }
0036     lstr = lstr.substr(cutAt + 1);
0037   }
0038   if(lstr.length() > 0) {
0039     results.push_back(lstr);
0040   }
0041 }
0042 
0043 void fillCellsWithRowNeighbourAverage(TH2* h2) {
0044   double lastNotZeroValue = 0.0;
0045   std::vector<unsigned int> zeros;
0046 
0047   // Looping through Y-coordinate
0048   for (unsigned int y = 1; y <= 18; y++) {
0049 
0050     // Set Zeros (we consider only rows!)
0051     lastNotZeroValue = 0.0;
0052     zeros.clear();
0053 
0054     // Looping through X-coordinate
0055     for (unsigned int x = 1; x <= 36; x++) {
0056       if (chamberExists(x, y)) {
0057         double v = h2->GetBinContent(x, y);
0058       
0059         if (v == 0.0) {
0060           // Put another element to zeros vector
0061           zeros.push_back(x);
0062         } else {
0063           // If there already were empty cells - fill 'em with averages
0064           double aver = (lastNotZeroValue > 0.0 ? (lastNotZeroValue  + v) / 2.0 : v);
0065           for (unsigned int i = 0; i < zeros.size(); i++) {
0066             h2->SetBinContent(zeros.at(i), y, aver);
0067           }
0068           // Clean vector
0069           zeros.clear();
0070           // Save as last non zero
0071           lastNotZeroValue = v;
0072         }
0073       }
0074     }
0075 
0076     // If there were empty cells - fill 'em with last non-zero
0077     if (lastNotZeroValue != 0.0) {
0078       for (unsigned int i = 0; i < zeros.size(); i++) {
0079         h2->SetBinContent(zeros.at(i), y, lastNotZeroValue);
0080       }
0081     }
0082 
0083   }
0084 }
0085 
0086 void fillCellsWithRowAverage(TH2* h2) {
0087 
0088   std::vector<double> averages;
0089   double items = 0;
0090 
0091   // Looping through Y-coordinate
0092   for (unsigned int y = 1; y <= 18; y++) {
0093 
0094     averages.push_back(0.0);
0095     double items = 0;
0096 
0097     // Looping through X-coordinate
0098     for (unsigned int x = 1; x <= 36; x++) {
0099       if (chamberExists(x, y)) {
0100         double v = h2->GetBinContent(x, y);
0101         if (v != 0.0) {
0102           averages[y - 1] += v;
0103           items += 1;
0104         }
0105       }
0106     }
0107     averages[y - 1] /= items;
0108   }
0109 
0110   // Write row averages to histogram
0111 
0112   for (unsigned int y = 1; y <= 18; y++) {
0113     for (unsigned int x = 1; x <= 36; x++) {
0114       if (chamberExists(x, y)) {
0115         h2->SetBinContent(x, y, averages[y - 1]);
0116       }
0117     }
0118   }
0119 
0120 }
0121 
0122 void copy(const std::string& src_file, const std::string& src_obj, const std::string& des_file, const std::string& des_path, const std::string& des_obj) {
0123 
0124   gStyle->SetPalette(1,0);
0125 
0126   TFile *old_file = new TFile(src_file.c_str());
0127   if (!old_file) {
0128     std::cerr << "Can not open file: " << src_file << std::endl;
0129     return;
0130   }
0131 
0132   TObject* old_obj = old_file->Get(src_obj.c_str());
0133   if (!old_obj) {
0134     std::cerr << "Can not open object: " << src_obj << " in file " << src_file << std::endl;
0135     return;
0136   }
0137 
0138   TFile *new_file = new TFile(des_file.c_str(), "RECREATE");
0139   TDirectory *cdto = 0;
0140 
0141   std::vector<std::string> tokens;
0142   splitString(des_path, "/", tokens);
0143   for (unsigned int i = 0; i < tokens.size(); i++) {
0144     if (cdto == 0) {
0145       cdto = new_file->mkdir(tokens.at(i).c_str());
0146     } else {
0147       cdto = cdto->mkdir(tokens.at(i).c_str());
0148     }
0149     cdto->cd();
0150   }
0151 
0152   TObject *new_obj = dynamic_cast<TObject*> (old_obj->Clone(des_obj.c_str()));
0153 
0154   if (new_obj->IsA()->InheritsFrom("TH2")) {
0155     TH2* h2 = (TH2*) new_obj;
0156     if (h2) {
0157       if(h2->GetXaxis()->GetXmin() <= 1 && h2->GetXaxis()->GetXmax() >= 36 &&
0158          h2->GetYaxis()->GetXmin() <= 1 && h2->GetYaxis()->GetXmax() >= 18) {
0159 
0160         //fillCellsWithRowNeighbourAverage(h2);
0161         fillCellsWithRowAverage(h2);
0162         h2->Draw("colz");
0163 
0164       }
0165     }
0166   }
0167 
0168   new_file->Write();
0169 
0170 }
0171 
0172 void reference() {
0173 
0174   copy("DQM_V0001_CSC_R000132601.root", 
0175        "DQMData/Run 132601/CSC/Run summary/Summary/CSC_Reporting", 
0176        "csc_reference.root", 
0177        "DQMData/Run 132601/CSC/Run summary/Summary/CSC_Reporting", 
0178        "CSC_Reporting");
0179 
0180 }