/[PAMELA software]/yoda/event/PamelaRun.cpp
ViewVC logotype

Contents of /yoda/event/PamelaRun.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2.3 - (show annotations) (download)
Fri Dec 3 22:04:04 2004 UTC (20 years ago) by kusanagi
Branch: MAIN
Changes since 2.2: +20 -23 lines
Minor changes in several packets structure

1 /** @file
2 * $Author: kusanagi $
3 * $Date: 2004/10/17 12:28:15 $
4 * $Revision: 2.2 $
5 *
6 * Implementation of the PamelaRun class.
7 */
8 #include <iostream>
9 #include <iomanip>
10 #include <sstream>
11 #include <list>
12 #include <exception>
13 #include <string>
14 #include <log4cxx/logger.h>
15
16 #include <TFile.h> //Substituted by Maurizio 05 Feb 2004
17 #include <TTree.h> //Substituted by Maurizio 05 Feb 2004
18 #include <TChain.h> //Substituted by Maurizio 05 Feb 2004
19 #include <TKey.h> //Substituted by Maurizio 05 Feb 2004
20 #include <TList.h> //Substituted by Maurizio 05 Feb 2004
21
22
23 #include "PamelaRun.h"
24 #include "EventHeader.h"
25 #include "Algorithm.h"
26 #include "AlgorithmInfo.h"
27 #include "Exception.h"
28 #include <sys/stat.h>
29
30 extern "C" {
31 #include <dirent.h>
32 #include "DirectoryStructure.h"
33 }
34
35 using namespace pamela;
36
37 static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.PamelaRun"));
38 static std::stringstream oss;
39
40 /**
41 * Get the run name according to a certain run number.
42 * @param run Run number.
43 * @return a string with the run name.
44 */
45 std::string PamelaRun::GetRunName(int run) {
46 std::ostringstream t_Stream;
47 t_Stream << std::setw( 4 ) << std::setfill( '0' ) << run;
48 return "Run" + t_Stream.str();
49 }
50
51 /**
52 * Check if the run number is already assigned
53 * @param int run - Number of the run to check
54 * @return int
55 */
56 /*int PamelaRun::RunExists(int run) throw (std::exception){
57 int res;
58 DIR *dirp;
59 std::string pathDir((char*)getenv("YODA_DATA"));
60 pathDir = pathDir + "/";
61 pathDir = pathDir + GetRunName(run);
62 // Open the directory
63 if ((dirp = opendir(pathDir.c_str())) == NULL) {
64 res = 0;
65 } else {
66 closedir(dirp);
67 res = 1;
68 }
69 return res;
70 }*/
71
72
73 /**
74 * Check if the run number is already assigned
75 * @param int run - Number of the run to check
76 * @return int
77 */
78 std::string PamelaRun::RunExists(std::string input) throw (std::exception){
79 int res;
80 DIR *dirp;
81 std::string fileName;
82 std::string pathDir((char*)getenv("YODA_DATA"));
83 std::string tempName;
84 std::string::size_type pos;
85 std::ostringstream t_Stream;
86
87 pos = input.find_last_of("/");
88 //if ((pos == std::string::npos) && !input.empty()) ;//throw Exception("Directories not allowed....");
89 fileName = input.substr(pos+1);
90 pos = fileName.find_last_of(".");
91 fileName = fileName.substr(0,pos);
92 pathDir = pathDir + "/";
93 tempName = pathDir + fileName;
94
95 int i = 0;
96 while (res){
97 if ((dirp = opendir(tempName.c_str())) == NULL) {
98 res = 0;
99 } else {
100 closedir(dirp);
101 t_Stream.str("");
102 t_Stream << std::setw( 2 ) << std::setfill( '0' ) << ++i;
103 tempName = pathDir + fileName + t_Stream.str() ;
104 res = 1;
105 }
106 }
107 return (fileName + t_Stream.str());
108 }
109
110 /**
111 * Create a new Pamela run.
112 * @param run Run number
113 * @param path Base path name to the data
114 */
115 PamelaRun::PamelaRun(std::string fileName, std::string path):
116 Path(path),
117 RunNumber(1){ //veirificare se รจ possibilie eliminare questa variabile
118 logger->debug(_T("Constructor"));
119 Run = RunExists(fileName);
120 info = RunInfo(this);
121 }
122
123 /**
124 * Get the directory name that contains the files of a certain event
125 * type for this run.
126 * @param type the packet type.
127 * @return the complete path for this event type.
128 */
129 std::string PamelaRun::GetDirName(PacketType const * type) const {
130 std::string EventType = type->GetName();
131 return Path + "/" + Run + "/" + EventType;
132 }
133
134 /**
135 * Get the file name for a certain event type.
136 * @param type subpacket type.
137 * @param name subpacket name.
138 * @return the complete path and file name.
139 */
140 std::string PamelaRun::GetFileName(const SubPacket* type, std::string name) const {
141 if (type->IsDetectorSpecific()) {
142 return GetDirName(type->GetPacketType()) + "/"
143 + type->GetSubDetector()->GetName() + "/"
144 + Run + "." + type->GetPacketType()->GetName() + "."
145 + type->GetSubDetector()->GetName() + "."
146 + name + ".root";
147 } else {
148 return GetDirName(type->GetPacketType()) + "/"
149 + Run + "." + type->GetPacketType()->GetName() + "."
150 + name + ".root";
151 }
152 }
153
154 /**
155 * Get the file name for a certain event type.
156 * @param type subpacket type.
157 * @return the complete path and file name.
158 */
159 std::string PamelaRun::GetFileName(const SubPacket* type) const {
160 return GetFileName(type, type->GetSubPacketName());
161 }
162
163 /**
164 * Get the branch name that corresponds to a certain
165 * subpacket. Usually, this is the name of the subpacket.
166 * @param type subpacket type.
167 * @return the corresponding branch name.
168 */
169 static std::string GetDefaultBranchName(const SubPacket* type) {
170 return type->GetSubPacketName();
171 }
172
173 /**
174 * Get the tree name for a certain subpacket. This is the name of the
175 * subdetector for detector specific subpackets, and the packet name
176 * ("Physics" etc.) for common packets.
177 * @param type the packet type.
178 * @return the corresponding tree name.
179 */
180 static std::string GetTreeName(const SubPacket* type) {
181 if (type->IsDetectorSpecific()) {
182 return type->GetSubDetector()->GetName();
183 } else {
184 return type->GetPacketType()->GetName();
185 }
186 }
187
188 /**
189 * Get a list of all root files from a certain path.
190 * @param path Path name to start the search.
191 * @return A list of all file names.
192 */
193 static std::list<std::string> GetRootFiles(std::string path) throw (std::exception) {
194 std::list<std::string> files;
195 DIR *dir = opendir(path.c_str());
196 if (dir == 0) {
197 oss.str("");
198 oss << "Could not open " << path;
199 logger->debug(oss.str().c_str());
200 //throw Exception("Could not open " + path);
201 }
202
203 for (struct dirent *d = readdir(dir); d != NULL; d = readdir(dir)) {
204 if ((strcmp(".",d->d_name) == 0) || (strcmp("..",d->d_name) == 0))
205 continue;
206 std::string filename = path + "/" + d->d_name;
207 struct stat buf;
208 stat(filename.c_str(), &buf);
209 if S_ISDIR(buf.st_mode) {
210 std::list<std::string>toAdd = GetRootFiles(filename);
211 files.insert(files.end(), toAdd.begin(), toAdd.end());
212 } else if ((filename.substr(filename.size()-5, filename.size())
213 == ".root") // correct suffix
214 && (!isdigit(filename[filename.size()-6]))) { // base file
215 std::string nextfilename = filename;
216 nextfilename.insert(filename.size()-5, "_1");
217 if (stat(nextfilename.c_str(), &buf) == 0) {
218 filename.insert(filename.size()-5, "*");
219 }
220 oss.str("");
221 oss << "Using " << filename;
222 logger->debug(oss.str().c_str());
223 files.push_back(filename);
224 }
225 }
226 return files;
227 }
228
229 /**
230 * Helper function to open the ROOT TTree of the header within the run
231 * framework.
232 * @param type The packet type.
233 * @return the ROOT TTree.
234 */
235 TChain* PamelaRun::ReadHeaderTree(const PacketType *type)
236 throw (std::exception) {
237 EventHeader header(type);
238 std::string FileName = GetFileName(&header);
239 std::string TreeName = GetTreeName(&header);
240 TChain *tree = new TChain(TreeName.c_str());
241 tree->Add(FileName.c_str());
242 return tree;
243 }
244
245 /**
246 * All add trees of a file as friend to a given tree. The tree name of
247 * each tree found in the file is used as the name of the friend alias.
248 * @param tree The base tree to add all other as friends.
249 * @param FileName The name of the file with other trees.
250 */
251 static void AddAllAsFriend(TChain* tree, std::string FileName) {
252 std::string BaseFileName = FileName;
253 bool HaveChain = false;
254 if (BaseFileName[BaseFileName.size()-6] == '*') {
255 BaseFileName.erase(BaseFileName.size()-6, 1);
256 HaveChain = true;
257 }
258 TFile* File = new TFile(BaseFileName.c_str(), "read");
259 if (!File->IsOpen()) {
260 oss.str("");
261 oss << "Could not open file " << BaseFileName << " for reading.";
262 logger->warn(oss.str().c_str());
263 return;
264 }
265 TList* list = File->GetListOfKeys();
266 if (HaveChain) {
267 for (TIter i(list); TKey* k = (TKey*)i();) {
268 if (std::string(TTree::Class()->GetName()) == k->GetClassName()) {
269 std::string TreeName = k->GetName();
270 TChain* FriendChain = new TChain(TreeName.c_str());
271 FriendChain->Add(FileName.c_str());
272 tree->AddFriend(FriendChain, TreeName.c_str());
273 oss.str("");
274 oss << "Adding chain " << TreeName << " with "
275 << FriendChain->GetEntries() << " entries as Friend";
276 logger->warn(oss.str().c_str());
277 }
278 }
279 File->Close();
280 } else {
281 for (TIter i(list); TKey* k = (TKey*)i();) {
282 if (std::string(TTree::Class()->GetName()) == k->GetClassName()) {
283 std::string TreeName = k->GetName();
284 TTree* FriendTree = (TTree *)File->Get(TreeName.c_str());
285 tree->AddFriend(FriendTree, TreeName.c_str());
286 oss.str("");
287 oss << "Adding tree " << TreeName << " with "
288 << FriendTree->GetEntries() << " entries as Friend";
289 logger->debug(oss.str().c_str());
290 }
291 }
292 }
293 }
294
295 /**
296 * Read all Root TTrees which belong to a certain event type and mount them
297 * together as "friends".
298 * @param type The packet type.
299 * @return The root trees with the friends.
300 */
301 TTree* PamelaRun::ReadTTree(const PacketType* type) {
302 oss.str("");
303 oss << "Getting root files of " << type->GetName();
304 logger->debug(oss.str().c_str());
305 RootTreeMap::iterator t = TTreeMap.find(type);
306 if (t != TTreeMap.end()) {
307 return t->second;
308 } else {
309 oss.str("");
310 oss << "Reading root files of " << type->GetName();
311 logger->debug(oss.str().c_str());
312 EventHeader header(type);
313 std::string HeaderFileName = GetFileName(&header);
314 TChain* HeaderTree = ReadHeaderTree(type);
315 std::list<std::string> rootfiles = GetRootFiles(GetDirName(type));
316 for (std::list<std::string>::iterator i = rootfiles.begin();
317 i != rootfiles.end(); i++){
318 if (*i == HeaderFileName)
319 continue; // dont add the header tree itself.
320 AddAllAsFriend(HeaderTree, *i);
321 }
322 TTreeMap.insert(RootTreeMap::value_type(type, HeaderTree));
323 return HeaderTree;
324 }
325 }
326
327 /**
328 * Register a certain SubPacket, identified by its name, to be read
329 * from the repository. This function is made for interactive work.
330 * @param subpacket A pointer to the pointer of the packet.
331 * @param name The name of the subpacket
332 */
333 void PamelaRun::ReadSubPacket(void* subpacket, std::string name) {
334 SubPacket *packet = *(SubPacket**)subpacket;
335
336 // look into the map of subpackets if we already read it.
337 std::string FullName = packet->GetPacketType()->GetName() + "." + name;
338 SubPacketMap::iterator i = SubPacketAddresses.find(FullName);
339 if (i != SubPacketAddresses.end()) { // it is in the map
340 *(SubPacket**)subpacket = i->second;
341 return;
342 } else { // not found in the map of used subpackets
343 TTree* tree = ReadTTree(packet->GetPacketType());
344 TBranch* branch = tree->GetBranch(name.c_str());
345 if (branch != 0) {
346 branch->SetAddress(subpacket);
347 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
348 } else {
349 oss.str("");
350 oss << "Could not find data for " << packet->GetPacketType()->GetName() << "/" << name ;
351 logger->error(oss.str().c_str());
352 }
353 }
354 }
355
356 /**
357 * Register a certain SubPacket, identified by its default name, to be
358 * read from the repository. This function is made for
359 * interactive work.
360 * @param subpacket A pointer to the pointer of the packet.
361 */
362 void PamelaRun::ReadSubPacket(void* subpacket) {
363 SubPacket *packet = *(SubPacket**)subpacket;
364 ReadSubPacket(subpacket, GetDefaultBranchName(packet));
365 }
366
367 /**
368 * Register a certain SubPacket with its default name, to be read from
369 * the repository. This functions is for use from the algorithm.
370 * @param algo Algorithm that needs this SubPacket.
371 * @param subpacket A pointer to the pointer of the packet.
372 */
373 void PamelaRun::ReadSubPacket(const Algorithm* algo, void* subpacket) {
374 //:TODO: store the request of the algorithm here.
375 ReadSubPacket(subpacket);
376 }
377
378 /**
379 * Register a certain SubPacket, with a custom name, to
380 * be read from the repository. This functions is for use from
381 * the algorithm.
382 * @param algo Algorithm that needs this SubPacket.
383 * @param subpacket A pointer to the pointer of the packet.
384 * @param name The name of the subpacket
385 */
386 void PamelaRun::ReadSubPacket(const Algorithm* algo, void* subpacket,
387 std::string name) {
388 //:TODO: store the request of the algorithm here.
389 ReadSubPacket(subpacket, name);
390 }
391
392 /**
393 * Helper function to create a ROOT TTree within the run framework.
394 * @param algo Algorithm that creates this SubPacket.
395 * @param packet subpacket type
396 * @param name the name of the subpacket
397 * @return the ROOT TTree.
398 */
399 TTree* PamelaRun::CreateTTree(Algorithm* algo, const SubPacket* packet,
400 std::string name)
401 throw (std::exception) {
402 std::string FileName = GetFileName(packet, name);
403 std::string EventType = packet->GetPacketType()->GetName();
404 CreateDirectoryStructure(FileName.c_str());
405 TFile* File = new TFile(FileName.c_str(), "create");
406 File->SetCompressionLevel(5);
407 if (!File->IsOpen()) {
408 oss.str("");
409 oss << "Could not open file " << FileName ;
410 logger->error(oss.str().c_str());
411 //throw Exception("Could not open file " + FileName);
412 }
413 std::string TreeName = GetTreeName(packet);
414 TTree *tree = new TTree(TreeName.c_str(), algo->GetAlgorithmName().c_str());
415 WritingRootTrees[packet->GetPacketType()].push_back(tree);
416 tree->GetCurrentFile()->cd();
417 AlgorithmInfo ai(algo);
418 ai.Write();
419 info.Write();
420 oss.str("");
421 oss << "Creating file " << FileName << " with Tree " << TreeName;
422 logger->info(oss.str().c_str());
423 return tree;
424 }
425
426
427 /**
428 * Register a certain SubPacket to be written to the repository. A
429 * usual call sequence for this function ist
430 * MyEvent *event = new MyEvent();
431 * run->WriteSubPacket(this, &event, event->Class()
432 * @param algo Algorithm that produces this SubPacket.
433 * @param subpacket A pointer to the pointer of the subpacket.
434 * @param c The class the subpacket belongs to.
435 * @param name The name of the packet.
436 */
437 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
438 const TClass *c, std::string name) {
439 SubPacket *packet = *(SubPacket **)subpacket;
440 oss.str("");
441 oss << "Register: " << name << " for " << algo->GetAlgorithmName() << " (writing)";
442 logger->debug(oss.str().c_str());
443 TTree* HeaderTree = ReadTTree(packet->GetPacketType());
444 TTree* tree = CreateTTree(algo, packet, name);
445 oss.str("");
446 oss << "Branch: " << name << " Class: " << c->GetName();
447 logger->debug(oss.str().c_str());
448 tree->Branch(name.c_str(), c->GetName(), subpacket);
449 HeaderTree->AddFriend(tree, tree->GetName());
450
451 std::string FullName = packet->GetPacketType()->GetName() + "." + name;
452 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
453 }
454
455 /**
456 * Register a certain SubPacket with its default name to be written to
457 * the repository. A usual call sequence for this function ist
458 * MyEvent *event = new MyEvent();
459 * run->WriteSubPacket(this, &event, event->Class()
460 * @param algo Algorithm that produces this SubPacket.
461 * @param subpacket A pointer to the pointer of the subpacket.
462 * @param c The class the subpacket belongs to.
463 */
464 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
465 const TClass *c) {
466 SubPacket *packet = *(SubPacket **)subpacket;
467 WriteSubPacket(algo, subpacket, c, GetDefaultBranchName(packet));
468 }
469
470 /**
471 * Write the header packet of a specified packet type. This is mainly used
472 * for the raw reader to create the base for the event structure.
473 * @param algo Algorithm that produces this SubPacket.
474 * @param subpacket A pointer to the pointer of the packet.
475 * @param type The packet type.
476 */
477 void PamelaRun::WriteHeader(Algorithm* algo, EventHeader** subpacket,
478 const PacketType* type) {
479 EventHeader header(type);
480 std::string FileName = GetFileName(&header, GetDefaultBranchName(&header));
481 std::string EventType = (&header)->GetPacketType()->GetName();
482 CreateDirectoryStructure(FileName.c_str());
483 TFile* File = new TFile(FileName.c_str(), "create");
484 if (!File->IsOpen()) {
485 oss.str("");
486 oss << "Could not open file " << FileName ;
487 logger->error(oss.str().c_str());
488 //throw Exception("Could not open file " + FileName);
489 }
490 //std::string TreeName = GetTreeName(packet);
491 TTree *tree = new TTree("Pscu", algo->GetAlgorithmName().c_str());
492 WritingRootTrees[(&header)->GetPacketType()].push_back(tree);
493 tree->GetCurrentFile()->cd();
494 AlgorithmInfo ai(algo);
495 ai.Write();
496 info.Write();
497 oss.str("");
498 oss << "Created file " << FileName << " with Tree Pscu";
499 logger->info(oss.str().c_str());
500 tree->Branch(GetDefaultBranchName(&header).c_str(),
501 (*subpacket)->Class()->GetName(), subpacket);
502 TTreeMap.insert(RootTreeMap::value_type(type, tree));
503 }
504
505
506 /**
507 * Write the header packet to all ROOT files in the tree. Intended to
508 * be used for raw data readers that create the initial event structure.
509 * @param algo Algorithm that produces this SubPacket.
510 * @param subpacket A pointer to the pointer of the header packet.
511 */
512 void PamelaRun::WriteHeaders(Algorithm* algo, EventHeader** subpacket) {
513
514 WriteHeader(algo, subpacket, PacketType::PhysEndRun);
515 WriteHeader(algo, subpacket, PacketType::CalibCalPulse1);
516 WriteHeader(algo, subpacket, PacketType::CalibCalPulse2);
517 WriteHeader(algo, subpacket, PacketType::Physics);
518 WriteHeader(algo, subpacket, PacketType::CalibTrkBoth);
519 WriteHeader(algo, subpacket, PacketType::CalibTrk1);
520 WriteHeader(algo, subpacket, PacketType::CalibTrk2);
521 WriteHeader(algo, subpacket, PacketType::CalibTrd);
522 WriteHeader(algo, subpacket, PacketType::CalibTof);
523 WriteHeader(algo, subpacket, PacketType::CalibS4);
524 WriteHeader(algo, subpacket, PacketType::CalibCalPed);
525 WriteHeader(algo, subpacket, PacketType::CalibAc);
526 WriteHeader(algo, subpacket, PacketType::RunHeader);
527 WriteHeader(algo, subpacket, PacketType::RunTrailer);
528 WriteHeader(algo, subpacket, PacketType::CalibHeader);
529 WriteHeader(algo, subpacket, PacketType::CalibTrailer);
530 WriteHeader(algo, subpacket, PacketType::InitHeader);
531 WriteHeader(algo, subpacket, PacketType::InitTrailer);
532 WriteHeader(algo, subpacket, PacketType::EventTrk);
533 WriteHeader(algo, subpacket, PacketType::TestTrk);
534 WriteHeader(algo, subpacket, PacketType::TestTof);
535 WriteHeader(algo, subpacket, PacketType::Log);
536 WriteHeader(algo, subpacket, PacketType::VarDump);
537 WriteHeader(algo, subpacket, PacketType::ArrDump);
538 WriteHeader(algo, subpacket, PacketType::TabDump);
539 WriteHeader(algo, subpacket, PacketType::Tmtc);
540 WriteHeader(algo, subpacket, PacketType::Mcmd);
541 WriteHeader(algo, subpacket, PacketType::ForcedFECmd);
542 WriteHeader(algo, subpacket, PacketType::AcInit);
543 WriteHeader(algo, subpacket, PacketType::CalInit);
544 WriteHeader(algo, subpacket, PacketType::TrkInit);
545 WriteHeader(algo, subpacket, PacketType::TofInit);
546 WriteHeader(algo, subpacket, PacketType::TrgInit);
547 WriteHeader(algo, subpacket, PacketType::NdInit);
548 WriteHeader(algo, subpacket, PacketType::CalAlarm);
549 WriteHeader(algo, subpacket, PacketType::AcAlarm);
550 WriteHeader(algo, subpacket, PacketType::TrkAlarm);
551 WriteHeader(algo, subpacket, PacketType::TrgAlarm);
552 WriteHeader(algo, subpacket, PacketType::TofAlarm);
553 }
554
555 /**
556 * Write the ROOT files to disk.
557 */
558 void PamelaRun::WriteFiles(void) {
559 // Workaround: unlink all friend trees first top avoid to store
560 // the links in the header tree file.
561 for (RootTreeMap::iterator i = TTreeMap.begin(); i != TTreeMap.end(); i++) {
562 if (i->second->GetListOfFriends() != 0) {
563 i->second->GetListOfFriends()->Delete();
564 }
565 }
566
567 for (TTreeListMap::iterator i = WritingRootTrees.begin();
568 i != WritingRootTrees.end(); i++) {
569 for (TTreeList::iterator j = i->second.begin();
570 j != i->second.end(); j++) {
571 (*j)->GetCurrentFile()->Write();
572 }
573 }
574 }
575
576 /**
577 * Fill all ROOT trees of a certain type that were opened for writing.
578 * @param type the package type of the trees to fill.
579 */
580 void PamelaRun::FillTrees(const PacketType* type) {
581 for (TTreeList::iterator j = WritingRootTrees[type].begin();
582 j != WritingRootTrees[type].end(); j++) {
583 (*j)->Fill();
584 ;
585 }
586 }
587
588

  ViewVC Help
Powered by ViewVC 1.1.23