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

Contents of /yoda/event/PamelaRun.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2.0 - (show annotations) (download)
Tue Sep 21 20:49:57 2004 UTC (20 years, 2 months ago) by kusanagi
Branch: MAIN
Changes since 1.6: +2 -2 lines
Major release

1 /** @file
2 * $Author: kusanagi $
3 * $Date: 2004/09/21 20:23:37 $
4 * $Revision: 1.6 $
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.flush();
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.flush();
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.flush();
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.flush();
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.flush();
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.flush();
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.flush();
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.flush();
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 if (!File->IsOpen()) {
407 oss.flush();
408 oss << "Could not open file " << FileName ;
409 logger->error(oss.str().c_str());
410 throw Exception("Could not open file " + FileName);
411 }
412 std::string TreeName = GetTreeName(packet);
413 TTree *tree = new TTree(TreeName.c_str(), algo->GetAlgorithmName().c_str());
414 WritingRootTrees[packet->GetPacketType()].push_back(tree);
415 tree->GetCurrentFile()->cd();
416 AlgorithmInfo ai(algo);
417 ai.Write();
418 info.Write();
419 oss.flush();
420 oss << "Creating file " << FileName << " with Tree " << TreeName;
421 logger->info(oss.str().c_str());
422 return tree;
423 }
424
425
426 /**
427 * Register a certain SubPacket to be written to the repository. A
428 * usual call sequence for this function ist
429 * MyEvent *event = new MyEvent();
430 * run->WriteSubPacket(this, &event, event->Class()
431 * @param algo Algorithm that produces this SubPacket.
432 * @param subpacket A pointer to the pointer of the subpacket.
433 * @param c The class the subpacket belongs to.
434 * @param name The name of the packet.
435 */
436 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
437 const TClass *c, std::string name) {
438 SubPacket *packet = *(SubPacket **)subpacket;
439 oss.flush();
440 oss << "Register: " << name << " for " << algo->GetAlgorithmName() << " (writing)";
441 logger->debug(oss.str().c_str());
442 TTree* HeaderTree = ReadTTree(packet->GetPacketType());
443 TTree* tree = CreateTTree(algo, packet, name);
444 oss.flush();
445 oss << "Branch: " << name << " Class: " << c->GetName();
446 logger->debug(oss.str().c_str());
447 tree->Branch(name.c_str(), c->GetName(), subpacket);
448 HeaderTree->AddFriend(tree, tree->GetName());
449
450 std::string FullName = packet->GetPacketType()->GetName() + "." + name;
451 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
452 }
453
454 /**
455 * Register a certain SubPacket with its default name to be written to
456 * the repository. A usual call sequence for this function ist
457 * MyEvent *event = new MyEvent();
458 * run->WriteSubPacket(this, &event, event->Class()
459 * @param algo Algorithm that produces this SubPacket.
460 * @param subpacket A pointer to the pointer of the subpacket.
461 * @param c The class the subpacket belongs to.
462 */
463 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
464 const TClass *c) {
465 SubPacket *packet = *(SubPacket **)subpacket;
466 WriteSubPacket(algo, subpacket, c, GetDefaultBranchName(packet));
467 }
468
469 /**
470 * Write the header packet of a specified packet type. This is mainly used
471 * for the raw reader to create the base for the event structure.
472 * @param algo Algorithm that produces this SubPacket.
473 * @param subpacket A pointer to the pointer of the packet.
474 * @param type The packet type.
475 */
476 void PamelaRun::WriteHeader(Algorithm* algo, EventHeader** subpacket,
477 const PacketType* type) {
478 oss.flush();
479 oss << "Creating header tree for " << type->GetName();
480 logger->debug(oss.str().c_str());
481 EventHeader header(type);
482 std::string FileName = GetFileName(&header, GetDefaultBranchName(&header));
483 std::string EventType = (&header)->GetPacketType()->GetName();
484 CreateDirectoryStructure(FileName.c_str());
485 TFile* File = new TFile(FileName.c_str(), "create");
486 if (!File->IsOpen()) {
487 oss.flush();
488 oss << "Could not open file " << FileName ;
489 logger->error(oss.str().c_str());
490 throw Exception("Could not open file " + FileName);
491 }
492 //std::string TreeName = GetTreeName(packet);
493 TTree *tree = new TTree("Pscu", algo->GetAlgorithmName().c_str());
494 WritingRootTrees[(&header)->GetPacketType()].push_back(tree);
495 tree->GetCurrentFile()->cd();
496 AlgorithmInfo ai(algo);
497 ai.Write();
498 info.Write();
499 oss.flush();
500 oss << "Creating file " << FileName << " with Tree Pscu";
501 logger->info(oss.str().c_str());
502 tree->Branch(GetDefaultBranchName(&header).c_str(),
503 (*subpacket)->Class()->GetName(), subpacket);
504 TTreeMap.insert(RootTreeMap::value_type(type, tree));
505 }
506
507
508 /**
509 * Write the header packet to all ROOT files in the tree. Intended to
510 * be used for raw data readers that create the initial event structure.
511 * @param algo Algorithm that produces this SubPacket.
512 * @param subpacket A pointer to the pointer of the header packet.
513 */
514 void PamelaRun::WriteHeaders(Algorithm* algo, EventHeader** subpacket) {
515
516 WriteHeader(algo, subpacket, PacketType::PhysEndRun);
517 WriteHeader(algo, subpacket, PacketType::CalibCalPulse1);
518 WriteHeader(algo, subpacket, PacketType::CalibCalPulse2);
519 WriteHeader(algo, subpacket, PacketType::Physics);
520 WriteHeader(algo, subpacket, PacketType::CalibTrkBoth);
521 WriteHeader(algo, subpacket, PacketType::CalibTrk1);
522 WriteHeader(algo, subpacket, PacketType::CalibTrk2);
523 WriteHeader(algo, subpacket, PacketType::CalibTrd);
524 WriteHeader(algo, subpacket, PacketType::CalibTof);
525 WriteHeader(algo, subpacket, PacketType::CalibS4);
526 WriteHeader(algo, subpacket, PacketType::CalibCalPed);
527 WriteHeader(algo, subpacket, PacketType::CalibAc);
528 WriteHeader(algo, subpacket, PacketType::RunHeader);
529 WriteHeader(algo, subpacket, PacketType::RunTrailer);
530 WriteHeader(algo, subpacket, PacketType::CalibHeader);
531 WriteHeader(algo, subpacket, PacketType::CalibTrailer);
532 WriteHeader(algo, subpacket, PacketType::InitHeader);
533 WriteHeader(algo, subpacket, PacketType::InitTrailer);
534 WriteHeader(algo, subpacket, PacketType::EventTrk);
535 WriteHeader(algo, subpacket, PacketType::TestTrk);
536 WriteHeader(algo, subpacket, PacketType::TestTof);
537 WriteHeader(algo, subpacket, PacketType::Log);
538 WriteHeader(algo, subpacket, PacketType::VarDump);
539 WriteHeader(algo, subpacket, PacketType::ArrDump);
540 WriteHeader(algo, subpacket, PacketType::TabDump);
541 WriteHeader(algo, subpacket, PacketType::Tmtc);
542 WriteHeader(algo, subpacket, PacketType::Mcmd);
543 WriteHeader(algo, subpacket, PacketType::ForcedFECmd);
544 WriteHeader(algo, subpacket, PacketType::AcInit);
545 WriteHeader(algo, subpacket, PacketType::CalInit);
546 WriteHeader(algo, subpacket, PacketType::TrkInit);
547 WriteHeader(algo, subpacket, PacketType::TofInit);
548 WriteHeader(algo, subpacket, PacketType::TrgInit);
549 WriteHeader(algo, subpacket, PacketType::CalAlarm);
550 WriteHeader(algo, subpacket, PacketType::AcAlarm);
551 WriteHeader(algo, subpacket, PacketType::TrkAlarm);
552 WriteHeader(algo, subpacket, PacketType::TrgAlarm);
553 WriteHeader(algo, subpacket, PacketType::TofAlarm);
554 }
555
556 /**
557 * Write the ROOT files to disk.
558 */
559 void PamelaRun::WriteFiles(void) {
560 // Workaround: unlink all friend trees first top avoid to store
561 // the links in the header tree file.
562 for (RootTreeMap::iterator i = TTreeMap.begin(); i != TTreeMap.end(); i++) {
563 if (i->second->GetListOfFriends() != 0) {
564 i->second->GetListOfFriends()->Delete();
565 }
566 }
567
568 for (TTreeListMap::iterator i = WritingRootTrees.begin();
569 i != WritingRootTrees.end(); i++) {
570 for (TTreeList::iterator j = i->second.begin();
571 j != i->second.end(); j++) {
572 (*j)->GetCurrentFile()->Write();
573 }
574 }
575 }
576
577 /**
578 * Fill all ROOT trees of a certain type that were opened for writing.
579 * @param type the package type of the trees to fill.
580 */
581 void PamelaRun::FillTrees(const PacketType* type) {
582 for (TTreeList::iterator j = WritingRootTrees[type].begin();
583 j != WritingRootTrees[type].end(); j++) {
584 (*j)->Fill();
585 ;
586 }
587 }
588
589

  ViewVC Help
Powered by ViewVC 1.1.23