Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:13:57

0001 #pragma once
0002 #include <sys/types.h>
0003 #include <sys/stat.h>
0004 
0005 #include "constant_values.hh"
0006 
0007 #include <boost/filesystem.hpp>
0008 
0009 
0010 bool IsFileOrDirectoryExist(const string& path) {
0011     struct stat buffer;
0012     return (stat(path.c_str(), &buffer) == 0);
0013 }
0014 
0015 bool IsFileExist(const string& path) {
0016     struct stat buffer;
0017     return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
0018 }
0019 
0020 bool IsDirectoryExist(const string& path) {
0021     struct stat buffer;
0022     return (stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
0023 }
0024 
0025 namespace fs = boost::filesystem;
0026 R__LOAD_LIBRARY( libboost_filesystem.so )
0027 vector < string > GetFileList(const string& directory_path, const string& pattern="" ) 
0028 {
0029 
0030     vector < string > entries;
0031     try 
0032       {
0033         for (auto& entry : fs::directory_iterator(directory_path) ) 
0034       {
0035         
0036             string name = entry.path().filename().string();
0037         // cout << pattern << "\t"
0038         //   << name << "\t"
0039         //   << int(fs::is_regular_file(entry.status()) == true ) << "\t"
0040         //   << int( (name.find(pattern) != string::npos) == true  )
0041         //   << endl;
0042         
0043             if (fs::is_regular_file(entry.status())) 
0044           {
0045                 if (name.find(pattern) != string::npos) 
0046           {
0047             //cout << name << " is taken." << endl;
0048                     entries.push_back(name);
0049           }
0050           } 
0051             else if (fs::is_directory(entry.status())) 
0052           {
0053                 if (name.find(pattern) != string::npos) 
0054           {
0055                     entries.push_back(name + "/");
0056           }
0057           }
0058       }
0059       } 
0060     catch (const exception& e) 
0061       {
0062         cerr << "Error: " << e.what() << endl;
0063       }
0064     return entries;
0065 }
0066 
0067 string GetEvtDir( int run_num )
0068 {
0069 
0070   if( run_num < 43276 ) // in the commissioning phase
0071     return kIntt_commissioning_evt_dir;
0072 
0073   // in the physics data taking phase
0074   return kIntt_physics_evt_dir;
0075 }
0076 
0077 string GetRunNum8digits( int run_num )
0078 {
0079   return string( 8 - to_string(run_num).size(), '0' ) + to_string( run_num );
0080 }
0081 
0082 string GetRunType( int run_num = 26975 )
0083 {
0084 
0085   string evt_dir = GetEvtDir( run_num );
0086   cout << "evt dir: " << evt_dir << endl;
0087   
0088   for( int suffix_index=0; suffix_index<2; suffix_index++ )
0089     {
0090       string suffix = ".evt";
0091       if( suffix_index == 1 )
0092     suffix = ".prdf" ;
0093       
0094       for( int i=0; i<kRun_type_num; i++ )
0095     {
0096       string file_pre = evt_dir + kRun_types[i] + "/"
0097         + kRun_types[i] + "_intt";
0098       
0099       string file_suf = "-0000" + suffix;
0100       for( int j=0; j<kFelix_num; j++ )
0101         {
0102           string file_path = file_pre + to_string( j ) + "-" + string( 8 - to_string(run_num).size(), '0' ) + to_string( run_num ) + file_suf;
0103           //cout << " - " << file_path << endl;
0104           if( IsFileExist( file_path ) == true )
0105         {
0106           cout << " " << file_path << " was found. The run type is " << kRun_types[i] << endl;
0107           return kRun_types[i];
0108         }
0109           
0110         }
0111       
0112     }
0113     }
0114   
0115   cerr <<  evt_dir << "{run_type}/"
0116        << "{run_type}" << "_intt{FELIX}-"
0117        << string( 8 - to_string(run_num).size(), '0' ) + to_string( run_num )
0118        << "-0000.evt"
0119        << " is not found."
0120        << endl;
0121   
0122   return "NotFound";
0123 }
0124 
0125 
0126 /*!
0127   @brief A path to the raw data of run from intt_server is returned
0128   @details Files matches the 2nd argument run_type should work
0129  */
0130 string GetFilePath( int run, string run_type, int intt_server, int segment=0 )
0131 {
0132   assert( 0<=intt_server && intt_server <= 7 );
0133 
0134   stringstream ss_rtn;
0135   ss_rtn << GetEvtDir( run ) << run_type << "/"
0136      << run_type << "_intt" << intt_server << "-"
0137      << setw(8) << setfill( '0' ) << run << "-"
0138      << setw(4) << setfill( '0' ) << segment
0139      << ".evt";
0140   
0141   if( IsFileExist( ss_rtn.str() ) )
0142     return ss_rtn.str();
0143     
0144   ss_rtn.str( "" );
0145   ss_rtn << GetEvtDir( run )  << run_type << "/"
0146      << run_type << "_intt" << intt_server << "-" << setw(8) << setfill( '0' ) << run << "-0000.prdf";
0147   
0148   if( IsFileExist( ss_rtn.str() ) )
0149     return ss_rtn.str();
0150 
0151   return "";  
0152 }
0153 
0154 /*!
0155   @brief A list of raw data file paths is returned
0156  */
0157 vector < string > GetFilePaths( int run, string run_type, int intt_server )
0158 {
0159 
0160   vector < string > rtn;
0161   for( int i=0; i<9999; i++ )
0162     {
0163       string path = GetFilePath( run, run_type, intt_server, i );
0164 
0165       // If file is not found, stop finding
0166       if( path == "" )
0167     break;
0168 
0169       rtn.push_back( path );
0170     }
0171 
0172   return rtn;
0173 }
0174 
0175 vector < string > GetDsts( int run, bool is_official, string should_include="", string should_not_include="" )
0176 {
0177   /*!
0178     @brief It returns a list of a path to DST files
0179     @param run The run number
0180     @param run_type The run type
0181     @param is_official A flag to use the official DST or not
0182     @param should_include You can require paths to include this string
0183     @param should_not_include You can require paths NOT to include this string
0184     @details This is used to get a path to DST files to be used.
0185     The arguments should_include/should_not_include is useful to include/exclude DST having no hot channels
0186    */
0187   string root_dir = kIntt_dst_dir;
0188 
0189   cout << " DST search started..." << endl;
0190   string run_type = GetRunType( run );
0191   if( run_type == "NotFound" )
0192     {
0193       cerr << " Run type of run " << run << " is not found." << endl;
0194       cerr << " It means no evt/prdf files were found. It has to be accessible." << endl;
0195       cerr << " An empty list is returned." << endl;
0196       return vector < string >();
0197     }
0198     
0199   if( is_official == true )
0200     {
0201 
0202       if( run < kFirst_physics_run )
0203     root_dir = kOfficial_Dst_dir ;
0204       else
0205     root_dir = kOfficial_physics_Dst_dir ;
0206 
0207       cout << " Official DST? Where is it?" << endl;
0208       string sub_dir = root_dir + "intt" + run_type;
0209 
0210       // temp solution for physics runs
0211       if( run_type == "physics" )
0212      sub_dir = root_dir + "intt" + "beam";
0213       
0214       bool found = fs::exists( sub_dir );
0215       if( found == true )
0216     {
0217       root_dir = sub_dir;
0218     }
0219 
0220       if( root_dir == kOfficial_Dst_dir )
0221     {
0222       cerr << " Good directory is not found" << endl;
0223       cerr << " An empty list is returned." << endl;
0224       return vector < string >();
0225     }
0226       else
0227     {
0228       cout << " OK, a sub-directory candidate found: " << root_dir << endl;
0229     }
0230 
0231       auto dir_list = GetFileList( root_dir ); // There must be only directories
0232       for( auto& dir: dir_list )
0233     {
0234 
0235       if( run >= kFirst_physics_run )
0236         {
0237 
0238           if( dir.substr(0, 4) != "run_" )
0239         continue;
0240         }
0241       
0242       //cout << dir.substr( string("run_").size(), 8 ) << endl;
0243       int run_range_begin = stoi( dir.substr( string("run_").size(), 8 ) );
0244       int run_range_end   = stoi( dir.substr( dir.find_last_of( "_" )+1, 8 ) );
0245 
0246       if( run_range_begin <= run && run < run_range_end )
0247         {
0248           root_dir = root_dir + "/" + dir;
0249           cout << " Sub-sub directory candidate found: " << root_dir << endl;
0250           break;
0251         }
0252     }
0253       
0254     } // end of if( is_official )
0255 
0256   vector < string > rtn_candidates = GetFileList( root_dir, to_string(run) );
0257   sort( rtn_candidates.begin(), rtn_candidates.end() );
0258 
0259   vector < string > rtn;
0260   for( auto& file : rtn_candidates )
0261     {
0262 
0263       if( should_include != "" && file.find( should_include ) == string::npos )
0264     continue;
0265       
0266       if( should_not_include != "" && file.find( should_not_include ) != string::npos )
0267     continue;
0268 
0269 
0270       rtn.push_back( root_dir + file );
0271       //cout << "\t- " << root_dir + file << endl;
0272 
0273     }
0274   
0275   return rtn;
0276 }
0277 
0278 vector < string > GetDstsTrkr( int run, bool is_official, string should_include="", string should_not_include="" )
0279 {
0280   auto files_prim = GetDsts( run, false, should_include, should_not_include );
0281   vector < string > files;
0282   for( auto& file : files_prim )
0283     {
0284       if( is_official == true && file.find( "official" ) != string::npos )
0285     files.push_back( file );
0286       else if( is_official == false && file.find( "special" ) != string::npos )
0287     files.push_back( file );
0288 
0289     }
0290 
0291   return files;
0292 }
0293 
0294 int functions()
0295 {
0296 
0297   //GetRunType();
0298   auto a = GetDsts( 41300, true );
0299   
0300   auto b = GetDsts( 41300, false, "", "no_hot" );
0301 
0302   auto c = GetDstsTrkr(  50377, true, "no_hot", "" ); // 1000000, true, false, true, false
0303   for( auto d : c )
0304     cout << d << endl;
0305   return 0;
0306 }