|
| 1 | +#if !defined(__CLING__) || defined(__ROOTCLING__) |
| 2 | +#include <TTree.h> |
| 3 | +#include <TChain.h> |
| 4 | +#include <TFile.h> |
| 5 | +#include <TStopwatch.h> |
| 6 | +#include <FairLogger.h> |
| 7 | +#include <vector> |
| 8 | +#include <string> |
| 9 | +#include <iomanip> |
| 10 | +#include "ITSMFTReconstruction/ChipMappingMFT.h" |
| 11 | +#include "ITSMFTReconstruction/GBTWord.h" |
| 12 | +#include "ITSMFTReconstruction/PayLoadCont.h" |
| 13 | +#include "DataFormatsITSMFT/ROFRecord.h" |
| 14 | +#include "ITSMFTBase/Digit.h" |
| 15 | +#endif |
| 16 | + |
| 17 | +#include "ITSMFTReconstruction/RawPixelReader.h" |
| 18 | + |
| 19 | +void run_digi2raw_mft(std::string outName = "rawmft.bin", // name of the output binary file |
| 20 | + std::string inpName = "mftdigits.root", // name of the input MFT digits |
| 21 | + std::string digTreeName = "o2sim", // name of the digits tree |
| 22 | + std::string digBranchName = "MFTDigit", // name of the digits branch |
| 23 | + std::string rofRecName = "MFTDigitROF", // name of the ROF records tree and its branch |
| 24 | + uint8_t ruSWMin = 0, uint8_t ruSWMax = 0xff, // seq.ID of 1st and last RU (stave) to convert |
| 25 | + uint8_t superPageSize = o2::itsmft::NCRUPagesPerSuperpage / 2 // CRU superpage size, max = 256 |
| 26 | +) |
| 27 | +{ |
| 28 | + TStopwatch swTot; |
| 29 | + swTot.Start(); |
| 30 | + using ROFR = o2::itsmft::ROFRecord; |
| 31 | + using ROFRVEC = std::vector<o2::itsmft::ROFRecord>; |
| 32 | + |
| 33 | + ///-------> input |
| 34 | + TChain digTree(digTreeName.c_str()); |
| 35 | + TChain rofTree(rofRecName.c_str()); |
| 36 | + |
| 37 | + digTree.AddFile(inpName.c_str()); |
| 38 | + rofTree.AddFile(inpName.c_str()); |
| 39 | + |
| 40 | + std::vector<o2::itsmft::Digit> digiVec, *digiVecP = &digiVec; |
| 41 | + if (!digTree.GetBranch(digBranchName.c_str())) { |
| 42 | + LOG(FATAL) << "Failed to find the branch " << digBranchName << " in the tree " << digTreeName; |
| 43 | + } |
| 44 | + digTree.SetBranchAddress(digBranchName.c_str(), &digiVecP); |
| 45 | + |
| 46 | + // ROF record entries in the digit tree |
| 47 | + ROFRVEC rofRecVec, *rofRecVecP = &rofRecVec; |
| 48 | + if (!rofTree.GetBranch(rofRecName.c_str())) { |
| 49 | + LOG(FATAL) << "Failed to find the branch " << rofRecName << " in the tree " << rofRecName; |
| 50 | + } |
| 51 | + rofTree.SetBranchAddress(rofRecName.c_str(), &rofRecVecP); |
| 52 | + ///-------< input |
| 53 | + |
| 54 | + ///-------> output |
| 55 | + if (outName.empty()) { |
| 56 | + outName = "raw" + digBranchName + ".raw"; |
| 57 | + LOG(INFO) << "Output file name is not provided, set to " << outName << FairLogger::endl; |
| 58 | + } |
| 59 | + auto outFl = fopen(outName.c_str(), "wb"); |
| 60 | + if (!outFl) { |
| 61 | + LOG(FATAL) << "failed to open raw data output file " << outName; |
| 62 | + ; |
| 63 | + } else { |
| 64 | + LOG(INFO) << "opened raw data output file " << outName; |
| 65 | + } |
| 66 | + o2::itsmft::PayLoadCont outBuffer; |
| 67 | + ///-------< output |
| 68 | + |
| 69 | + o2::itsmft::RawPixelReader<o2::itsmft::ChipMappingMFT> rawReader; |
| 70 | + rawReader.setPadding128(true); |
| 71 | + rawReader.setVerbosity(0); |
| 72 | + |
| 73 | + //------------------------------------------------------------------------------->>>> |
| 74 | + // just as an example, we require here that the IB staves are read via 3 links, |
| 75 | + // while OB staves use only 1 link. |
| 76 | + // Note, that if the RU container is not defined, it will be created automatically |
| 77 | + // during encoding. |
| 78 | + // If the links of the container are not defined, a single link readout will be assigned |
| 79 | + const auto& mp = rawReader.getMapping(); |
| 80 | + LOG(INFO) << "Number of RUs = " << mp.getNRUs(); |
| 81 | + for (int ir = 0; ir < mp.getNRUs(); ir++) { |
| 82 | + auto& ru = rawReader.getCreateRUDecode(ir); // create RU container |
| 83 | + uint32_t lanes = mp.getCablesOnRUType(ru.ruInfo->ruType); // lanes patter of this RU |
| 84 | + ru.links[0] = std::make_unique<o2::itsmft::GBTLink>(); |
| 85 | + ru.links[0]->lanes = lanes; // single link reads all lanes |
| 86 | + LOG(INFO) << "RU " << std::setw(3) << ir << " type " << int(ru.ruInfo->ruType) << " on lr" << int(ru.ruInfo->layer) |
| 87 | + << " : FEEId 0x" << std::hex << std::setfill('0') << std::setw(6) << mp.RUSW2FEEId(ir, int(ru.ruInfo->layer)) |
| 88 | + << " reads lanes " << std::bitset<25>(ru.links[0]->lanes); |
| 89 | + } |
| 90 | + |
| 91 | + //-------------------------------------------------------------------------------<<<< |
| 92 | + int lastTreeID = -1; |
| 93 | + long offs = 0, nEntProc = 0; |
| 94 | + for (int i = 0; i < rofTree.GetEntries(); i++) { |
| 95 | + rofTree.GetEntry(i); |
| 96 | + if (rofTree.GetTreeNumber() > lastTreeID) { // this part is needed for chained input |
| 97 | + if (lastTreeID > 0) { // new chunk, increase the offset |
| 98 | + offs += digTree.GetTree()->GetEntries(); |
| 99 | + } |
| 100 | + lastTreeID = rofTree.GetTreeNumber(); |
| 101 | + } |
| 102 | + |
| 103 | + for (const auto& rofRec : rofRecVec) { |
| 104 | + auto rofEntry = rofRec.getROFEntry(); |
| 105 | + int nDigROF = rofRec.getNROFEntries(); |
| 106 | + LOG(INFO) << "Processing ROF:" << rofRec.getROFrame() << " with " << nDigROF << " entries"; |
| 107 | + if (!nDigROF) { |
| 108 | + LOG(INFO) << "Frame is empty"; // ?? |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (rofEntry.getEvent() != digTree.GetReadEntry() + offs || !nEntProc) { |
| 112 | + digTree.GetEntry(rofEntry.getEvent() + offs); // read tree entry containing needed ROF data |
| 113 | + nEntProc++; |
| 114 | + } |
| 115 | + int digIndex = rofEntry.getIndex(); // needed ROF digits start from this one |
| 116 | + int maxDigIndex = digIndex + nDigROF; |
| 117 | + LOG(INFO) << "BV===== digIndex " << digIndex << " maxDigIndex " << maxDigIndex << "\n"; |
| 118 | + |
| 119 | + int nPagesCached = rawReader.digits2raw(digiVec, rofEntry.getIndex(), nDigROF, rofRec.getBCData(), |
| 120 | + ruSWMin, ruSWMax); |
| 121 | + |
| 122 | + if (nPagesCached >= superPageSize) { |
| 123 | + int nPagesFlushed = rawReader.flushSuperPages(superPageSize, outBuffer); |
| 124 | + fwrite(outBuffer.data(), 1, outBuffer.getSize(), outFl); //write to file |
| 125 | + outBuffer.clear(); |
| 126 | + LOG(INFO) << "Flushed " << nPagesFlushed << " CRU pages"; |
| 127 | + } |
| 128 | + //printf("BV===== stop after the first ROF!\n"); |
| 129 | + //break; |
| 130 | + } |
| 131 | + } // loop over multiple ROFvectors (in case of chaining) |
| 132 | + |
| 133 | + // flush the rest |
| 134 | + int flushed = 0; |
| 135 | + do { |
| 136 | + flushed = rawReader.flushSuperPages(o2::itsmft::NCRUPagesPerSuperpage, outBuffer); |
| 137 | + fwrite(outBuffer.data(), 1, outBuffer.getSize(), outFl); //write to file |
| 138 | + if (flushed) { |
| 139 | + LOG(INFO) << "Flushed final " << flushed << " CRU pages"; |
| 140 | + } |
| 141 | + outBuffer.clear(); |
| 142 | + } while (flushed); |
| 143 | + |
| 144 | + fclose(outFl); |
| 145 | + // |
| 146 | + swTot.Stop(); |
| 147 | + swTot.Print(); |
| 148 | +} |
0 commit comments