source: Idarraga/mpxdataconverter/listHandler.cpp

Last change on this file was 292, checked in by idarraga, 12 years ago
File size: 5.4 KB
Line 
1/////////////////////////////////////////
2// John Idárraga
3// For the MediPix collaboration 06/2007
4
5#include "listHandler.h"
6
7
8ListHandler::ListHandler(Char_t * dirPath){
9
10        m_dirPath = dirPath;
11        std::cout << "[INFO] looking up in " << m_dirPath << std::endl;
12
13}
14
15std::vector<std::string> ListHandler::OrderFilesPairMatch(std::vector<std::string> listFrames, std::vector<std::string> listDSC){
16
17
18        std::vector<std::string> orderedString;
19
20        std::vector<std::string>::iterator itFrames = listFrames.begin();
21        std::vector<std::string>::iterator itDSD = listDSC.begin();
22
23        TString tempName = "";
24        TString tempName2 = "";
25
26        for( ; itFrames != listFrames.end() ; itFrames++ )
27        {
28                // first possible name
29                tempName = ((TString)(*itFrames));
30                tempName+=".dsc";
31
32                // second possible name
33                tempName2 = ((TString)(*itFrames));
34                int size = tempName2.Sizeof();
35                bool marktobreak = false;
36                for(int i = size-1 ; i >= 0 ; i--) {
37                        if(tempName2[i] == '.'){
38                                marktobreak = true;
39                        }
40                        // remove first
41                        tempName2.Remove(i);
42                        // then break if necessary
43                        if(marktobreak) break;
44                }
45                tempName2+=".dsc";
46
47                /*
48                if(((TString)(*itFrames)).Contains(".txt")) {
49                        tempName2.Remove(TString::kTrailing, 't');
50                        tempName2.Remove(TString::kTrailing, 'x');
51                        tempName2.Remove(TString::kTrailing, 't');
52                        tempName2.Remove(TString::kTrailing, '.');
53                }
54                tempName2+=".dsc";
55
56                // third possible name
57                tempName2 = ((TString)(*itFrames));
58                if(((TString)(*itFrames)).Contains(".dat")) {
59                        tempName2.Remove(TString::kTrailing, 't');
60                        tempName2.Remove(TString::kTrailing, 'x');
61                        tempName2.Remove(TString::kTrailing, 't');
62                        tempName2.Remove(TString::kTrailing, '.');
63                }
64                tempName2+=".dsc";
65                 */
66
67                /* look for the corresponding .dsc file
68                 that hopefully is not so far in the list.
69                 No need to optimize this now, is fast enough.
70                 */
71
72                for(itDSD = listDSC.begin() ; itDSD != listDSC.end() ; itDSD++ ) {
73                        if((TString)(*itDSD) == tempName || (TString)(*itDSD) == tempName2) {
74                                /* get the good filename in the list and erase the entry
75                                   in  listDSC.  So it gets faster next time. */
76                                orderedString.push_back((*itDSD));
77                                listDSC.erase(itDSD);
78                                //std::cout << " !!! new size = " << listDSC.size() << std::endl;
79                                //std::cout << " !!! found --> " << (*itDSD) << std::endl;
80                                break;
81                        }
82                }
83
84        }
85
86        return orderedString;
87}
88
89std::vector<std::string> ListHandler::getListToLoop(TString tempScratchDir, Int_t typeF){
90
91        TString filename = "";
92        if(tempScratchDir.Length() > 0){
93                filename += tempScratchDir;
94                filename += "/";
95        }
96
97        if(typeF==FRAME_FILES) filename += "listOfFiles.txt";
98        if(typeF==DSC_FILES) filename += "listOfFiles.dsc.txt";
99
100        std::string command0 = "/bin/bash -c 'rm -f "; // listOfFiles.txt"   "'";
101        command0 += filename.Data();
102        command0 += "'";
103
104        std::string command02 = "/bin/bash -c 'rm -f ";
105        command02 += filename.Data();
106        command02 += "'";
107
108        std::string commandn = "/bin/bash -c 'ls ";
109        std::string commandn2 = "/bin/bash -c 'ls ";
110
111        std::string command1 = "/bin/bash -c 'for a in ";
112        std::string command2 = "/bin/bash -c 'for a in ";
113
114        /* how many .dsc files do we have ? */
115        commandn += m_dirPath + " | grep \".dsc\" | wc -l >> "; //listOfFiles.txt'";
116        commandn += filename.Data();
117        commandn += "'";
118
119        commandn2 += m_dirPath + " | grep \".dsc\" | wc -l >> "; //listOfFiles.dsc.txt'";
120        commandn2 += filename.Data();
121        commandn2 += "'";
122
123        /* get the files that are not dsc, sometimes frame data comes
124     withouth extention (.txt), or can be any other */
125        command1 += m_dirPath + "/* ; do [[ $a == *.dsc ]] || echo $a >> "; // listOfFiles.txt ; done'";
126        command1 += filename.Data();
127        command1 += " ; done'";
128
129
130        /* get the files that are dsc */
131        command2 += m_dirPath + "/* ; do [[ $a == *.dsc ]] && echo $a >> "; // listOfFiles.dsc.txt ; done'";
132        command2 += filename.Data();
133        command2 += " ; done'";
134
135        /* get old files erased */
136        if(typeF==FRAME_FILES) system(command0.c_str());
137        if(typeF==DSC_FILES) system(command02.c_str());
138
139        if(typeF==FRAME_FILES) system(commandn.c_str());
140        if(typeF==DSC_FILES) system(commandn2.c_str());
141
142        int rcm = 0, rcm2 = 0;
143        if(typeF==FRAME_FILES) rcm = system(command1.c_str());
144        if(typeF==DSC_FILES) rcm2 = system(command2.c_str());
145
146        if(rcm != 0){
147                std::cout << "[ERROR] unexpected error when listing files inside " << m_dirPath << std::endl;
148                std::cout << "        please READ all the bash errors above. " << std::endl;
149                std::cout << "        Other possible reasons: " << std::endl;
150                std::cout << "        I need to make temporary files.  May be you " << std::endl;
151                std::cout << "        don't have 'w' permissions in this directory ? " << std::endl;
152                std::cout << "        Use the third (optional) parameter to this " << std::endl;
153                std::cout << "        program to change the temp dir" << std::endl;
154                exit(1);
155        }
156
157        fstream filestrList;
158        filestrList.open(filename, fstream::in);
159
160        std::string lineTemp;
161        std::vector<std::string> listOfFiles;
162        Int_t cntrInFile = 0;
163        Int_t cntrInActualFiles = 0;
164        Int_t nFiles = 0;
165
166        while (filestrList.good())
167        {
168                filestrList >> lineTemp;
169
170                if(cntrInFile == 0)
171                {
172                        nFiles = atoi(lineTemp.c_str());
173                }
174                if(cntrInFile > 0 && cntrInFile <= nFiles)
175                {
176                        //std::cout << lineTemp << std::endl;
177                        listOfFiles.push_back(lineTemp);
178                        cntrInActualFiles++;
179                }
180                cntrInFile++;
181        }
182
183        if(nFiles != cntrInActualFiles){
184                std::cout << "[ERROR] There should be " << nFiles << " frame (and .dsc) files in this directory and I found "
185                                << cntrInFile << "... giving up.";
186                exit(1);
187        }
188        return listOfFiles;
189}
Note: See TracBrowser for help on using the repository browser.